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 = Go::Context->with_timeout_sec($ctx, 5.0);
my $ctx_with_timeout = $derived->ctx;
my $cancel = $derived->cancel;

# 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.

without_cancel

static method without_cancel : Go::Context ($parent_ctx : Go::Context);

Returns a copy of $parent_ctx that is not canceled when $parent_ctx is canceled.

See context.WithoutCancel.

with_cancel

static method with_cancel : Go::Context::Derived ($parent_ctx : Go::Context);

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

See context.WithCancel.

with_cancel_cause

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

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

See context.WithCancelCause.

with_deadline

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

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

See context.WithDeadline.

with_deadline_cause

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

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

See context.WithDeadlineCause.

with_timeout

static method with_timeout : Go::Context::Derived ($parent_ctx : Go::Context, $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

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

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

with_value

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

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

See context.WithValue.

Instance Methods

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