-
-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
82 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package e2etest | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/cenkalti/backoff/v4" | ||
|
||
"github.com/gotd/td/bin" | ||
"github.com/gotd/td/tg" | ||
"github.com/gotd/td/tgerr" | ||
) | ||
|
||
type retryInvoker struct { | ||
prev tg.Invoker | ||
} | ||
|
||
func retryResult[T any](ctx context.Context, cb func() (T, error)) (T, error) { | ||
var zero T | ||
return backoff.RetryWithData[T](func() (T, error) { | ||
res, err := cb() | ||
if err != nil { | ||
if tgerr.IsCode(err, -500) { | ||
return zero, err | ||
} | ||
if tgerr.Is(err, "CONNECTION_NOT_INITED") { | ||
return zero, err | ||
} | ||
if ok, err := tgerr.FloodWait(ctx, err); ok { | ||
return zero, err | ||
} | ||
return zero, backoff.Permanent(err) | ||
} | ||
return res, nil | ||
}, backoff.WithContext(backoff.NewConstantBackOff(time.Millisecond*500), ctx)) | ||
} | ||
|
||
func retry(ctx context.Context, cb func() error) error { | ||
return backoff.Retry(func() error { | ||
if err := cb(); err != nil { | ||
if tgerr.IsCode(err, -500) { | ||
return err | ||
} | ||
if tgerr.Is(err, "CONNECTION_NOT_INITED") { | ||
return err | ||
} | ||
if ok, err := tgerr.FloodWait(ctx, err); ok { | ||
return err | ||
} | ||
return backoff.Permanent(err) | ||
} | ||
|
||
return nil | ||
}, backoff.WithContext(backoff.NewExponentialBackOff(), ctx)) | ||
} | ||
|
||
func (w retryInvoker) Invoke(ctx context.Context, input bin.Encoder, output bin.Decoder) error { | ||
return retry(ctx, func() error { | ||
return w.prev.Invoke(ctx, input, output) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.