mirror of
https://github.com/lukaszraczylo/go-telegram.git
synced 2026-07-10 21:23:12 +00:00
Initial release of go-telegram
A fully-generated, strongly-typed Go client for the Telegram Bot API. * 176 methods + 301 types generated from Bot API v10.0 * 1408 auto-generated tests (8 scenarios per method) * Typed unions throughout — no 'any' in the public surface * Pluggable HTTP transport and JSON codec (default goccy/go-json) * Built-in retry middleware honouring Telegram's retry_after * Generic dispatcher with filters and conversation handlers * Self-verifying codegen pipeline (regen → audit → emit → run tests) * 14 example bots covering common patterns
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
package transport
|
||||
|
||||
import (
|
||||
"math"
|
||||
"math/rand/v2"
|
||||
"time"
|
||||
)
|
||||
|
||||
// BackoffStrategy returns the duration to wait before the next attempt
|
||||
// after `attempt` consecutive failures (1-based). Implementations must
|
||||
// be safe to call from a single goroutine.
|
||||
type BackoffStrategy interface {
|
||||
NextDelay(attempt int) time.Duration
|
||||
}
|
||||
|
||||
// ExponentialBackoff implements capped exponential back-off with jitter.
|
||||
// Defaults: Base=500ms, Max=30s, Factor=2.0, Jitter=0.2.
|
||||
type ExponentialBackoff struct {
|
||||
Base time.Duration
|
||||
Max time.Duration
|
||||
Factor float64
|
||||
Jitter float64 // 0..1; fraction of computed delay added/subtracted at random
|
||||
}
|
||||
|
||||
// DefaultBackoff returns an ExponentialBackoff with library defaults.
|
||||
func DefaultBackoff() *ExponentialBackoff {
|
||||
return &ExponentialBackoff{
|
||||
Base: 500 * time.Millisecond,
|
||||
Max: 30 * time.Second,
|
||||
Factor: 2.0,
|
||||
Jitter: 0.2,
|
||||
}
|
||||
}
|
||||
|
||||
// NextDelay implements BackoffStrategy.
|
||||
func (b *ExponentialBackoff) NextDelay(attempt int) time.Duration {
|
||||
if attempt < 1 {
|
||||
attempt = 1
|
||||
}
|
||||
d := float64(b.Base) * math.Pow(b.Factor, float64(attempt-1))
|
||||
if b.Jitter > 0 {
|
||||
d *= 1 + (rand.Float64()*2-1)*b.Jitter
|
||||
}
|
||||
if d > float64(b.Max) {
|
||||
d = float64(b.Max)
|
||||
}
|
||||
if d < 0 {
|
||||
d = 0
|
||||
}
|
||||
return time.Duration(d)
|
||||
}
|
||||
Reference in New Issue
Block a user