Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client are disconnected only on shutdown #118

Merged
merged 1 commit into from Aug 8, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 11 additions & 3 deletions rpcserver.go
Expand Up @@ -143,6 +143,7 @@ type websocketClient struct {
allRequests chan []byte
responses chan []byte
quit chan struct{} // closed on disconnect
wg sync.WaitGroup
}

func newWebsocketClient(c *websocket.Conn, authenticated bool, remoteAddr string) *websocketClient {
Expand Down Expand Up @@ -790,7 +791,7 @@ out:
break out
}
f := s.HandlerClosure(raw.Method)
s.wg.Add(1)
wsc.wg.Add(1)
go func(request []byte, raw *rawRequest) {
resp := f(request, raw)
mresp, err := json.Marshal(resp)
Expand All @@ -799,7 +800,7 @@ out:
}
_ = wsc.send(mresp)

s.wg.Done()
wsc.wg.Done()
}(request, &raw)
}

Expand All @@ -818,6 +819,9 @@ out:
<-s.notificationHandlerQuit
}

// allow client to disconnect after all handler goroutines are done
wsc.wg.Wait()
close(wsc.responses)
s.wg.Done()
}

Expand All @@ -826,7 +830,11 @@ func (s *rpcServer) WebsocketClientSend(wsc *websocketClient) {
out:
for {
select {
case response := <-wsc.responses:
case response, ok := <-wsc.responses:
if !ok {
// client disconnected
break out
}
err := wsc.conn.SetWriteDeadline(time.Now().Add(deadline))
if err != nil {
log.Warnf("Cannot set write deadline on "+
Expand Down