Browse Source

backlog must always have higher priority

pull/604/head
Anton Kaliaev 7 years ago
parent
commit
236489aecf
No known key found for this signature in database GPG Key ID: 7B6881D965918214
2 changed files with 26 additions and 13 deletions
  1. +22
    -12
      rpc/lib/client/ws_client.go
  2. +4
    -1
      rpc/lib/client/ws_client_test.go

+ 22
- 12
rpc/lib/client/ws_client.go View File

@ -247,6 +247,24 @@ func (c *WSClient) startReadWriteRoutines() {
go c.writeRoutine()
}
func (c *WSClient) processBacklog() error {
select {
case request := <-c.backlog:
c.conn.SetWriteDeadline(time.Now().Add(writeWait))
err := c.conn.WriteJSON(request)
if err != nil {
c.Logger.Error("failed to resend request", "err", err)
c.reconnectAfter <- err
// requeue request
c.backlog <- request
return err
}
c.Logger.Info("resend a request", "req", request)
default:
}
return nil
}
func (c *WSClient) reconnectRoutine() {
for {
select {
@ -268,7 +286,10 @@ func (c *WSClient) reconnectRoutine() {
break LOOP
}
}
c.startReadWriteRoutines()
err = c.processBacklog()
if err == nil {
c.startReadWriteRoutines()
}
}
case <-c.Quit:
return
@ -288,17 +309,6 @@ func (c *WSClient) writeRoutine() {
for {
select {
case request := <-c.backlog:
c.conn.SetWriteDeadline(time.Now().Add(writeWait))
err := c.conn.WriteJSON(request)
if err != nil {
c.Logger.Error("failed to resend request", "err", err)
c.reconnectAfter <- err
// add request to the backlog, so we don't lose it
c.backlog <- request
return
}
c.Logger.Info("resend a request", "req", request)
case request := <-c.send:
c.conn.SetWriteDeadline(time.Now().Add(writeWait))
err := c.conn.WriteJSON(request)


+ 4
- 1
rpc/lib/client/ws_client_test.go View File

@ -138,7 +138,10 @@ func TestWSClientReconnectFailure(t *testing.T) {
s.Close()
// results in WS write error
call(t, "a", c)
// provide timeout to avoid blocking
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
c.Call(ctx, "a", make(map[string]interface{}))
// expect to reconnect almost immediately
time.Sleep(10 * time.Millisecond)


Loading…
Cancel
Save