Name

SPVM::Go::Context - Context propagation, cancellation, and value sharing (Go-style)

Description

Go::Context class in SPVM provides a way to carry deadlines, cancellation signals, and other request-scoped values across API boundaries and between goroutines.

It is based on the implementation of context.Context in the Go programming language.

Usage

use Go::Context;

# Create a background context
my $ctx = Go::Context->background;

# Create a context with 5 seconds timeout
my $derived = $ctx->with_timeout_sec(5.0);
my $ctx_with_timeout = $derived->context;
my $cancel = $derived->cancel_cb;

# Done channel
my $done = $ctx_with_timeout->done;

# Manually cancel when finished
$cancel->();

Class Methods

background

static method background : Go::Context ();

Returns a non-nil, empty Go::Context. It is never canceled, has no values, and has no deadline. It is typically used by the main function, initialization, and tests.

See context.Background.

todo

static method todo : Go::Context ();

Returns a non-nil, empty Go::Context. Code should use todo when it's unclear which Context to use or it is not yet available.

See context.TODO.

Instance Methods

without_cancel

method without_cancel : Go::Context ();

Returns a copy of this context that is not canceled when this context is canceled.

See context.WithoutCancel.

with_cancel

method with_cancel : Go::Context::Derived ();

Returns a Go::Context::Derived object containing a new context and a cancel function.

See context.WithCancel.

with_cancel_cause

method with_cancel_cause : Go::Context::Derived ($cause : string);

Similar to "with_cancel", but also sets a cause for the cancellation.

See context.WithCancelCause.

with_deadline

method with_deadline : Go::Context::Derived ($deadline : Go::Time);

Returns a derived context that is canceled when the $deadline expires or this context's done channel is closed.

See context.WithDeadline.

with_deadline_cause

method with_deadline_cause : Go::Context::Derived ($deadline : Go::Time, $cause : string);

Similar to "with_deadline", but also sets a cause when the $deadline expires.

See context.WithDeadlineCause.

with_timeout

method with_timeout : Go::Context::Derived ($timeout : Go::Duration_1l);

A wrapper for "with_deadline" that takes a duration $timeout instead of a specific time.

See context.WithTimeout.

with_timeout_sec

method with_timeout_sec : Go::Context::Derived ($timeout_sec : double);

A helper method that creates a timeout context from the specified number of seconds $timeout_sec (as a double).

with_value

method with_value : Go::Context ($key : object, $value : object);

Returns a copy of this context in which the value associated with $key is $value.

See context.WithValue.

deadline

method deadline : Go::Time ();

Returns the time when work done on behalf of this context should be canceled.

See Context.Deadline.

done

method done : Go::Channel ();

Returns a channel that's closed when work done on behalf of this context should be canceled.

See Context.Done.

err

method err : int ();

Returns an error code (basic type ID) explaining why this context was canceled.

See Context.Err.

cause

method cause : string ();

Returns a string explaining why this context was canceled.

See context.Cause.

value

method value : object ($key : object);

Returns the value associated with this context for $key, or undef if no value is associated with $key.

See Context.Value.

See Also

Author

Yuki Kimoto kimoto.yuki@gmail.com

Copyright & License

Copyright (c) 2026 Yuki Kimoto

MIT License