Path: blob/dev/pkg/protocols/common/protocolstate/context.go
2072 views
package protocolstate12import (3"context"45"github.com/rs/xid"6)78// contextKey is a type for context keys9type ContextKey string1011type ExecutionContext struct {12ExecutionID string13}1415// executionIDKey is the key used to store execution ID in context16const executionIDKey ContextKey = "execution_id"1718// WithExecutionID adds an execution ID to the context19func WithExecutionID(ctx context.Context, executionContext *ExecutionContext) context.Context {20return context.WithValue(ctx, executionIDKey, executionContext)21}2223// HasExecutionID checks if the context has an execution ID24func HasExecutionContext(ctx context.Context) bool {25_, ok := ctx.Value(executionIDKey).(*ExecutionContext)26return ok27}2829// GetExecutionID retrieves the execution ID from the context30// Returns empty string if no execution ID is set31func GetExecutionContext(ctx context.Context) *ExecutionContext {32if id, ok := ctx.Value(executionIDKey).(*ExecutionContext); ok {33return id34}35return nil36}3738// WithAutoExecutionContext creates a new context with an automatically generated execution ID39// If the input context already has an execution ID, it will be preserved40func WithAutoExecutionContext(ctx context.Context) context.Context {41if HasExecutionContext(ctx) {42return ctx43}44return WithExecutionID(ctx, &ExecutionContext{ExecutionID: xid.New().String()})45}464748