ctxkit

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package ctxkit carries request-scoped identity on a context.Context.

It exists so that a service, a repository, a background goroutine and a log line all read the same trace id and the same tenant from the same place. The keys are unexported: nothing outside this package can collide with them, and nothing can put a value of the wrong type under one.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Detach

func Detach(parent context.Context) context.Context

Detach returns a context that keeps the trace, tenant, actor and request id of parent but NOT its cancellation or deadline.

This is the only correct way to hand work to a goroutine that outlives the request that started it. Passing the request context directly cancels the background work the moment the client disconnects — which is how fire-and-forget background work silently stops half-done.

func RequestID

func RequestID(ctx context.Context) string

RequestID returns the per-request id, or "" when none was set.

func Trace

func Trace(ctx context.Context) string

Trace returns the correlation id, or "" when none was set.

func WithActor

func WithActor(ctx context.Context, a Actor) context.Context

WithActor returns a context carrying the acting principal.

func WithRequestID

func WithRequestID(ctx context.Context, id string) context.Context

WithRequestID returns a context carrying the per-request id.

func WithTenant

func WithTenant(ctx context.Context, t Tenant) context.Context

WithTenant returns a context scoped to one tenant.

func WithTrace

func WithTrace(ctx context.Context, traceID string) context.Context

WithTrace returns a context carrying a correlation id.

Types

type Actor

type Actor struct {
	UserID   uuid.UUID
	DeviceID string
	// Roles are opaque strings; guards interpret them.
	Roles []string
}

Actor is who made the request, inside the tenant.

func ActorOf

func ActorOf(ctx context.Context) Actor

ActorOf returns the acting principal, zero when none was set.

func (Actor) HasRole

func (a Actor) HasRole(role string) bool

HasRole reports whether the actor carries role.

type Tenant

type Tenant struct {
	AccountID uuid.UUID
	// Operator is true for a platform super-admin. It does not widen the
	// tenant scope on its own; a caller that wants cross-tenant reach asks for
	// it explicitly.
	Operator bool
}

Tenant is the ownership boundary every stored row is scoped by. A zero AccountID means "no tenant" — a platform task, a reconciler, a migration — and a repository MUST refuse to treat that as "all tenants".

func TenantOf

func TenantOf(ctx context.Context) Tenant

TenantOf returns the tenant, zero when none was set.

func (Tenant) Zero

func (t Tenant) Zero() bool

Zero reports whether t names no tenant.

Jump to

Keyboard shortcuts

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