From 45a14c8c0d09f10df2c4b857880a65848f25d5f9 Mon Sep 17 00:00:00 2001 From: "kahgeh.tan" Date: Thu, 27 Aug 2020 15:45:13 +1000 Subject: [PATCH] remove some logs --- .gitignore | 3 ++- .vscode/launch.json | 19 ------------------- main.go | 35 +++++++++++++++++++++-------------- 3 files changed, 23 insertions(+), 34 deletions(-) delete mode 100644 .vscode/launch.json diff --git a/.gitignore b/.gitignore index 587e4ec..b05260f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules .cache -dist \ No newline at end of file +dist +.vscode \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index 052e096..0000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - // Use IntelliSense to learn about possible attributes. - // Hover to view descriptions of existing attributes. - // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [ - { - "name": "Launch", - "type": "go", - "request": "launch", - "mode": "auto", - "program": "${fileDirname}", - "env": { - "PORT": 8080 - }, - "args": [] - } - ] -} \ No newline at end of file diff --git a/main.go b/main.go index 51db49a..9c6f76d 100644 --- a/main.go +++ b/main.go @@ -23,6 +23,17 @@ type safeChannel struct { mutex sync.Mutex } +func createClientChannel(clientID string) *safeChannel { + newClientChannel := &safeChannel{ + clientID: clientID, + value: make(chan event), + mutex: sync.Mutex{}, + } + clientChannels[clientID] = newClientChannel + log.Printf("created channel for client(%v)", clientID) + return newClientChannel +} + func (c *safeChannel) delete(clientChannelsMap map[string]*safeChannel) { if c == nil { return @@ -32,7 +43,7 @@ func (c *safeChannel) delete(clientChannelsMap map[string]*safeChannel) { log.Printf("deleting channel %v...", c.clientID) close(c.value) delete(clientChannelsMap, c.clientID) - log.Printf("deleting channel %v completed", c.clientID) + log.Printf("delete channel %v completed", c.clientID) } func (c *safeChannel) send(event event) { @@ -44,7 +55,7 @@ func (c *safeChannel) send(event event) { c.value <- event } -var clientChannels map[string]*safeChannel = make(map[string]*safeChannel) +var clientChannels = make(map[string]*safeChannel) func handlePing() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { @@ -53,11 +64,7 @@ func handlePing() http.HandlerFunc { } func sendEvent(w http.ResponseWriter, r *http.Request, clientID string) { - clientChannels[clientID] = &safeChannel{ - clientID: clientID, - value: make(chan event), - mutex: sync.Mutex{}, - } + createClientChannel(clientID) eventChannel := clientChannels[clientID].value defer func() { clientChannels[clientID].delete(clientChannels) @@ -68,18 +75,16 @@ func sendEvent(w http.ResponseWriter, r *http.Request, clientID string) { for { select { case event := <-eventChannel: - log.Printf("handler for %v: receiving event for %v", clientID, event.clientID) - if event.clientID == clientID { - fmt.Fprintf(w, "data: %s\n\n", event.payload) - flusher.Flush() - log.Printf("sending event to client %v", clientID) - } + fmt.Fprintf(w, "data: %s\n\n", event.payload) + flusher.Flush() + log.Printf("sending event to client %v", clientID) case <-r.Context().Done(): return } } } + func handleConnect(w http.ResponseWriter, r *http.Request, clientID string) { w.Header().Set("Content-Type", "text/event-stream") w.Header().Set("Cache-Control", "no-cache") @@ -89,11 +94,13 @@ func handleConnect(w http.ResponseWriter, r *http.Request, clientID string) { } func handleSend(event event) { + log.Printf("received send event request to client %v", event.clientID) clientChannel := clientChannels[event.clientID] if clientChannel != nil { - log.Printf("receive send event request to client %v", event.clientID) clientChannel.send(event) + return } + log.Printf("send event request dropped because client(%v) doesn't exist", event.clientID) } func handleEvent() http.HandlerFunc {