Server middleware changes in 0.56.x #2895
jdisanti
announced in
Change Log
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
The middleware system has been reworked as we push for a unified, simple, and consistent API. The following changes have been made in service of this goal:
ServiceShape
trait has been added.Plugin
trait has been simplified.HttpMarker
andModelMarker
marker traits have been added to better distinguish when plugins run and what they have access to.Operation
structure has been removed.Scoped
Plugin
has been added.The
Plugin
trait has now been simplified and theOperation
struct has been removed.Addition of
ServiceShape
Since the 0.52 release the
OperationShape
has existed.This allowed
Plugin
authors to access these associated types and constants. See thePrintPlugin
as an example.We continue with this approach and introduce the following trait:
With the changes to
Plugin
, described below, middleware authors now have access to this information at compile time.Simplication of the
Plugin
traitPreviously,
modified an
Operation
.Now,
maps a
tower::Service
to atower::Service
. This is equivalent totower::Layer
with two extra type parameters:Service
andOperation
, which implementServiceShape
andOperationShape
respectively.Having both
Service
andOperation
as type parameters also provides an even surface for advanced users to extend the codegenerator in a structured way. See this issue for more context.The following middleware setup
now becomes
Alternatively, using the new
ServiceShape
, implemented onSer
:A single
Plugin
can no longer apply atower::Layer
on HTTP requests/responses and modelled structures at the same time (see middleware positions C and D). Instead onePlugin
must be specified for each and passed to the service builder constructor separately:To better distinguish when a plugin runs and what it has access to,
Plugin
s now have to additionally implement theHttpMarker
marker trait, theModelMarker
marker trait, or both:The motivation behind this change is to simplify the job of middleware authors, separate concerns, accomodate common cases better, and to improve composition internally.
Because
Plugin
is now closer totower::Layer
we have two canonical converters:Removal of
PluginPipeline
Since plugins now come in two flavors (those marked with
HttpMarker
and those marked withModelMarker
) that shouldn't be mixed in a collection of plugins, the primary way of concatenating plugins,PluginPipeline
has been removed in favor of theHttpPlugins
andModelPlugins
types, which eagerly check that whenever a plugin is pushed, it is of the expected type.This worked before, but you wouldn't be able to do apply this collection of plugins anywhere; if you tried to, the compilation error messages would not be very helpful:
Now collections of plugins must contain plugins of the same flavor:
In the above example,
&http_and_model_plugin
implements bothHttpMarker
andModelMarker
, so we can add it to both collections.Removal of
Operation
The
aws_smithy_http_server::operation::Operation
structure has now been removed. Previously, there existed a{operation_name}_operation
setter on the service builder, which accepted anOperation
. This allowed users toto set an operation with a
tower::Service
, andto add a
tower::Layer
(acting on HTTP requests/responses post-routing) to a single operation.We have seen little adoption of this API and for this reason we have opted instead to introduce a new setter, accepting a
tower::Service
, on the service builder:Applying a
tower::Layer
to a subset of operations should now be done through thePlugin
API viafilter_by_operation_id
or the new
Scoped
Plugin
introduced below.Addition of
Scoped
Currently, users can selectively apply a
Plugin
via thefilter_by_operation_id
functionIn addition to this, we now provide
Scoped
, which selectively applies aPlugin
at compiletime. Users should prefer this tofilter_by_operation_id
when applicable.Beta Was this translation helpful? Give feedback.
All reactions