Skip to content

Commit

Permalink
update middleware sequence documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
pirtleshell committed Feb 7, 2024
1 parent 845d356 commit 29004a1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 34 deletions.
9 changes: 4 additions & 5 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ DATABASE_CONNECTION_MAX_IDLE_SECONDS=5
DATABASE_MAX_OPEN_CONNECTIONS=20
# controls whether the service will attempt to run migrations when it starts
RUN_DATABASE_MIGRATIONS=true
DATABASE_QUERY_LOGGING_ENABLED=true
DATABASE_QUERY_LOGGING_ENABLED=false
# How often the metric compaction routine will run
# defaults to 3600 / 1 hour if not set
METRIC_COMPACTION_ROUTINE_INTERVAL_SECONDS=5
Expand All @@ -93,7 +93,7 @@ EVM_QUERY_SERVICE_URL=http://kava-validator:8545
METRIC_COLLECTION_ENABLED=true
# How often the metric partitioning routine will run
# defaults to 86400 / 24 hours if not set
METRIC_PARTITIONING_ROUTINE_INTERVAL_SECONDS=60
METRIC_PARTITIONING_ROUTINE_INTERVAL_SECONDS=6000
# how long (after the proxy service starts) it will wait
# to run the first iteration of the metric partitioning routine
METRIC_PARTITIONING_ROUTINE_DELAY_FIRST_RUN_SECONDS=10
Expand All @@ -105,10 +105,10 @@ METRIC_PARTITIONINING_PREFILL_PERIOD_DAYS=7
# Used by `ready` script to ensure metric partitions have been created.
MINIMUM_REQUIRED_PARTITIONS=30
# Whether metric pruning routines should run on the configured interval, defaults to true
METRIC_PRUNING_ENABLED=true
METRIC_PRUNING_ENABLED=false
# How frequenlty metric pruning routines should run
# defaults to 1 day
METRIC_PRUNING_ROUTINE_INTERVAL_SECONDS=10
METRIC_PRUNING_ROUTINE_INTERVAL_SECONDS=1000
# how long (after the proxy service starts) it will wait
# to run the first iteration of the metric pruning routine
METRIC_PRUNING_ROUTINE_DELAY_FIRST_RUN_SECONDS=5
Expand Down Expand Up @@ -148,7 +148,6 @@ HOSTNAME_TO_ACCESS_CONTROL_ALLOW_ORIGIN_VALUE_MAP=
##### Database Config
POSTGRES_PASSWORD=password


##### Redis Config
# for local development don't require a password to connect to the redis server
ALLOW_EMPTY_PASSWORD=yes
60 changes: 31 additions & 29 deletions architecture/MIDDLEWARE.MD
Original file line number Diff line number Diff line change
Expand Up @@ -20,49 +20,51 @@ Any modifications that the middleware function makes to the request or response

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.

```golang
service := ProxyService{}
https://github.com/Kava-Labs/kava-proxy-service/blob/847d7889bf5f37770d373d73cd4600a769ebd29c/service/service.go#L54-L110

// create an http router for registering handlers for a given route
mux := http.NewServeMux()
## Middleware

// will run after the proxy middleware handler and is
// the final function called after all other middleware
// allowing it to access values added to the request context
// to do things like metric the response and cache the response
afterProxyFinalizer := createAfterProxyFinalizer(&service)
The middleware sequence of EVM requests to the proxy service:
![Middleware Sequence for Proxy Service](./images/proxy_service_middleware_sequence.jpg)

// create an http handler that will proxy any request to the specified URL
proxyMiddleware := createProxyRequestMiddleware(afterProxyFinalizer, config, serviceLogger)
### Decode Request Middleware

// create an http handler that will log the request to stdout
// this handler will run before the proxyMiddleware handler
requestLoggingMiddleware := createRequestLoggingMiddleware(proxyMiddleware, serviceLogger)
1. Captures start time of request for latency metrics calculations
1. 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.

// register middleware chain as the default handler for any request to the proxy service
mux.HandleFunc("/", requestLoggingMiddleware)
### Single Request Middleware Sequence
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.

// create an http server for the caller to start on demand with a call to ProxyService.Run()
server := &http.Server{
Addr: fmt.Sprintf(":%s", config.ProxyServicePort),
Handler: mux,
}
```
This middleware sequence uses the decoded single request from the request context.

## Middleware
#### IsCached Middleware
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.

### Request Logging Middleware
See [CACHING](./CACHING.md#iscachedmiddleware) for more details.

1. Logs the request body to stdout and stores a parsed version of the request body in the context key `X-KAVA-PROXY-DECODED-REQUEST-BODY` for use by other middleware.

### Proxy Middleware
#### Proxy Middleware

1. Proxies the request to the configured backend origin server.
2. 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.

1. 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](./PROXY_ROUTING.md) for details on configuration and how requests are routed.

### After Proxy Middleware
#### Caching Middleware
Handles determining if a response can be put in the cache if it isn't already.

See [CACHING](./CACHING.md#cachingmiddleware) for more details.

#### After Proxy Middleware

1. Parses the request body and latency from context key values and creates a request metric for the proxied request.

### Batch Processing Middleware
1. Pulls decoded batch out of the request context
2. Separates into individual sub-requests
3. Routes each sub-request through the Single Request Middleware Sequence in order to leverage caching and metrics creation.
4. Combines all sub-request responses into a single response to the client.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 29004a1

Please sign in to comment.