Skip to content

Commit

Permalink
run http requests asynchronously
Browse files Browse the repository at this point in the history
  • Loading branch information
Baptouuuu committed Nov 5, 2023
1 parent 7e2975d commit ac41d76
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"require": {
"php": "~8.2",
"innmind/immutable": "~5.2",
"innmind/operating-system": "~4.0",
"innmind/operating-system": "~4.1",
"innmind/filesystem": "~7.3"
},
"autoload": {
Expand Down
52 changes: 51 additions & 1 deletion proofs/functional.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@
use Innmind\OperatingSystem\Factory;
use Innmind\TimeContinuum\Earth\Period\Second;
use Innmind\Filesystem\Name;
use Innmind\Url\Path;
use Innmind\Http\{
Request,
Method,
ProtocolVersion,
};
use Innmind\Url\{
Url,
Path,
};
use Innmind\Immutable\Sequence;
use Innmind\BlackBox\Set;

Expand Down Expand Up @@ -229,4 +237,46 @@ static function($os) use ($assert, &$chunks) {
);
},
);

yield test(
'HTTP requests are handled asynchronously',
static function($assert) {
$order = [];
Forerunner::of(Factory::build())(null, Predetermined::of(
static function($os) use ($assert, &$order) {
$os
->remote()
->http()(Request::of(
Url::of('https://httpbin.org/delay/2'),
Method::get,
ProtocolVersion::v11,
))
->match(
static fn() => null,
static fn() => null,
);
$order[] = 'first';
},
static function($os) use ($assert, &$order) {
$os
->remote()
->http()(Request::of(
Url::of('https://httpbin.org/delay/1'),
Method::get,
ProtocolVersion::v11,
))
->match(
static fn() => null,
static fn() => null,
);
$order[] = 'second';
},
));

$assert->same(
['second', 'first'],
$order,
);
},
);
};
12 changes: 11 additions & 1 deletion src/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
Factory,
OperatingSystem,
};
use Innmind\TimeContinuum\Earth\{
ElapsedPeriod,
Period\Millisecond,
};

/**
* @template R
Expand Down Expand Up @@ -46,7 +50,13 @@ public function start(OperatingSystem $synchronous): mixed
$suspend,
$config->streamCapabilities(),
))
->haltProcessVia(Asynchronous\Halt::of($suspend)),
->haltProcessVia(Asynchronous\Halt::of($suspend))
->withHttpHeartbeat(
ElapsedPeriod::of(10), // this is blocking the active task so it needs to be low
static fn() => $suspend(Suspend\Halt::of( // this allows to jump between tasks
Millisecond::of(1),
)),
),
),
));

Expand Down

0 comments on commit ac41d76

Please sign in to comment.