Slide 1

Slide 1 text

June 8th 2017 • PHPUGDD • Dresden • Check24 HOLGER WOLTERSDORF Async PHP Requests & Reactive Responses with PHP-FPM

Slide 2

Slide 2 text

HOLGER WOLTERSDORF CIO • FATHER • HUSBAND • PHP DEV WITH ♥ github.com/hollodotme @hollodotme

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

SEPTEMBER 22nd ARNE BLANKERTS thePHPcc SEBASTIAN HEUER kartenmacherei JAN BURKL ZEND MARCO PIVETTA Ocramius STEPHAN HOCHDÖRFER bitExpert ALEXANDER MIERTSCH prooph software CHRISTIAN LÜCK reactPHP

Slide 5

Slide 5 text

+ async

Slide 6

Slide 6 text

USE CASE PDF CREATION SERVICE

Slide 7

Slide 7 text

ASYNC PHP REQUESTS AND REACTIVE RESPONSES WITH PHP-FPM • JUNE 8th 2017 • PHPUGDD. • DRESDEN • CHECK24 HOLGER WOLTERSDORF WE TRIED… 7 /dev/null 2>&1 &'); # OR shell_exec('php "/create-pdf.php" > /dev/null 2>&1 &'); PHP 4 - 2001

Slide 8

Slide 8 text

ASYNC PHP REQUESTS AND REACTIVE RESPONSES WITH PHP-FPM • JUNE 8th 2017 • PHPUGDD. • DRESDEN • CHECK24 HOLGER WOLTERSDORF WORKS, BUT… 8 ๏ PHP SCRIPT IS CALLED IN CLI MODE (DIFFERENT ENVIRONMENT) ๏ COMMAND GETS QUIET MESSY WHEN A LOT OF DATA SHOULD BE PASSED ๏ DATA HANDLING IN CALLED SCRIPT BASED ON $ARGV ARRAY ๏ RESPONSE ENDS UP IN NIRWANA ๏ DEBUGGING IS A NIGHTMARE (ESPECIALLY IN PRODUCTION)

Slide 9

Slide 9 text

ASYNC PHP REQUESTS AND REACTIVE RESPONSES WITH PHP-FPM • JUNE 8th 2017 • PHPUGDD. • DRESDEN • CHECK24 HOLGER WOLTERSDORF WE TRIED… 9

Slide 10

Slide 10 text

ASYNC PHP REQUESTS AND REACTIVE RESPONSES WITH PHP-FPM • JUNE 8th 2017 • PHPUGDD. • DRESDEN • CHECK24 HOLGER WOLTERSDORF WORKS, BUT… 10 ๏ WEBSERVER ALWAYS INVOLVED (= OVERHEAD + ERROR SOURCE) ๏ MAYBE A LOAD BALANCER INVOLVED, TOO ๏ AT LEAST 2 ENVIRONMENTS TO MAINTAIN ๏ CURL EXTENSION NEEDED ๏ RESPONSE ENDS UP IN NIRWANA ๏ CALLED SCRIPT MUST BE EXPOSED (UNDER DOMAIN’S DOCUMENT ROOT)

Slide 11

Slide 11 text

ASYNC PHP REQUESTS AND REACTIVE RESPONSES WITH PHP-FPM • JUNE 8th 2017 • PHPUGDD. • DRESDEN • CHECK24 HOLGER WOLTERSDORF WE TRIED VERY HARD… 11 query( "INSERT INTO queue (id, script, data) VALUES ('123', '/create-pdf.php', '{json}')" ); #/>$ crontab -e */1 * * * * php "/path/to/queue-processor.php" +

Slide 12

Slide 12 text

ASYNC PHP REQUESTS AND REACTIVE RESPONSES WITH PHP-FPM • JUNE 8th 2017 • PHPUGDD. • DRESDEN • CHECK24 HOLGER WOLTERSDORF WORKS, BUT… 12 ๏ NO ON-DEMAND EXECUTION ๏ NEEDS A LOT OF LOCKING AND LOGGING ๏ RAISE CONDITIONS FOR THE WIN! ๏ ERRORS CAN PILE UP UNTIL SERVER IS DEAD ๏ HEAVY DATABASE LOAD FOR TECHNICALLY ELUSIVE DATA (HINT: BAD IDEA) ๏ MAINTENANCE OUTSIDE PHP PROJECT NEEDED (CRONTAB) ๏ HARD TO TEST

