Documentation
¶
Overview ¶
Package floodwait implements a tg.Invoker that handles flood wait errors.
Index ¶
- Constants
- func Middleware(opts ...MiddlewareOption) middleware.Middleware
- type MiddlewareOption
- type SimpleWaiter
- type Waiter
- func (w *Waiter) InvokeRaw(ctx context.Context, input bin.Encoder, output bin.Decoder) error
- func (w *Waiter) Run(ctx context.Context) error
- func (w *Waiter) WithClock(c clock.Clock) *Waiter
- func (w *Waiter) WithMaxRetries(m int) *Waiter
- func (w *Waiter) WithMaxWait(m time.Duration) *Waiter
- func (w *Waiter) WithTick(t time.Duration) *Waiter
Examples ¶
Constants ¶
const ErrFloodWait = "FLOOD_WAIT"
ErrFloodWait is error type of "FLOOD_WAIT" error.
Variables ¶
This section is empty.
Functions ¶
func Middleware ¶
func Middleware(opts ...MiddlewareOption) middleware.Middleware
Middleware returns a new SimpleWaiter middleware constructor.
Types ¶
type MiddlewareOption ¶
type MiddlewareOption func(w *SimpleWaiter) *SimpleWaiter
MiddlewareOption configures new SimpleWaiter in middleware constructor.
type SimpleWaiter ¶
type SimpleWaiter struct {
// contains filtered or unexported fields
}
SimpleWaiter is a tg.Invoker that handles flood wait errors on underlying invoker.
This implementation is more suitable for one-off tasks and programs with low level of concurrency and parallelism.
See Waiter for a fully-blown scheduler-based flood wait handler.
Example ¶
package main
import (
"time"
"github.com/gotd/td/middleware/floodwait"
"github.com/gotd/td/tg"
)
func main() {
var invoker tg.Invoker // e.g. *telegram.Client
waiter := floodwait.NewSimpleWaiter(invoker).
WithMaxWait(5 * time.Minute).
WithMaxRetries(3)
// Do something with waiter invoker.
// E.g. create a new RPC client.
tg.NewClient(waiter)
}
func NewSimpleWaiter ¶
func NewSimpleWaiter(invoker tg.Invoker) *SimpleWaiter
NewSimpleWaiter returns a new invoker that waits on the flood wait errors.
func (*SimpleWaiter) WithClock ¶
func (w *SimpleWaiter) WithClock(c clock.Clock) *SimpleWaiter
WithClock sets clock to use. Default is to use system clock.
func (*SimpleWaiter) WithMaxRetries ¶
func (w *SimpleWaiter) WithMaxRetries(m uint) *SimpleWaiter
WithMaxRetries sets max number of retries before giving up. Default is to keep retrying on flood wait errors indefinitely.
func (*SimpleWaiter) WithMaxWait ¶
func (w *SimpleWaiter) WithMaxWait(m time.Duration) *SimpleWaiter
WithMaxWait limits wait time per attempt. SimpleWaiter will return an error if flood wait time exceeds that limit. Default is to wait without time limit.
To limit total wait time use a context.Context with timeout or deadline set.
type Waiter ¶
type Waiter struct {
// contains filtered or unexported fields
}
Waiter is a tg.Invoker that handles flood wait errors on underlying invoker.
This implementation uses a request scheduler and is more suitable for long-running programs with high level of concurrency and parallelism.
You should use Waiter if unsure which waiter implementation to use.
See SimpleWaiter for a simple timer-based implementation.
Example ¶
package main
import (
"context"
"time"
"golang.org/x/sync/errgroup"
"github.com/gotd/td/middleware/floodwait"
"github.com/gotd/td/tg"
)
func main() {
var invoker tg.Invoker // e.g. *telegram.Client
waiter := floodwait.NewWaiter(invoker).
WithMaxWait(5 * time.Minute).
WithMaxRetries(3)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
g, ctx := errgroup.WithContext(ctx)
g.Go(func() error {
return waiter.Run(ctx)
})
g.Go(func() error {
// Cancel context for waiter goroutine even
// if we return nil error.
defer cancel()
// Do something with waiter invoker.
// E.g. create a new RPC client.
tg.NewClient(waiter)
return nil
})
if err := g.Wait(); err != nil {
panic(err)
}
}
func (*Waiter) WithMaxRetries ¶
WithMaxRetries sets max number of retries before giving up. Default is to retry at most 5 times.
func (*Waiter) WithMaxWait ¶
WithMaxWait limits wait time per attempt. Waiter will return an error if flood wait time exceeds that limit. Default is to wait at most a minute.
To limit total wait time use a context.Context with timeout or deadline set.