An interceptor can add logic to clients or servers, similar to the decorators
or middleware you may have seen in other libraries. Interceptors may
mutate the request and response, catch errors and retry/recover, emit
logs, or do nearly everything else.
For a simple example, the following interceptor logs all requests:
You can think of interceptors like a layered onion. A request initiated
by a client goes through the outermost layer first. In the center, the
actual HTTP request is run by the transport. The response then comes back
through all layers and is returned to the client.
Similarly, a request received by a server goes through the outermost layer
first. In the center, the actual HTTP request is received by the handler. The
response then comes back through all layers and is returned to the client.
To implement that layering, Interceptors are functions that wrap a call
invocation. In an array of interceptors, the interceptor at the end of
the array is applied first.
An interceptor can add logic to clients or servers, similar to the decorators or middleware you may have seen in other libraries. Interceptors may mutate the request and response, catch errors and retry/recover, emit logs, or do nearly everything else.
For a simple example, the following interceptor logs all requests:
You can think of interceptors like a layered onion. A request initiated by a client goes through the outermost layer first. In the center, the actual HTTP request is run by the transport. The response then comes back through all layers and is returned to the client.
Similarly, a request received by a server goes through the outermost layer first. In the center, the actual HTTP request is received by the handler. The response then comes back through all layers and is returned to the client.
To implement that layering, Interceptors are functions that wrap a call invocation. In an array of interceptors, the interceptor at the end of the array is applied first.