Documentation
¶
Overview ¶
Package reqlog is the live view of requests crossing a tunnel.
Both ends see the same request and can report it: the hub as it proxies (revproxy.WithRequestHook) and the peer as it serves (dial.Options.RequestHook). Neither stores anything. A hook is called once per request, after the response, and whatever it does with the event is the application's business: print it, count it, ship it somewhere.
The holt CLI uses it for the line `holt expose` and `holt hub` print as traffic goes through, which is the whole point: a tunnel you cannot watch is hard to trust.
Index ¶
Constants ¶
const DefaultRecent = 100
DefaultRecent is how many events a new watcher is replayed when the broker is built with 0. Enough to fill a console panel, small enough to forget.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Broker ¶ added in v0.37.0
type Broker struct {
// contains filtered or unexported fields
}
Broker fans one Hook out to many watchers: the hub publishes each request once, and every console tab watching gets it.
Nothing is persisted. The broker keeps the last Recent events in memory so a tab that opens mid-traffic is not blank, and that buffer dies with the process. A watcher that falls behind loses events rather than slowing the request that produced them.
broker := reqlog.NewBroker(0)
proxy := revproxy.New(reg, revproxy.WithRequestHook(broker.Hook()))
for ev := range broker.Watch(ctx) { ... }
func NewBroker ¶ added in v0.37.0
NewBroker returns a broker replaying the last recent events to each new watcher. Pass 0 for DefaultRecent, or a negative number for no replay at all (a watcher then sees only what arrives after it).
func (*Broker) Hook ¶ added in v0.37.0
Hook returns the Hook that feeds the broker, for revproxy.WithRequestHook or dial.Options.RequestHook.
type Event ¶
type Event struct {
// At is when the response completed.
At time.Time
// Peer is the tunnel the request went through. The hub fills it;
// a peer reporting its own requests leaves it empty, since it
// knows only one.
Peer string
// Method and Path are the request's, as received.
Method string
Path string
// Status is the response code, 0 when the request never got one
// (the connection died first).
Status int
// Duration is how long the response took at the reporting end,
// so the hub's includes the tunnel hop and the peer's does not.
Duration time.Duration
}
Event is one request, seen from whichever end reports it.
type Hook ¶
type Hook func(Event)
Hook receives one event per request. It runs on the request's goroutine after the response, so it should not block: print, count, or hand off to a channel.
type Recorder ¶
type Recorder struct {
http.ResponseWriter
// contains filtered or unexported fields
}
Recorder captures the status code of a response while forwarding everything else, so a handler behind it keeps streaming, flushing and hijacking as it would without one.
func NewRecorder ¶
func NewRecorder(w http.ResponseWriter) *Recorder
NewRecorder wraps w. Until the handler writes, the status reads as 200, which is what net/http sends for a handler that writes a body without calling WriteHeader.
func (*Recorder) Flush ¶
func (r *Recorder) Flush()
Flush keeps streaming responses streaming: without it the wrapper would hide the underlying Flusher and the proxy's immediate flush would buffer.
func (*Recorder) Unwrap ¶
func (r *Recorder) Unwrap() http.ResponseWriter
Unwrap lets http.ResponseController reach the real writer, which the standard library looks for (hijack, deadlines).