51 lines
1.4 KiB
Go
51 lines
1.4 KiB
Go
|
|
package services
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"errors"
|
||
|
|
"io"
|
||
|
|
"net"
|
||
|
|
"net/url"
|
||
|
|
"strings"
|
||
|
|
"syscall"
|
||
|
|
|
||
|
|
"code.tczkiot.com/wlw/ai-agent/contract"
|
||
|
|
)
|
||
|
|
|
||
|
|
// businessActionFailureIsRetryable is deliberately conservative. Transport
|
||
|
|
// failures can happen after the host has committed a write, so they always
|
||
|
|
// produce unknown_outcome even when wrapped in a customer-safe error.
|
||
|
|
func businessActionFailureIsRetryable(ctx context.Context, err error) bool {
|
||
|
|
if err == nil {
|
||
|
|
return true
|
||
|
|
}
|
||
|
|
if ctx != nil && ctx.Err() != nil {
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) ||
|
||
|
|
errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) ||
|
||
|
|
errors.Is(err, syscall.ECONNRESET) || errors.Is(err, syscall.ECONNABORTED) ||
|
||
|
|
errors.Is(err, syscall.EPIPE) {
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
var netErr net.Error
|
||
|
|
if errors.As(err, &netErr) && (netErr.Timeout() || netErr.Temporary()) {
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
var urlErr *url.Error
|
||
|
|
if errors.As(err, &urlErr) {
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
message := strings.ToLower(err.Error())
|
||
|
|
for _, marker := range []string{
|
||
|
|
"timeout", "timed out", "deadline exceeded", "context canceled",
|
||
|
|
"connection reset", "connection aborted", "broken pipe", "unexpected eof",
|
||
|
|
"server closed idle connection", "transport connection broken",
|
||
|
|
} {
|
||
|
|
if strings.Contains(message, marker) {
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return contract.BusinessActionErrorOutcome(err) == contract.BusinessActionFailureRetryable
|
||
|
|
}
|