A Go library of concurrency utilities.
- Promise and Future
- SingleFlight
Run the go get command to install Meridian:
go get github.com/AndreySenov/meridianUse the -u flag to update Meridian to the latest version:
go get -u github.com/AndreySenov/meridianA Promise and a Future are companion constructs used to handle results of asynchronous tasks.
The Promise produces the result at most once. The Future provides a read-only interface to consume the result.
Any number of Future handles can observe the outcome of the same Promise.
Usage example:
func GetProfile(ctx context.Context, id string) (*Profile, error) {
p := meridian.NewPromise[*Profile]()
go func() {
r, err := fetchProfileFromDB(id)
p.Complete(r, err) // or p.Resolve(r) / p.Reject(err)
}()
f := p.Future()
return f.Get(ctx) // blocks until completed or ctx is done
}SingleFlight is an alternative to golang.org/x/sync/singleflight with generic keys and values.
While a task for a key is in flight, every Do call with that key joins it and
receives the same result instead of running its own task:
var flights meridian.SingleFlight[string, *Profile]
func LoadProfile(ctx context.Context, id string) (*Profile, error) {
future := flights.Do(id, func() (*Profile, error) {
return fetchProfileFromDB(id) // runs once per key, no matter how many callers
})
return future.Get(ctx) // each caller waits with its own context
}See the package documentation for the full API reference.
Meridian is licensed under the Apache License, Version 2.0. See NOTICE and LICENSE for details.