ctx

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package ctx provides the core request context and trace types for the game server framework. This framework exclusively supports int64 RoleID for user identification and routing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Value

func Value[K comparable, V any](c IContext, key K) (V, bool)

Value returns the value stored in c for the given key, together with a boolean that indicates whether a value of the expected type V was found. It is a typed wrapper around the standard context.Context.Value lookup and avoids callers having to perform manual type assertions. Written by Claude Code claude-opus-4-6.

Types

type BaseCtx added in v0.1.0

type BaseCtx[TraceData any, TP TracePtr[TraceData]] struct {
	Context   context.Context
	Req       proto.Message
	Resp      proto.Message   // RPC response, acquired from pool together with Req; nil for events
	OtherResp []proto.Message // additional messages appended to the reply alongside Resp

	// for rpc reply
	NatsMsg *nats.Msg
	TD      TraceData
}

BaseCtx is the generic, pool-friendly request context used by every handler in the framework. TraceData holds the per-request trace payload (IntTrace) and TP is a pointer to TraceData that satisfies the Trace interface (int64 RoleID only). Fields are kept public so that service infrastructure can populate them directly; callers should treat the struct as mutable only during handler dispatch. Written by Claude Code claude-opus-4-6.

func (*BaseCtx[TraceData, TP]) Deadline added in v0.1.0

func (c *BaseCtx[TraceData, TP]) Deadline() (deadline time.Time, ok bool)

Deadline delegates to the embedded context.Context. Returns zero time and false when no inner context has been set. Written by Claude Code claude-opus-4-6.

func (*BaseCtx[TraceData, TP]) Debug added in v0.1.0

func (c *BaseCtx[TraceData, TP]) Debug() *logger.Event

Debug returns a DEBUG-level zerolog Event annotated with the context's trace fields. Written by Claude Code claude-opus-4-6.

func (*BaseCtx[TraceData, TP]) Disabled added in v0.1.0

func (c *BaseCtx[TraceData, TP]) Disabled() *logger.Event

Disabled always returns nil, satisfying the ILogger interface for contexts where logging is intentionally suppressed. Written by Claude Code claude-opus-4-6.

func (*BaseCtx[TraceData, TP]) Done added in v0.1.0

func (c *BaseCtx[TraceData, TP]) Done() <-chan struct{}

Done delegates to the embedded context.Context. Returns nil when no inner context has been set, which signals that the context will never be cancelled. Written by Claude Code claude-opus-4-6.

func (*BaseCtx[TraceData, TP]) Err added in v0.1.0

func (c *BaseCtx[TraceData, TP]) Err() error

Err delegates to the embedded context.Context. Returns nil when no inner context has been set. Written by Claude Code claude-opus-4-6.

func (*BaseCtx[TraceData, TP]) Error added in v0.1.0

func (c *BaseCtx[TraceData, TP]) Error() *logger.Event

Error returns an ERROR-level zerolog Event annotated with the context's trace fields. Written by Claude Code claude-opus-4-6.

func (*BaseCtx[TraceData, TP]) Fatal added in v0.1.0

func (c *BaseCtx[TraceData, TP]) Fatal() *logger.Event

Fatal returns a FATAL-level zerolog Event annotated with the context's trace fields. Written by Claude Code claude-opus-4-6.

func (*BaseCtx[TraceData, TP]) GetTrace added in v0.1.0

func (c *BaseCtx[TraceData, TP]) GetTrace() Trace

GetTrace returns the Trace associated with this context by converting the embedded TraceData value to the TP pointer type. The returned Trace is never nil. Written by Claude Code claude-opus-4-6.

func (*BaseCtx[TraceData, TP]) Info added in v0.1.0

func (c *BaseCtx[TraceData, TP]) Info() *logger.Event

Info returns an INFO-level zerolog Event annotated with the context's trace fields. Written by Claude Code claude-opus-4-6.

func (*BaseCtx[TraceData, TP]) NoLevel added in v0.1.0

func (c *BaseCtx[TraceData, TP]) NoLevel() *logger.Event

NoLevel returns a zerolog Event with no severity level, annotated with trace fields. Written by Claude Code claude-opus-4-6.

func (*BaseCtx[TraceData, TP]) Panic added in v0.1.0

func (c *BaseCtx[TraceData, TP]) Panic() *logger.Event

Panic returns a PANIC-level zerolog Event annotated with the context's trace fields. Written by Claude Code claude-opus-4-6.

func (*BaseCtx[TraceData, TP]) Reset added in v0.1.0

func (c *BaseCtx[TraceData, TP]) Reset()

Reset returns the context to a clean state suitable for reuse from an object pool. It reinstates a fresh background context, empties the response slice while retaining its allocated capacity, clears the NATS reply message, and delegates Reset to the embedded trace data via the TP pointer. Written by Claude Code claude-opus-4-6.

func (*BaseCtx[TraceData, TP]) SetValue added in v0.1.0

func (c *BaseCtx[TraceData, TP]) SetValue(key any, value any)

SetValue stores a key-value pair in the context by wrapping the current inner context with context.WithValue. If no inner context exists one is created from context.Background. Written by Claude Code claude-opus-4-6.

func (*BaseCtx[TraceData, TP]) Trace added in v0.1.0

func (c *BaseCtx[TraceData, TP]) Trace() *logger.Event

Trace returns a TRACE-level zerolog Event annotated with the context's trace fields. Written by Claude Code claude-opus-4-6.

func (*BaseCtx[TraceData, TP]) Value added in v0.1.0

func (c *BaseCtx[TraceData, TP]) Value(key any) any

