package throttling import ( "context" "time" ) type AsyncThrottler[T any] struct { throttler Throttler[T] ctx context.Context events chan *T ticker *time.Ticker } func NewAsyncThrottler[T any](ctx context.Context, notifier func(t *T), minDelay time.Duration, pollInterval time.Duration) *AsyncThrottler[T] { throttler := AsyncThrottler[T]{ throttler: NewThrottler[T](notifier, minDelay), ctx: ctx, events: make(chan *T), ticker: time.NewTicker(pollInterval), } go func() { defer throttler.ticker.Stop() for { select { case <-ctx.Done(): return case <-throttler.ticker.C: throttler.throttler.Ping() case event := <-throttler.events: throttler.throttler.Notify(event) } } }() return &throttler } func (throttler *AsyncThrottler[T]) Notify(value *T) { throttler.events <- value }