retry calls
This commit is contained in:
@@ -67,7 +67,7 @@ func (m *CartTCPClient) SendPacket(messageType CartMessage, id CartId, data []by
|
||||
return m.PersistentConnection.HandleConnectionError(err)
|
||||
}
|
||||
|
||||
func (m *CartTCPClient) Call(messageType CartMessage, id CartId, responseType CartMessage, data []byte) (*CallResult, error) {
|
||||
func (m *CartTCPClient) call(messageType CartMessage, id CartId, responseType CartMessage, data []byte) (*CallResult, error) {
|
||||
packetChan := m.Expect(responseType, id)
|
||||
err := m.SendPacket(messageType, id, data)
|
||||
if err != nil {
|
||||
@@ -76,8 +76,27 @@ func (m *CartTCPClient) Call(messageType CartMessage, id CartId, responseType Ca
|
||||
select {
|
||||
case ret := <-packetChan:
|
||||
return &ret, nil
|
||||
case <-time.After(time.Second):
|
||||
case <-time.After(time.Millisecond * 300):
|
||||
log.Printf("Timeout waiting for cart response to message type %d\n", responseType)
|
||||
return nil, m.PersistentConnection.HandleConnectionError(fmt.Errorf("timeout"))
|
||||
}
|
||||
}
|
||||
|
||||
func isRetirableError(err error) bool {
|
||||
log.Printf("is retryable error: %v", err)
|
||||
return false
|
||||
}
|
||||
|
||||
func (m *CartTCPClient) Call(messageType CartMessage, id CartId, responseType CartMessage, data []byte) (*CallResult, error) {
|
||||
retries := 0
|
||||
result, err := m.call(messageType, id, responseType, data)
|
||||
for err != nil && retries < 3 {
|
||||
if !isRetirableError(err) {
|
||||
break
|
||||
}
|
||||
retries++
|
||||
log.Printf("Retrying call to %d\n", messageType)
|
||||
result, err = m.call(messageType, id, responseType, data)
|
||||
}
|
||||
return result, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user