Skip to content

Commit

Permalink
dont recover upload if gin context is cancelled
Browse files Browse the repository at this point in the history
  • Loading branch information
divyam234 committed Nov 30, 2023
1 parent 88fd0ff commit e08ff97
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
19 changes: 18 additions & 1 deletion utils/recovery/recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ import (
"github.com/gotd/td/tgerr"
)

func hasError(err error, target string) bool {
for err != nil {
if err.Error() == target {
return true
}
if unwrapper, ok := err.(interface{ Unwrap() error }); ok {
err = unwrapper.Unwrap()
} else {
break
}
}
return false
}

type recovery struct {
ctx context.Context
backoff backoff.BackOff
Expand Down Expand Up @@ -47,7 +61,10 @@ func (r *recovery) shouldRecover(err error) bool {
default:
}

//recover only if context is not cancelled and error is not a rpc error
isContextErr := hasError(err, "context canceled")

_, ok := tgerr.As(err)

return !ok
return !isContextErr && !ok
}
10 changes: 7 additions & 3 deletions utils/retry/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import (
"github.com/gotd/td/tgerr"
)

var internalErrors = []string{
"Timedout",
"No workers running",
}

type retry struct {
max int
errors []string
Expand Down Expand Up @@ -38,8 +43,7 @@ func (r retry) Handle(next tg.Invoker) telegram.InvokeFunc {

func New(max int, errors ...string) telegram.Middleware {
return retry{
max: max,
errors: append(errors,
"Timedout"),
max: max,
errors: append(errors, internalErrors...),
}
}
3 changes: 1 addition & 2 deletions utils/tgc/tgc.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ func deviceConfig(appConfig *utils.Config) telegram.DeviceConfig {
return config
}
func NewDefaultMiddlewares(ctx context.Context) ([]telegram.Middleware, error) {
_clock := tdclock.System

return []telegram.Middleware{
recovery.New(ctx, Backoff(_clock)),
recovery.New(ctx, Backoff(tdclock.System)),
retry.New(5),
floodwait.NewSimpleWaiter(),
}, nil
Expand Down

0 comments on commit e08ff97

Please sign in to comment.