|
| 1 | +// Package distributedhdr carries a per-request "which worker node served |
| 2 | +// me" record from the distributed router (core/services/nodes) up to the |
| 3 | +// HTTP response writer wrapper (core/http/middleware). It is the bridge |
| 4 | +// for the X-LocalAI-Node response header without coupling those two |
| 5 | +// packages directly or going through any shared mutable state. |
| 6 | +// |
| 7 | +// Why its own package: both core/http/middleware and core/services/nodes |
| 8 | +// already import pkg/model, and the natural homes for this key would |
| 9 | +// create an import cycle if either side hosted the helper. Putting the |
| 10 | +// key and the tiny helpers here keeps it neutral - producer and consumer |
| 11 | +// import a leaf package, not each other. |
| 12 | +package distributedhdr |
| 13 | + |
| 14 | +import ( |
| 15 | + "context" |
| 16 | + "sync/atomic" |
| 17 | +) |
| 18 | + |
| 19 | +// ctxKey is an unexported context-key type so external packages cannot |
| 20 | +// collide with our key by accident. |
| 21 | +type ctxKey struct{} |
| 22 | + |
| 23 | +// holderKey is the singleton context key used by WithHolder / Holder / |
| 24 | +// Stamp. Unexported on purpose: producers and consumers must go through |
| 25 | +// the helpers below so the storage representation stays an |
| 26 | +// implementation detail. |
| 27 | +var holderKey = ctxKey{} |
| 28 | + |
| 29 | +// NewHolder returns an empty holder ready to be attached to a request |
| 30 | +// context via WithHolder. The middleware creates one per HTTP request; |
| 31 | +// the router fills it; the response writer wrapper reads it on the |
| 32 | +// first byte. The atomic.Value gives us race-clean publish across the |
| 33 | +// goroutines that may write (router) and read (response writer |
| 34 | +// wrapper) the slot. |
| 35 | +func NewHolder() *atomic.Value { |
| 36 | + return &atomic.Value{} |
| 37 | +} |
| 38 | + |
| 39 | +// WithHolder returns a derived context that carries the provided holder. |
| 40 | +// The middleware calls this on the per-request context BEFORE the |
| 41 | +// downstream handler chain runs, so any goroutine that inherits this |
| 42 | +// context (notably the SmartRouter / ModelRouterAdapter) can find the |
| 43 | +// holder via Stamp. |
| 44 | +func WithHolder(ctx context.Context, h *atomic.Value) context.Context { |
| 45 | + if h == nil { |
| 46 | + return ctx |
| 47 | + } |
| 48 | + return context.WithValue(ctx, holderKey, h) |
| 49 | +} |
| 50 | + |
| 51 | +// Holder returns the holder attached to ctx, or nil if none was set. |
| 52 | +// Callers that just want to publish should prefer Stamp; Holder is |
| 53 | +// useful for tests and for propagating the holder across derived |
| 54 | +// contexts (see Inherit). |
| 55 | +func Holder(ctx context.Context) *atomic.Value { |
| 56 | + if ctx == nil { |
| 57 | + return nil |
| 58 | + } |
| 59 | + h, _ := ctx.Value(holderKey).(*atomic.Value) |
| 60 | + return h |
| 61 | +} |
| 62 | + |
| 63 | +// Stamp records the picked worker node ID into the holder attached to |
| 64 | +// ctx. No-op when: |
| 65 | +// - ctx is nil |
| 66 | +// - no holder is attached (e.g. the X-LocalAI-Node feature is off, so |
| 67 | +// the middleware never ran) |
| 68 | +// - nodeID is empty |
| 69 | +// |
| 70 | +// Stamp is safe to call from any goroutine. Subsequent reads via Load |
| 71 | +// observe the most recent stamp. |
| 72 | +func Stamp(ctx context.Context, nodeID string) { |
| 73 | + if nodeID == "" { |
| 74 | + return |
| 75 | + } |
| 76 | + h := Holder(ctx) |
| 77 | + if h == nil { |
| 78 | + return |
| 79 | + } |
| 80 | + h.Store(nodeID) |
| 81 | +} |
| 82 | + |
| 83 | +// Load returns the node ID most recently stamped into the holder, or |
| 84 | +// "" if nothing has been stamped. Intended for the response writer |
| 85 | +// wrapper's first-byte hook. |
| 86 | +func Load(h *atomic.Value) string { |
| 87 | + if h == nil { |
| 88 | + return "" |
| 89 | + } |
| 90 | + v, _ := h.Load().(string) |
| 91 | + return v |
| 92 | +} |
| 93 | + |
| 94 | +// Inherit copies the holder reference from src into dst when present. |
| 95 | +// Used at request-handling seams where the request context is replaced |
| 96 | +// with a fresh context derived from the long-lived application context |
| 97 | +// (so the cancel semantics of the original context are preserved while |
| 98 | +// also allowing the load path to outlive the HTTP transport). The |
| 99 | +// holder is a pointer, so both contexts point at the same slot; a |
| 100 | +// router stamp via Stamp(dst, ...) is observed by the middleware that |
| 101 | +// reads through src's holder. |
| 102 | +func Inherit(dst, src context.Context) context.Context { |
| 103 | + h := Holder(src) |
| 104 | + if h == nil { |
| 105 | + return dst |
| 106 | + } |
| 107 | + return WithHolder(dst, h) |
| 108 | +} |
0 commit comments