Php sdk-v3-async
Transcript of Php sdk-v3-async
AWS PHP SDK v3Asynchronous Requests
Michael Dowling @mtdowling
AWS PHP SDK v3Asynchronous Requests
Michael Dowling @mtdowling
What we’ll cover
• Promises
• Handlers and Middleware
• ReactPHP in the SDK
• A promise represents the eventual result of an asynchronous operation.
• Promises are values and are composable
https://promisesaplus.com/
Promises/A+
Promises• A promise has a then() method, which allows
access to its current or eventual value or reason.
• Promises are resolved with a value or rejected with a reason.
• State transitions: Pending -> (Fulfilled | Rejected)
• Fulfilled and rejected promises are immutable.
https://promisesaplus.com/
Promises
then()• Calling then() on a promise will return a new promise
that is fulfilled or rejected based on the result of invoking the provided then($onFullfilled, $onRejected) callbacks.
• If a callback throws, the promise is rejected and downstream promises are rejected.
• If a rejected callback does not throw, downstream promises are fulfilled.
• If a promise is fulfilled or rejected with a promise, any callbacks registered on the promise will not be invoked until the promise is resolved.
then()
Promises in the SDK
1.
2.
• Multiple promises can be composed together into a single promise using combinators
• Guzzle ships with several combinators
Promise Combinators
Promise Combinator Examples
all($promises) // usage
some($promises) // usage
any($promises) // usage
each_limit($iterable, $concurrency, …)
Promise Coroutines
SDK Pool
Handlers and Middleware• A handler is a function that performs the actual
transformation of a command and request into a result (e.g., Guzzle is a handler).
• A middleware is a higher-order function that modifies a command, request, or result as it passes through the middleware.
• A HandlerList is used by clients and commands to compose middleware on a handler.
Handlers
HandlerList• A client has a HandlerList that contains a
handler and a list of middleware.
• Each command created by a client get a clone of the client’s HandlerList that allows per/command customizations.
HandlerList• Request lifecycle in the HandlerList
init -> validate -> build -> sign
sign -> build -> validate -> init
execute
returns Promise<Result, Exception>
Command
Adding middleware to HandlerList
Middleware
Anatomy of a middlewareExample: Validation middleware.
2) Shared state
3) Return acomposablefunction
4) Return anew composedhandler
5) Call thenext handlerand returna promise
1) Wrappingfunction
Higher-order middleware
• Some handlers support async, some emulate.
• cURL handlers support async but must use wait or you must tick the curl event loop explicitly.
• Some handlers support async interop with event loops…
Custom Handlers
Using a custom handler
• http_handler: works at the PSR-7 layer. handler($request, $options): Promise<Response, Error>
• handler: works at the SDK layer. handler($command, $request): Promise<Result, Error>
Using a custom handler
React Guzzle Handler• The SDK supports Guzzle.
• Cees-Jan Kiewiet created a React Guzzle handler.
• Therefore you can use React with the SDK!
composer require wyrihaximus/react-‐guzzle-‐psr7
https://github.com/WyriHaximus/react-guzzle-psr7
Questions?