Slide 13

Slide 13 text

ASYNC PHP REQUESTS AND REACTIVE RESPONSES WITH PHP-FPM • JUNE 8th 2017 • PHPUGDD. • DRESDEN • CHECK24 HOLGER WOLTERSDORF WE TRIED TO BE CLEVER… 13

Slide 14

Slide 14 text

ASYNC PHP REQUESTS AND REACTIVE RESPONSES WITH PHP-FPM • JUNE 8th 2017 • PHPUGDD. • DRESDEN • CHECK24 HOLGER WOLTERSDORF WORKS, BUT… 14 ๏ WEBSERVER INVOLVED ๏ RESPONSE ENDS UP IN NIRWANA ๏ MEMORY LEAKS FOR THE WIN! ๏ PRETTY HARD ERROR HANDLING ๏ NO EXECUTION TIME LIMIT ๏ WTF? - DON’T TRY TO BE CLEVER!

Slide 15

Slide 15 text

ASYNC PHP REQUESTS AND REACTIVE RESPONSES WITH PHP-FPM • JUNE 8th 2017 • PHPUGDD. • DRESDEN • CHECK24 HOLGER WOLTERSDORF WE COULD USE… 15 pthreads

Slide 16

Slide 16 text

ASYNC PHP REQUESTS AND REACTIVE RESPONSES WITH PHP-FPM • JUNE 8th 2017 • PHPUGDD. • DRESDEN • CHECK24 HOLGER WOLTERSDORF WE COULD USE… 16 pthreads ๏ NEEDS CUSTOM PHP BUILD ๏ NOT ALL EXTENSIONS ARE THREAD-SAFE ๏ NOT WORKING IN WEB ENVIRONMENT ๏ BASIC KNOWLEDGE ABOUT MULTI THREADING NEEDED ๏ FEATURE AND CONFIG OVERHEAD FOR SIMPLE ASYNC TASKS ๏ PROCESS / THREAD MANAGEMENT IS UP TO YOU

Slide 17

Slide 17 text

ASYNC PHP REQUESTS AND REACTIVE RESPONSES WITH PHP-FPM • JUNE 8th 2017 • PHPUGDD. • DRESDEN • CHECK24 HOLGER WOLTERSDORF WE COULD USE… 17 pcntl

Slide 18

Slide 18 text

ASYNC PHP REQUESTS AND REACTIVE RESPONSES WITH PHP-FPM • JUNE 8th 2017 • PHPUGDD. • DRESDEN • CHECK24 HOLGER WOLTERSDORF WE COULD USE… 18 pcntl ๏ NEEDS CUSTOM PHP BUILD ๏ NOT WORKING ON WINDOWS ๏ BASIC KNOWLEDGE ABOUT UNIX PROCESSES NEEDED ๏ PROCESS MANAGEMENT IS UP TO YOU

Slide 19

Slide 19 text

ASYNC PHP REQUESTS AND REACTIVE RESPONSES WITH PHP-FPM • JUNE 8th 2017 • PHPUGDD. • DRESDEN • CHECK24 HOLGER WOLTERSDORF WE COULD USE… 19 ๏ NEEDS ANOTHER PIECE OF INFRASTRUCTURE + PHP EXTENSION ๏ FEATURE RICH, BUT OVERLOADED FOR SIMPLE ASYNC TASKS ๏ A LOT OF SETUP FOR LOCAL DEVELOPMENT

Slide 20

Slide 20 text

ASYNC PHP REQUESTS AND REACTIVE RESPONSES WITH PHP-FPM • JUNE 8th 2017 • PHPUGDD. • DRESDEN • CHECK24 HOLGER WOLTERSDORF WHAT DO WE WANT? 20 ๏ MAKE ASYNC CALLS TO PHP ๏ EVENTUALLY GET THE RESPONSES ๏ NO ADDITIONAL INFRASTRUCTURE ๏ NO ADDITIONAL EXTENSIONS ๏ WEB-REQUEST LIKE DATA HANDLING (BECAUSE WE’RE USED TO IT) ๏ BACKGROUND WORKERS NOT EXPOSED TO "PUBLIC" ๏ TUNEABLE PROCESS MANAGEMENT

Slide 21

Slide 21 text

ASYNC PHP REQUESTS AND REACTIVE RESPONSES WITH PHP-FPM • JUNE 8th 2017 • PHPUGDD. • DRESDEN • CHECK24 HOLGER WOLTERSDORF THE IDEA… 21 TALK BY ARNE BLANKERTS AT CONFOO / IPC IN 2016

Slide 22

Slide 22 text

ASYNC PHP REQUESTS AND REACTIVE RESPONSES WITH PHP-FPM • JUNE 8th 2017 • PHPUGDD. • DRESDEN • CHECK24 HOLGER WOLTERSDORF WHAT I FOUND… 22 ๏ SEEMED OUTDATED AND UNMAINTAINED ๏ BUT DID A GOOD PART OF THE JOB PIERRICK CHARRON ๏ DESIGNED TO SEND SINGLE (A)SYNCHRONOUS REQUEST

Slide 23

Slide 23 text

hollodotme/fast-cgi-client

Slide 24

Slide 24 text

ASYNC PHP REQUESTS AND REACTIVE RESPONSES WITH PHP-FPM • JUNE 8th 2017 • PHPUGDD. • DRESDEN • CHECK24 HOLGER WOLTERSDORF WHAT I DID… 24 ๏ UPDATED PHP LANGUAGE LEVEL TO 7.X ๏ Releases v1.x for PHP 7.0.x ๏ Releases v2.x for PHP 7.1.x ๏ SPLIT CODE INTO MULTIPLE CLASSES ๏ ADDED UNIT AND INTEGRATION TESTS ๏ POLISHED THE CONNECTION, CLIENT, REQUEST & RESPONSE INTERFACES ๏ ADDED FEATURES FOR MULTI-REQUESTS AND LOOP INTEGRATION ๏ ADDED USAGE DOCUMENTATION

Slide 25

Slide 25 text

ASYNC PHP REQUESTS AND REACTIVE RESPONSES WITH PHP-FPM • JUNE 8th 2017 • PHPUGDD. • DRESDEN • CHECK24 HOLGER WOLTERSDORF FACTS 25 ๏ LOC ๏ Lines of code 1036 ๏ Logical lines of code 823 ๏ Comment lines of code 213 ๏ Average volume 218.58 ๏ Average comment weight 30.19 ๏ Average intelligent content 30.19 ๏ Logical lines of code by class 37 ๏ Logical lines of code by method 9 ๏ OBJECT ORIENTED PROGRAMMING ๏ Classes 22 ๏ Interface 5 ๏ Methods 92 ๏ Methods by class 4.18 ๏ Lack of cohesion of methods 0.56 ๏ Average afferent coupling 1.81 ๏ Average efferent coupling 2.04 ๏ Average instability 0.45

Slide 26

Slide 26 text

ASYNC PHP REQUESTS AND REACTIVE RESPONSES WITH PHP-FPM • JUNE 8th 2017 • PHPUGDD. • DRESDEN • CHECK24 HOLGER WOLTERSDORF CONNECT TO UNIX DOMAIN SOCKET 26

Slide 27

Slide 27 text

ASYNC PHP REQUESTS AND REACTIVE RESPONSES WITH PHP-FPM • JUNE 8th 2017 • PHPUGDD. • DRESDEN • CHECK24 HOLGER WOLTERSDORF PHP-FPM POOL CONFIG UNIX DOMAIN SOCKET 27 ; Pool name [background] ; Process ownership user = www-data group = www-data ; Socket path listen = /var/run/php/php7.1-fpm-background.sock ; Socket ownership listen.owner = www-data listen.group = www-data ; Process management pm = ondemand ; Maximum of children that can be alive at the same time pm.max_children = 5 ; Number of seconds after which an idle children will be killed pm.process_idle_timeout = 10s

Slide 28

Slide 28 text

ASYNC PHP REQUESTS AND REACTIVE RESPONSES WITH PHP-FPM • JUNE 8th 2017 • PHPUGDD. • DRESDEN • CHECK24 HOLGER WOLTERSDORF CONNECT TO NETWORK SOCKET 28

Slide 29

Slide 29 text

ASYNC PHP REQUESTS AND REACTIVE RESPONSES WITH PHP-FPM • JUNE 8th 2017 • PHPUGDD. • DRESDEN • CHECK24 HOLGER WOLTERSDORF PHP-FPM POOL CONFIG NETWORK SOCKET 29 ; Pool name [background] ; Process ownership user = www-data group = www-data ; Socket IP and Port listen = 127.0.0.1:9001 ; Socket ownership listen.owner = www-data listen.group = www-data ; Process management pm = ondemand ; Maximum of children that can be alive at the same time pm.max_children = 5 ; Number of seconds after which an idle children will be killed pm.process_idle_timeout = 10s

Slide 30

Slide 30 text

ASYNC PHP REQUESTS AND REACTIVE RESPONSES WITH PHP-FPM • JUNE 8th 2017 • PHPUGDD. • DRESDEN • CHECK24 HOLGER WOLTERSDORF SEND A SYNCHRONOUS REQUEST 30 'value'] ); # Simulate HTTP Verbs: GET, POST, PUT, PATCH, DELETE $request = new PostRequest( '/create-pdf.php', $content ); $response = $client->sendRequest( $request ); echo $response->getBody();

Slide 31

Slide 31 text

ASYNC PHP REQUESTS AND REACTIVE RESPONSES WITH PHP-FPM • JUNE 8th 2017 • PHPUGDD. • DRESDEN • CHECK24 HOLGER WOLTERSDORF RESPONSE INTERFACE 31

Slide 32

Slide 32 text

ASYNC PHP REQUESTS AND REACTIVE RESPONSES WITH PHP-FPM • JUNE 8th 2017 • PHPUGDD. • DRESDEN • CHECK24 HOLGER WOLTERSDORF ASYNC FIRE & FORGET 32 'value'] ); $request = new PostRequest( '/create-pdf.php', $content ); $requestId = $client->sendAsyncRequest( $request ); echo "Request sent, got ID: {$requestId}";

Slide 33

Slide 33 text

ASYNC PHP REQUESTS AND REACTIVE RESPONSES WITH PHP-FPM • JUNE 8th 2017 • PHPUGDD. • DRESDEN • CHECK24 HOLGER WOLTERSDORF ASYNC REQUEST + WAIT FOR RESPONSE 33 sendAsyncRequest( $request ); echo "Request sent, got ID: {$requestId}"; # Do something in the meanwhile here ... $response = $client->readResponse( $requestId ); # Blocking call until response is received or read timed out echo $response->getBody();

Slide 34

Slide 34 text

ASYNC PHP REQUESTS AND REACTIVE RESPONSES WITH PHP-FPM • JUNE 8th 2017 • PHPUGDD. • DRESDEN • CHECK24 HOLGER WOLTERSDORF USING RESPONSE CALLBACKS 34 addResponseCallbacks( function( ProvidesResponseData $response ) { echo $response->getBody(); } ); # Failure callbacks expect a \Throwable instance # ...variadic function $request->addFailureCallbacks( function ( \Throwable $throwable ) { echo $throwable->getMessage(); } ); $requestId = $client->sendAsyncRequest( $request );

Slide 35

Slide 35 text

ASYNC PHP REQUESTS AND REACTIVE RESPONSES WITH PHP-FPM • JUNE 8th 2017 • PHPUGDD. • DRESDEN • CHECK24 HOLGER WOLTERSDORF USING RESPONSE CALLBACKS 35 waitForResponse( $requestId ); # Inner loop # ... is the same as while(true) # Outer loop { if ( $client->hasResponse( $requestId ) ) { $client->handleResponse( $requestId ); break; } }

Slide 36

Slide 36 text

ASYNC PHP REQUESTS AND REACTIVE RESPONSES WITH PHP-FPM • JUNE 8th 2017 • PHPUGDD. • DRESDEN • CHECK24 HOLGER WOLTERSDORF MULTIPLE REQUESTS - ORDERED RESPONSES 36 sendAsyncRequest( $request1 ); $requestIds[] = $client->sendAsyncRequest( $request2 ); $requestIds[] = $client->sendAsyncRequest( $request3 ); echo 'Sent requests: ' . implode( ', ', $requestIds ) . "\n"; # Do something else here in the meanwhile ... # Blocking call until all responses are received or read timed out foreach ($client->readResponses(5000, ...$requestIds) as $response) { echo $response->getBody() . "\n"; }

Slide 37

Slide 37 text

ASYNC PHP REQUESTS AND REACTIVE RESPONSES WITH PHP-FPM • JUNE 8th 2017 • PHPUGDD. • DRESDEN • CHECK24 HOLGER WOLTERSDORF MULTIPLE REQUESTS - ORDERED RESPONSES 37 # PRINTS Response #1 Response #2 Response #3 ๏ NO MATTER HOW LONG EACH WORKER TOOK TO RESPOND

