Documentation
¶
Overview ¶
Package middleware runs the gateway middleware plugins declare: a module with onRequest, onEvent and onResponse hooks that the gateway calls on every request an agent sends it, every event of the reply's stream and every whole reply.
It runs in the gateway's own process, in moejs, a JavaScript engine written in Go: a hook on one streamed event costs about a microsecond, where a round trip to the plugin host's Bun costs tens of them, and a reply streams thousands of events. Each middleware is compiled once; each request takes a runtime of its own from the middleware's pool, so requests run their hooks in parallel and a request's hooks share its ctx.state.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func On ¶ added in v0.1.1249
func On() int
On counts the middleware plugins.json has on, whether or not it loads.
func Points ¶ added in v0.1.1257
Points is the module's strategies and onUsage on their points, as one usage.Observer: plugins/middleware registers it while it is on. Only the process serving the gateway serves them (Serve); a strategy is registered while its plugin is on and loaded, and onUsage is given each row from then.
Types ¶
type Chain ¶ added in v0.1.1249
type Chain struct{}
Chain is the JS middleware as one agent middleware of the gateway's middleware point (gwmw.Use, by plugins/middleware): every middleware plugins.json has on, in its order.
type Routing ¶ added in v0.1.1257
type Routing struct {
ID string `json:"id"` // what a provider or group is routed by
Name string `json:"name"` // the strategy's own name
Plugin string `json:"plugin"` // the plugin it is of
}
Routing is a strategy a gateway module serves, as the routing pickers list it.
type Run ¶
type Run struct {
// contains filtered or unexported fields
}
Run is the middleware of one request. Each middleware's hooks run in one runtime for the whole request, so they share ctx.state.
func Begin ¶
Begin starts a request's middleware; nil when there is none, and a nil Run does nothing.
func (*Run) Request ¶
Request runs each onRequest on body, in turn: what one returns is the body the next is given, and undefined leaves it as it was. A hook that throws or takes too long leaves it as it was too. A non-nil Reject is a request a hook turned away.
func (*Run) Wrap ¶
func (r *Run) Wrap(w http.ResponseWriter) (http.ResponseWriter, func())
Wrap wraps w so each onEvent sees the reply's stream event by event, and each onResponse a whole JSON reply; w as it is when no middleware has either. done writes what the wrapper still holds.
type State ¶
type State struct {
Hooks []string `json:"hooks"`
Events []string `json:"events,omitempty"`
Error string `json:"error,omitempty"` // why it didn't load
Calls int64 `json:"calls"`
AvgMicro float64 `json:"avgMicros"`
Failures int64 `json:"failures"`
Last string `json:"lastError,omitempty"`
// Strategies are the routings it serves ("<plugin>/<name>"), Orders
// how many times they were asked; Usage says it has onUsage, Rows how
// many rows it was given and Dropped how many it missed, the queue
// full
Strategies []string `json:"strategies,omitempty"`
Orders int64 `json:"orders,omitempty"`
Usage bool `json:"usage,omitempty"`
Rows int64 `json:"rows,omitempty"`
Dropped int64 `json:"dropped,omitempty"`
}
State is a middleware as the Plugins page shows it.