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)
| 94 | // shared loader state, no overwriting across concurrent requests for the |
| 95 | // same model on multiple replicas. |
| 96 | func 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 | } |
no test coverage detected