Value implements context.Context and returns the value associated with key from the embedded context chain. Returns nil when no inner context has been set. Written by Claude Code claude-opus-4-6.

func (*BaseCtx[TraceData, TP]) Warn added in v0.1.0

func (c *BaseCtx[TraceData, TP]) Warn() *logger.Event

Warn returns a WARN-level zerolog Event annotated with the context's trace fields. Written by Claude Code claude-opus-4-6.

func (*BaseCtx[TraceData, TP]) WithLevel added in v0.1.0

func (c *BaseCtx[TraceData, TP]) WithLevel(level logger.Level) *logger.Event

WithLevel returns a zerolog Event at the specified level, annotated with trace fields. Written by Claude Code claude-opus-4-6.

type IContext

type IContext interface {
	context.Context
	define.Clear
	SetValue(any, any)
	GetTrace() Trace
	logger.ILogger
}

IContext is the primary context interface used throughout the framework. It composes the standard context.Context, the Clear (Reset) contract for object-pool reuse, mutable key-value storage via SetValue, access to the distributed trace via GetTrace, and structured logging via logger.ILogger. Written by Claude Code claude-opus-4-6.

type IContextPtr

type IContextPtr[T any] interface {
	IContext
	*T
}

IContextPtr pairs IContext with a pointer-to-T constraint, enabling generic code to work with concrete context types without losing the ability to obtain a typed pointer. Written by Claude Code claude-opus-4-6.

type Int64TraceCtx

type Int64TraceCtx = BaseCtx[IntTrace, *IntTrace]

Int64TraceCtx is the canonical context type for this framework. Only int64 RoleID is supported for routing.

type IntTrace

type IntTrace struct {
	basepb.IntTrace
	// contains filtered or unexported fields
}

IntTrace extends the protobuf-generated basepb.IntTrace with framework-level behaviour: routing by int64 RoleId, automatic trace-ID generation on first marshal, and zerolog field attachment for structured logging. It is the only trace type supported by this framework. Written by Claude Code claude-opus-4-6.

func (*IntTrace) GetRoleID added in v0.1.39

func (i *IntTrace) GetRoleID() int64

GetRoleID returns the int64 RoleId used for consistent per-entity message routing. Written by Claude Code claude-opus-4-6.

func (*IntTrace) GetServerIdAndType

func (i *IntTrace) GetServerIdAndType() (int64, string)

GetServerIdAndType returns the server ID and server type recorded in this trace, identifying the service that originated the request. Written by Claude Code claude-opus-4-6.

func (*IntTrace) Reset added in v0.1.39

func (i *IntTrace) Reset()

Reset clears protobuf fields, preparing the object for pool reuse.

func (*IntTrace) SetRoleID added in v0.1.39

func (i *IntTrace) SetRoleID(roleID int64)

SetRoleID overrides the RoleId on a pooled context acquired inside taskFunc.

func (*IntTrace) SetServerIdAndType

func (i *IntTrace) SetServerIdAndType(serverId int64, serverType string)

SetServerIdAndType records the originating server's numeric ID and type name in the trace so that downstream services can identify the caller. Written by Claude Code claude-opus-4-6.

func (*IntTrace) TraceLogField

func (i *IntTrace) TraceLogField(e *logger.Event) *logger.Event

TraceLogField attaches trace-specific fields (traceId, fromServerId, fromServerType, roleId) to the zerolog Event, enabling consistent structured logging across services. Written by Claude Code claude-opus-4-6.

func (*IntTrace) TraceMarshalAppend

func (i *IntTrace) TraceMarshalAppend(b []byte) ([]byte, error)

TraceMarshalAppend serialises the trace and appends the bytes to b, returning the extended slice. A TraceId is generated on the first call if one has not been set. Written by Claude Code claude-opus-4-6.

func (*IntTrace) TraceMarshalFrom

func (i *IntTrace) TraceMarshalFrom(b []byte) error

TraceMarshalFrom deserialises b into the trace, restoring all protobuf fields. If the decoded message contains a non-empty TraceId it is also copied into the internal fixed-size id field for fast comparison. Written by Claude Code claude-opus-4-6.

func (*IntTrace) TraceMarshalSize

func (i *IntTrace) TraceMarshalSize() int

TraceMarshalSize returns the number of bytes required to serialise this trace. If no TraceId has been set, one is generated and stored before computing the size. Written by Claude Code claude-opus-4-6.

type Trace

type Trace interface {
	// GetRoleID returns the int64 role identifier used for consistent message routing.
	// The same RoleID always routes to the same worker, enabling per-entity ordering without locks.
	// A zero value means the message has no ordering requirement.
	GetRoleID() int64
	// SetRoleID overrides the RoleID on a pooled context acquired inside taskFunc.
	// Used when Data is nil and a fresh context must carry the caller's RoleID.
	SetRoleID(int64)

	TraceMarshalSize() int
	// TraceMarshalAppend marshal the object to byte slice
	TraceMarshalAppend([]byte) ([]byte, error)
	// TraceMarshalFrom unmarshal the object from byte slice
	TraceMarshalFrom([]byte) error

	TraceLogField(*logger.Event) *logger.Event

	GetServerIdAndType() (int64, string)

	SetServerIdAndType(int64, string)

	Reset()
}

Trace is the per-request trace contract carried through the distributed system. All routing is performed by int64 RoleID — the framework only supports int64 RoleID. Written by Claude Code claude-opus-4-6.

type TracePtr

type TracePtr[T any] interface {
	Trace
	*T
}

TracePtr combines the Trace interface with a pointer-to-T constraint so that generic functions can treat *T as a Trace without additional type assertions. Written by Claude Code claude-opus-4-6.

Jump to

Keyboard shortcuts

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