The proxy service uses a composable stack of middleware functions where each middleware is responsible for a focused sub-set of those features.
Middleware take the form of a function that accepts as params the next
middleware function to call, and other parameters such as a logger or database client for debugging and storing request and response values.
func noOpMiddleware(next http.HandlerFunc, otherParams ...interface{}) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r)
}
}
Any modifications that the middleware function makes to the request or response before calling next.ServeHTTP()
will be observable by the next
middleware function, any modifications made after next.ServeHTTP()
has been called will not.
The earlier the middleware is instantiated, the later it will run. For example the first middleware created by the proxy service is the middleware that will run after the request has been logged and proxied, thereby allowing it to access both the recorded request body and response body, and any context enrichment added by prior middleware.
kava-proxy-service/service/service.go
Lines 54 to 110 in 847d788
The middleware sequence of EVM requests to the proxy service:
- Captures start time of request for latency metrics calculations
- Attempts to decode the request:
- As a single EVM request. If successful, forwards to Single Request Middleware Sequence with the request in the context.
- As a batch EVM request. If successful, forwards to Batch Processing Middleware with batch in context.
- On failure to decode, the request is sent down the Single Request Middleware Sequence, but with nothing in the context.
If a single request is decoded (as opposed to a batch list), or the request fails to decode, it is forwarded down this middleware sequence. Additionally, each individual sub-request of a batch is routed through this sequence in order to leverage caching and metrics collection.
This middleware sequence uses the decoded single request from the request context.
The front part of the two-part caching middleware. Responsible for determining if an incoming request can be fielded from the cache. If it can, the cached response is put into the context.
See CACHING for more details.
- Proxies the request to the configured backend origin server.
- Times the roundtrip latency for the response from the backend origin server and stores the latency in the context key
X-KAVA-PROXY-ORIGIN-ROUNDTRIP-LATENCY-MILLISECONDS
for use by other middleware.
The Proxy middleware is responsible for writing the response to the requestor. Subsequent middlewares are non-blocking to the response.
See Proxy Routing for details on configuration and how requests are routed.
Handles determining if a response can be put in the cache if it isn't already.
See CACHING for more details.
- Parses the request body and latency from context key values and creates a request metric for the proxied request.
- Pulls decoded batch out of the request context
- Separates into individual sub-requests
- Routes each sub-request through the Single Request Middleware Sequence in order to leverage caching and metrics creation.
- Combines all sub-request responses into a single response to the client.