Skip to content

Commit

Permalink
fix timeout handling in write loop
Browse files Browse the repository at this point in the history
  • Loading branch information
ftl committed May 13, 2021
1 parent fac8d82 commit 26b3184
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,6 @@ func (c *Client) writeLoop(conn clientConn, incoming <-chan Message) {
defer timer.Stop()

for {
now := time.Now()
if currentCommand == nil {
select {
case msg := <-c.txAudio:
Expand All @@ -243,9 +242,11 @@ func (c *Client) writeLoop(conn clientConn, incoming <-chan Message) {
continue
}
case cmd := <-c.commands:
now := time.Now()
if cmd.reply != nil {
currentCommand = &cmd
currentDeadline = now.Add(c.timeout)
log.Printf("cmd %v now %v deadline %v", cmd, now, currentDeadline)
}
err := conn.WriteMessage(websocket.TextMessage, []byte(cmd.String()))
if err != nil {
Expand All @@ -256,7 +257,9 @@ func (c *Client) writeLoop(conn clientConn, incoming <-chan Message) {
continue
}
} else {
now := time.Now()
timer.Reset(currentDeadline.Sub(now))
log.Printf("cmd %v now %v deadline %v", *currentCommand, now, currentDeadline)
select {
case <-c.disconnectChan:
return
Expand Down

0 comments on commit 26b3184

Please sign in to comment.