Skip to content

Repository files navigation

Meridian

Build Status Latest Release License Go Reference

A Go library of concurrency utilities.

Features

  • Promise and Future
  • SingleFlight

Installation

Run the go get command to install Meridian:

go get github.com/AndreySenov/meridian

Use the -u flag to update Meridian to the latest version:

go get -u github.com/AndreySenov/meridian

Promise and Future

A 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

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
}

Documentation

See the package documentation for the full API reference.

License

Meridian is licensed under the Apache License, Version 2.0. See NOTICE and LICENSE for details.

About

A Go library of concurrency utilities

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages