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
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
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:
ServiceShapetrait has been added.Plugintrait has been simplified.HttpMarkerandModelMarkermarker traits have been added to better distinguish when plugins run and what they have access to.Operationstructure has been removed.ScopedPluginhas been added.The
Plugintrait has now been simplified and theOperationstruct has been removed.Addition of
ServiceShapeSince the 0.52 release the
OperationShapehas existed.This allowed
Pluginauthors to access these associated types and constants. See thePrintPluginas 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
PlugintraitPreviously,
modified an
Operation.Now,
maps a
tower::Serviceto atower::Service. This is equivalent totower::Layerwith two extra type parameters:ServiceandOperation, which implementServiceShapeandOperationShaperespectively.Having both
ServiceandOperationas 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
Plugincan no longer apply atower::Layeron HTTP requests/responses and modelled structures at the same time (see middleware positions C and D). Instead onePluginmust 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,
Plugins now have to additionally implement theHttpMarkermarker trait, theModelMarkermarker 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
Pluginis now closer totower::Layerwe have two canonical converters:Removal of
PluginPipelineSince plugins now come in two flavors (those marked with
HttpMarkerand those marked withModelMarker) that shouldn't be mixed in a collection of plugins, the primary way of concatenating plugins,PluginPipelinehas been removed in favor of theHttpPluginsandModelPluginstypes, 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_pluginimplements bothHttpMarkerandModelMarker, so we can add it to both collections.Removal of
OperationThe
aws_smithy_http_server::operation::Operationstructure has now been removed. Previously, there existed a{operation_name}_operationsetter 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::Layerto a subset of operations should now be done through thePluginAPI viafilter_by_operation_idor the new
ScopedPluginintroduced below.Addition of
ScopedCurrently, users can selectively apply a
Pluginvia thefilter_by_operation_idfunctionIn addition to this, we now provide
Scoped, which selectively applies aPluginat compiletime. Users should prefer this tofilter_by_operation_idwhen applicable.Beta Was this translation helpful? Give feedback.
All reactions