middleware

package
v0.1.1274 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 16, 2026 License: MIT Imports: 21 Imported by: 0

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

func Points() usage.Observer

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.

func Reload

func Reload()

Reload drops the middleware loaded, so the next request loads it again.

func States

func States() map[string]State

States are the middleware plugins, by spec.

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.

func (Chain) Begin added in v0.1.1249

func (Chain) Begin(info gwmw.Info) gwmw.Run

Begin is Begin, nil when there is no middleware.

type Info

type Info = gwmw.Info

Info is what a request's hooks are told of it, in ctx.

type Reject

type Reject = gwmw.Reject

Reject is a request a middleware turned away (ctx.reject).

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.

func Routings added in v0.1.1257

func Routings() []Routing

Routings are the strategies of the modules loaded that this process's router serves now: none while plugins/middleware is off or the gateway isn't served here.

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

func Begin(info Info) *Run

Begin starts a request's middleware; nil when there is none, and a nil Run does nothing.

func (*Run) End

func (r *Run) End()

End gives the runtimes back.

func (*Run) Request

func (r *Run) Request(body []byte) ([]byte, *Reject)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL