Php sdk-v3-async

30
AWS PHP SDK v3 Asynchronous Requests Michael Dowling @mtdowling

Transcript of Php sdk-v3-async

Page 1: Php sdk-v3-async

AWS PHP SDK v3Asynchronous Requests

Michael Dowling @mtdowling

Page 2: Php sdk-v3-async

AWS PHP SDK v3Asynchronous Requests

Michael Dowling @mtdowling

Page 3: Php sdk-v3-async

What we’ll cover

• Promises

• Handlers and Middleware

• ReactPHP in the SDK

Page 4: Php sdk-v3-async

• A promise represents the eventual result of an asynchronous operation.

• Promises are values and are composable

https://promisesaplus.com/

Promises/A+

Page 5: Php sdk-v3-async

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/

Page 6: Php sdk-v3-async

Promises

Page 7: Php sdk-v3-async

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.

Page 8: Php sdk-v3-async

then()

Page 9: Php sdk-v3-async

Promises in the SDK

1.

2.

Page 10: Php sdk-v3-async

• Multiple promises can be composed together into a single promise using combinators

• Guzzle ships with several combinators

Promise Combinators

Page 11: Php sdk-v3-async

Promise Combinator Examples

Page 12: Php sdk-v3-async

all($promises)  //  usage

Page 13: Php sdk-v3-async

some($promises)  //  usage

Page 14: Php sdk-v3-async

any($promises)  //  usage

Page 15: Php sdk-v3-async

each_limit($iterable,  $concurrency,  …)

Page 16: Php sdk-v3-async

Promise Coroutines

Page 17: Php sdk-v3-async

SDK Pool

Page 18: Php sdk-v3-async

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.

Page 19: Php sdk-v3-async

Handlers

Page 20: Php sdk-v3-async

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.

Page 21: Php sdk-v3-async

HandlerList• Request lifecycle in the HandlerList

init -> validate -> build -> sign

sign -> build -> validate -> init

execute

returns Promise<Result, Exception>

Command

Page 22: Php sdk-v3-async

Adding middleware to HandlerList

Page 23: Php sdk-v3-async

Middleware

Page 24: Php sdk-v3-async

Anatomy of a middlewareExample: Validation middleware.

2)  Shared  state

3)  Return  acomposablefunction

4)  Return  anew  composedhandler

5)  Call  thenext  handlerand  returna  promise

1)  Wrappingfunction

Page 25: Php sdk-v3-async

Higher-order middleware

Page 26: Php sdk-v3-async

• 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

Page 27: Php sdk-v3-async

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>

Page 28: Php sdk-v3-async

Using a custom handler

Page 29: Php sdk-v3-async

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

Page 30: Php sdk-v3-async

Questions?