MCPcopy
hub / github.com/mudler/LocalAI / ExposeNodeHeader

Function ExposeNodeHeader

core/http/middleware/node_header.go:96–127  ·  view source on GitHub ↗

ExposeNodeHeader installs a per-request response writer wrapper that stamps the X-LocalAI-Node header from the per-request holder published by the distributed router on the first write. Off by default; opted in via --expose-node-header / LOCALAI_EXPOSE_NODE_HEADER. Attribution is per-request correc

(appCfg *config.ApplicationConfig)

Source from the content-addressed store, hash-verified

94// shared loader state, no overwriting across concurrent requests for the
95// same model on multiple replicas.
96func ExposeNodeHeader(appCfg *config.ApplicationConfig) echo.MiddlewareFunc {
97 return func(next echo.HandlerFunc) echo.HandlerFunc {
98 return func(c echo.Context) error {
99 if appCfg == nil || !appCfg.ExposeNodeHeader {
100 return next(c)
101 }
102
103 // One holder per request. The pointer is captured both in
104 // the wrapper closure (read side) and in the request
105 // context (write side, accessed by the router via
106 // distributedhdr.Stamp). Both sides point at the same
107 // atomic slot.
108 holder := distributedhdr.NewHolder()
109
110 req := c.Request()
111 c.SetRequest(req.WithContext(distributedhdr.WithHolder(req.Context(), holder)))
112
113 orig := c.Response().Writer
114 wrapper := &nodeHeaderWriter{
115 ResponseWriter: orig,
116 resolve: func() string {
117 return distributedhdr.Load(holder)
118 },
119 }
120 c.Response().Writer = wrapper
121 defer func() {
122 c.Response().Writer = orig
123 }()
124 return next(c)
125 }
126 }
127}

Callers 8

RegisterJINARoutesFunction · 0.92
RegisterOllamaRoutesFunction · 0.92
RegisterAnthropicRoutesFunction · 0.92
RegisterLocalAIRoutesFunction · 0.92
RegisterOpenAIRoutesFunction · 0.92

Calls 6

NewHolderFunction · 0.92
WithHolderFunction · 0.92
LoadFunction · 0.92
RequestMethod · 0.65
ContextMethod · 0.65
nextFunction · 0.50

Tested by

no test coverage detected