AsyncWork schedules and throttles function execution in golang
var wg sync.WaitGroup
wg.Add(2)
worker, err := asyncwork.New()
if err != nil {
panic(err)
}
worker.Start()
defer worker.Stop()
worker.PostJob(func(ctx context.Context) error {
// Long operation 1
log.Printf("Operation1")
wg.Done()
return nil
})
worker.PostJob(func(ctx context.Context) error {
// Long operation 2
log.Printf("Operation2")
wg.Done()
return nil
})
log.Printf("Pending: %v", worker.Len())
wg.Wait()
log.Printf("Pending: %v", worker.Len())
Pending: 2
Operation1
Operation2
Pending: 0
When throttling, only the last call is executed within a timespan
var wg sync.WaitGroup
wg.Add(2)
worker, err := asyncwork.New()
if err != nil {
panic(err)
}
worker.Start()
defer worker.Stop()
worker.PostJob(func(ctx context.Context) error {
// Long operation 1
log.Printf("Operation1")
wg.Done()
return nil
})
worker.PostThrottledJob(func(ctx context.Context) error {
// Long operation 2 is not executed due to throttle
log.Printf("Operation2")
return nil
}, 500*time.Millisecond)
worker.PostThrottledJob(func(ctx context.Context) error {
// Long operation 3
log.Printf("Operation3")
return nil
}, 500*time.Millisecond)
<-time.After(600 * time.Millisecond)
worker.PostThrottledJob(func(ctx context.Context) error {
// Long operation 4
log.Printf("Operation4")
wg.Done()
return nil
}, 500*time.Millisecond)
wg.Wait()
log.Printf("Pending: %v", worker.Len())
Operation1
Operation3
Operation4
Pending: 0