| 1 | package bot |
| 2 | |
| 3 | import "net/http" |
| 4 | |
| 5 | // SidecarHeader marks every request the live sidecar makes to its instance — |
| 6 | // the WebSocket dial and all REST calls — so a hibernation-aware router |
| 7 | // (activator.go) can tell sidecar traffic from member traffic. A co-spawned |
| 8 | // sidecar's connections flow through its own instance's router (the box pins |
| 9 | // its hosted names to loopback), and without the marker its keepalives would |
| 10 | // read as member activity: the instance could never idle, and after a sleep |
| 11 | // the reconnect loop would be a standing wake source. Marked requests are |
| 12 | // served while the instance is awake but stamp no activity, and never wake a |
| 13 | // sleeping one. |
| 14 | // |
| 15 | // The marker is deliberately spoofable-but-harmless: it only DOWNGRADES the |
| 16 | // sender's own requests (no activity credit; 503 while the instance sleeps). |
| 17 | // It cannot suppress other clients' activity stamps and cannot wake anything, |
| 18 | // so an outsider sending it gains nothing. Provisioning (provision.go) stays |
| 19 | // unmarked on purpose — claiming an invite is an operator action and should |
| 20 | // behave like a person's request. |
| 21 | const SidecarHeader = "X-Lchat-Sidecar" |
| 22 | |
| 23 | // markTransport stamps SidecarHeader on every request — the Runtime's HTTP |
| 24 | // client uses it so no REST call site can forget the marker. It clones the |
| 25 | // request first, per the RoundTripper contract. |
| 26 | type markTransport struct{} |
| 27 | |
| 28 | func (markTransport) RoundTrip(req *http.Request) (*http.Response, error) { |
| 29 | req = req.Clone(req.Context()) |
| 30 | req.Header.Set(SidecarHeader, "1") |
| 31 | return http.DefaultTransport.RoundTrip(req) |
| 32 | } |
| 33 | |