Note: entirely WIP as an exploratory project
go-async is a C# inspired package to facilitate async code-flows via Tasks (or Futures) for cpu/io bound workloads. Not intended for use in async http calls via polling of callback endpoints though I don't see why not.
This package follows a C#-eqsque naming scheme. Methods that do async work (returning a Task) have *Async as a suffix.
The entry point for the go-async package is the CreateTask and CeateAndRunAsync methods. The only enforced contract for this package is that the workload should provide a contextSource (a la a c#-style CTS) and respect cancellation from the source.
Upon reaching a cancellation, the worker should throw an error that unwraps to context.Canceled though this is not required. The package will do a best-effort guess otherwise.
i.e:
workLoadFn := func(ctx context.Context) (T, error) {
timer := time.NewTimer(60 * time.Second)
defer func() {
timer.Stop()
}()
select {
case <-ctx.Done():
//pretend to do cleanup before cancelling
time.Sleep(10 * time.Second)
return nil, RunnerExitedFromCancellationError{"worker ack'ed cancellation"}
case <-timer.C:
return "done work", nil
}
}
# Create and run the task. Alternatively just call CreateAndRunAsync
task := CreateTask(func() context.Context { return context.Backgroud()}, dummyWorkload())
task.RunAsync()
# do some other work
result, err := task.Result()
# or request a cancellation and forget the task:
# task.CancelAsync()
# or cancel and wait for cancellation
# task.Cancel()