package tgerr
import (
"context"
"time"
"github.com/gotd/td/clock"
)
const ErrFloodWait = "FLOOD_WAIT"
func AsFloodWait (err error ) (d time .Duration , ok bool ) {
if rpcErr , ok := AsType (err , ErrFloodWait ); ok {
return time .Second * time .Duration (rpcErr .Argument ), true
}
return 0 , false
}
type floodWaitOptions struct {
clock clock .Clock
}
type FloodWaitOption interface {
apply (o *floodWaitOptions )
}
type floodWaitOptionFunc func (o *floodWaitOptions )
func (f floodWaitOptionFunc ) apply (o *floodWaitOptions ) {
f (o )
}
func FloodWaitWithClock (c clock .Clock ) FloodWaitOption {
return floodWaitOptionFunc (func (o *floodWaitOptions ) {
o .clock = c
})
}
func FloodWait (ctx context .Context , err error , opts ...FloodWaitOption ) (bool , error ) {
opt := &floodWaitOptions {
clock : clock .System ,
}
for _ , o := range opts {
o .apply (opt )
}
if d , ok := AsFloodWait (err ); ok {
timer := opt .clock .Timer (d + 1 *time .Second )
defer clock .StopTimer (timer )
select {
case <- timer .C ():
return true , err
case <- ctx .Done ():
return false , ctx .Err ()
}
}
return false , err
}
The pages are generated with Golds v0.6.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds .