vlang.io | Docs | Changelog | Contributing
A library to merge existing V contexts
Have you ever faced the situation where you have to merge multiple existing contexts? If not, then you might, eventually.
For example, we can face the situation where we are building an application using a library that gives us a global context. This context expires once the application is stopped.
Meanwhile, we are exposing a service like this:
fn (f Foo) get(ctx context.Context, bar Bar) ?Baz {
. . .
}
Here, we receive another context provided by the service.
Then, in the get
implementation, we want for example to query a database
and we must provide a context for that.
Ideally, we would like to provide a merged context that would expire either:
This is exactly the purpose of this library.
In our case, we can now merge the two contexts in a single one like this:
ctx, cancel := onecontext.merge(ctx1, ctx2)
This returns a merged context that we can now propagate.
Via vpm
$ v install ulises-jeremias.onecontext
Via vpkg
$ vpkg get https://github.com/ulises-jeremias/onecontext
Done. Installation completed.
To test the module, just type the following command:
$ ./bin/test # execute `./bin/test -h` to know more about the test command
const canceled = error('canceled context')
canceled is the error returned when the cancel function is called on a merged context
fn merge(ctx context.Context, ctxs ...context.Context) (context.Context, context.CancelFn)
merge allows to merge multiple contexts it returns the merged context
fn (octx OneContext) deadline() ?time.Time
fn (octx OneContext) done() chan int
fn (mut octx OneContext) err() IError
fn (octx OneContext) value(key context.Key) ?context.Any
fn (mut octx OneContext) run()
fn (octx OneContext) str() string
fn (mut octx OneContext) cancel(err IError)
fn (mut octx OneContext) run_two_contexts(mut ctx1 context.Context, mut ctx2 context.Context)
fn (mut octx OneContext) run_multiple_contexts(mut ctx context.Context)