Slide 38

Slide 38 text

ASYNC PHP REQUESTS AND REACTIVE RESPONSES WITH PHP-FPM • JUNE 8th 2017 • PHPUGDD. • DRESDEN • CHECK24 HOLGER WOLTERSDORF MULTIPLE REQUESTS - REACTIVE RESPONSES 38 sendAsyncRequest( $request1 ); $requestIds[] = $client->sendAsyncRequest( $request2 ); $requestIds[] = $client->sendAsyncRequest( $request3 ); echo 'Sent requests: ' . implode( ', ', $requestIds ) . "\n"; # Do something else here in the meanwhile ... while ( $client->hasUnhandledResponses() ) { # read all ready responses foreach ( $client->readReadyResponses() as $response ) { echo $response->getBody() . "\n"; }
 echo '.'; }

Slide 39

Slide 39 text

ASYNC PHP REQUESTS AND REACTIVE RESPONSES WITH PHP-FPM • JUNE 8th 2017 • PHPUGDD. • DRESDEN • CHECK24 HOLGER WOLTERSDORF MULTIPLE REQUESTS - REACTIVE RESPONSES 39 hasUnhandledResponses() ) { $readyRequestIds = $client->getRequestIdsHavingResponse(); # read all ready responses foreach ( $readyRequestIds as $requestId ) { $response = $client->readResponse( $requestId ); echo $response->getBody() . "\n"; }
 echo '.'; }

Slide 40

Slide 40 text

ASYNC PHP REQUESTS AND REACTIVE RESPONSES WITH PHP-FPM • JUNE 8th 2017 • PHPUGDD. • DRESDEN • CHECK24 HOLGER WOLTERSDORF MULTIPLE REQUESTS - REACTIVE RESPONSES 40 # PRINTS .....Response #2 .......Response #3 ..........Response #1 ๏ ORDERED BY RESPONSE TIME

Slide 41

Slide 41 text

ASYNC PHP REQUESTS AND REACTIVE RESPONSES WITH PHP-FPM • JUNE 8th 2017 • PHPUGDD. • DRESDEN • CHECK24 HOLGER WOLTERSDORF MULTIPLE REQUESTS - REACTIVE CALLBACKS 41 waitForResponses(); # Inner loop # ... is the same as while ( $client->hasUnhandledResponses() ) # Outer loop { $client->handleReadyResponses(); } # ... is the same as while ( $client->hasUnhandledResponses() ) # Outer loop { $readyRequestIds = $client->getRequestIdsHavingResponse(); foreach ( $readyRequestIds as $requestId ) { $client->handleResponse( $requestId ); } }

Slide 42

Slide 42 text

QUESTIONS?

Slide 43

Slide 43 text

THANK YOU! github.com/hollodotme @hollodotme / @F9T3ch fortuneglobe.com phpug-dresden.org @phpugdd HOLGER WOLTERSDORF icehawk.github.io speakerdeck.com/hollodotme Slides available at:

Slide 44

Slide 44 text

ASYNC PHP REQUESTS AND REACTIVE RESPONSES WITH PHP-FPM • JUNE 8th 2017 • PHPUGDD. • DRESDEN • CHECK24 HOLGER WOLTERSDORF LINKS / REFERENCES 44 ๏ PHP USERGROUP DRESDEN e.V. http://phpug-dresden.org ๏ PHP Developer Day 2017: https://bit.ly/PHPDD17EN ๏ Asynchronous cURL Requests: http://www.paul-norman.co.uk/2009/06/asynchronous-curl-requests/ ๏ Arne Blankerts’ talk about nodejs + PHP: https://thephp.cc/dates/2016/02/confoo/just-married-node-js-and-php ๏ Original PHP FastCGI Client by Pierrick Charron: https://github.com/adoy/PHP-FastCGI-Client/ ๏ PHP FastCGI Client: https://github.com/hollodotme/fast-cgi-client ๏ PHP FastCGI Client Demo: https://github.com/hollodotme/fast-cgi-client-demo ๏ FastCGI Specification: http://www.mit.edu/~yandros/doc/specs/fcgi-spec.html ๏ Experimental use-case with rabbitMQ: https://hollo.me/php/experimental-async-php-volume-2.html