retry calls
All checks were successful
Build and Publish / BuildAndDeployAmd64 (push) Successful in 28s
Build and Publish / BuildAndDeploy (push) Successful in 2m23s

This commit is contained in:
matst80
2024-11-13 12:57:36 +01:00
parent edc8d471ab
commit dafefa377f

View File

@@ -67,7 +67,7 @@ func (m *CartTCPClient) SendPacket(messageType CartMessage, id CartId, data []by
return m.PersistentConnection.HandleConnectionError(err) 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) packetChan := m.Expect(responseType, id)
err := m.SendPacket(messageType, id, data) err := m.SendPacket(messageType, id, data)
if err != nil { if err != nil {
@@ -76,8 +76,27 @@ func (m *CartTCPClient) Call(messageType CartMessage, id CartId, responseType Ca
select { select {
case ret := <-packetChan: case ret := <-packetChan:
return &ret, nil 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) log.Printf("Timeout waiting for cart response to message type %d\n", responseType)
return nil, m.PersistentConnection.HandleConnectionError(fmt.Errorf("timeout")) 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
}