resource_server

package
v0.55.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 10, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConstructChildResource

func ConstructChildResource[T any](
	ctx context.Context,
	buildFn func(subCtx context.Context) (mux srpc.Invoker, result T, releaseFn func(), err error),
) (T, uint32, error)

ConstructChildResource creates a sub-resource with a managed context.

Extracts the ResourceClientContext from ctx, creates a sub-context derived from the client session (outlives the RPC call), calls buildFn with that context, and registers the resulting mux as a child resource.

On buildFn error the sub-context is canceled automatically. On success the sub-context lives until the resource is released or the client disconnects. The releaseFn returned by buildFn runs before the sub-context is canceled (user cleanup has a live context on explicit release; on client disconnect the client context is already dead which is accepted as abnormal teardown).

func NewResourceMux

func NewResourceMux(register ...func(srpc.Mux) error) srpc.Mux

NewResourceMux creates a new srpc.Mux and registers services with it. Panics if any registration function returns an error.

func WithResourceClientContext

func WithResourceClientContext(ctx context.Context, msc ResourceClientContext) context.Context

WithResourceClientContext attaches a ResourceClientContext to a Context.

Types

type RemoteResourceClient

type RemoteResourceClient struct {
	// contains filtered or unexported fields
}

RemoteResourceClient holds state for an attached client.

func (*RemoteResourceClient) AddAttachedResource

func (c *RemoteResourceClient) AddAttachedResource(
	id uint32,
	label string,
	cancel context.CancelFunc,
	srpcClient srpc.Client,
	releaseFn func(),
) error

AddAttachedResource registers a client-provided attached resource. Returns an error if the client has been released.

func (*RemoteResourceClient) AddResource

func (c *RemoteResourceClient) AddResource(mux srpc.Invoker, releaseFn func()) (uint32, error)

AddResource adds a new resource with the given mux and returns its unique ID. The releaseFn callback will be called when the resource is released (can be nil). Returns an error if the client has already been released.

Note: Server-side handlers may send the same resource ID to the client multiple times (out-of-band from this API). The client uses reference counting to track when all references to a resource ID have been released.

func (*RemoteResourceClient) AddResourceValue

func (c *RemoteResourceClient) AddResourceValue(mux srpc.Invoker, value any, releaseFn func()) (uint32, error)

AddResourceValue adds a new resource with an optional typed value.

func (*RemoteResourceClient) Context

func (c *RemoteResourceClient) Context() context.Context

Context returns the client session context. This context lives for the duration of the client session and is canceled when the client is released. Use this for sub-resources that need to outlive individual RPC calls.

func (*RemoteResourceClient) GetAttachedResource

func (c *RemoteResourceClient) GetAttachedResource(id uint32) (srpc.Client, error)

GetAttachedResource returns the srpc.Client for an attached resource.

func (*RemoteResourceClient) GetResourceValue

func (c *RemoteResourceClient) GetResourceValue(resourceID uint32) (any, error)

GetResourceValue returns the typed resource value for a tracked resource.

func (*RemoteResourceClient) ReleaseResource

func (c *RemoteResourceClient) ReleaseResource(resourceID uint32) bool

ReleaseResource releases a resource that was previously added. Calls the releaseFn callback if it was provided to AddResource. Sends a ResourceReleasedResponse message to the client. Returns true if the resource was found and released, false if not found. Safe to call even if the resource has already been released.

func (*RemoteResourceClient) RemoveAttachedResource

func (c *RemoteResourceClient) RemoveAttachedResource(id uint32)

RemoveAttachedResource removes an attached resource and cancels its context.

type ResourceClientContext

type ResourceClientContext interface {
	// Context returns the resource owner lifetime context.
	Context() context.Context
	// AddResource adds a child resource with a release callback.
	AddResource(mux srpc.Invoker, releaseFn func()) (uint32, error)
	// AddResourceValue adds a child resource with an optional typed value.
	AddResourceValue(mux srpc.Invoker, value any, releaseFn func()) (uint32, error)
	// ReleaseResource releases a child resource owned by this context.
	ReleaseResource(resourceID uint32) bool
	// GetResourceValue returns the typed value for a resource.
	GetResourceValue(resourceID uint32) (any, error)
	// GetAttachedResource returns a raw attached SRPC client.
	GetAttachedResource(id uint32) (srpc.Client, error)
}

ResourceClientContext is the value attached to a Context containing information about the Resource RPC request.

func GetResourceClientContext

func GetResourceClientContext(ctx context.Context) ResourceClientContext

GetResourceClientContext returns the ResourceClientContext from the Context or nil if unset.

func MustGetResourceClientContext

func MustGetResourceClientContext(ctx context.Context) (ResourceClientContext, error)

MustGetResourceClientContext returns the ResourceClientContext from the Context or an error if unset.

type ResourceServer

type ResourceServer struct {
	// contains filtered or unexported fields
}

ResourceServer provides the Resources RPC API.

The server and client track Resource handles via integer IDs per client. Each resource has a unique ID, but the server may send the same resource ID to a client multiple times (e.g., when creating multiple references). The client uses reference counting to track when all references are released.

func NewResourceServer

func NewResourceServer(rootResourceMux srpc.Invoker) *ResourceServer

NewResourceServer constructs a new ResourceServer.

func (*ResourceServer) Register

func (s *ResourceServer) Register(mux srpc.Mux) error

Register registers the server with the mux.

func (*ResourceServer) ResourceAttach

ResourceAttach allows a client to provide resources that server-side RPC handlers can invoke via getAttachedRef(id). One stream = one yamux session = N resources. Session-only Init/Ack, then Add/AddAck per resource.

func (*ResourceServer) ResourceClient

ResourceClient starts an instance of a client for the ResourceService, yielding a new client ID. The client can use that ID for future RPCs accessing the Resource tree. When the streaming RPC ends, all resources owned by the client will be released.

func (*ResourceServer) ResourceRefRelease

ResourceRefRelease releases a client's resource.

func (*ResourceServer) ResourceRpc

ResourceRpc is a rpc request for an open resource handle. Exposes service(s) depending on the resource type. Component ID: resource_id from ResourceClient call.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL