github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/internal/wait/wait.go (about) 1 package wait 2 3 import ( 4 "context" 5 "time" 6 7 "github.com/ydb-platform/ydb-go-sdk/v3/internal/backoff" 8 "github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors" 9 ) 10 11 // waitBackoff is a helper function that waits for i-th Backoff b or ctx 12 // expiration. 13 // It returns non-nil error if and only if deadline expiration branch wins. 14 func waitBackoff(ctx context.Context, b backoff.Backoff, i int) error { 15 t := time.NewTimer(b.Delay(i)) 16 defer t.Stop() 17 18 select { 19 case <-t.C: 20 return nil 21 case <-ctx.Done(): 22 if err := ctx.Err(); err != nil { 23 return xerrors.WithStackTrace(err) 24 } 25 26 return nil 27 } 28 } 29 30 func Wait(ctx context.Context, fastBackoff, slowBackoff backoff.Backoff, t backoff.Type, i int) error { 31 var b backoff.Backoff 32 switch t { 33 case backoff.TypeNoBackoff: 34 if err := ctx.Err(); err != nil { 35 return xerrors.WithStackTrace(err) 36 } 37 38 return nil 39 case backoff.TypeFast: 40 if fastBackoff == nil { 41 fastBackoff = backoff.Fast 42 } 43 b = fastBackoff 44 case backoff.TypeSlow: 45 if slowBackoff == nil { 46 slowBackoff = backoff.Slow 47 } 48 b = slowBackoff 49 } 50 51 return waitBackoff(ctx, b, i) 52 }