Skip to content

Commit

Permalink
handle octane task timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
Wirewraith committed Dec 9, 2024
1 parent 91ec5be commit 8be94bb
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions app/Libraries/GameBridge/BridgeConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@
use App\Models\GameServer;
use Illuminate\Database\Eloquent\Collection as ModelCollection;
use Illuminate\Support\Collection;
use Laravel\Octane\Exceptions\TaskTimeoutException;
use Laravel\Octane\Facades\Octane;
use RuntimeException;

class BridgeConnection
{
private Collection $targets;

private ?int $timeout = null;
private int $timeout = 5;

private ?string $message = null;

private ?bool $force = null;
private bool $force = false;

private ?int $cacheFor = null;
private int $cacheFor = 30;

public function target($target)
{
Expand Down Expand Up @@ -132,13 +133,25 @@ public function send(bool $wantResponse = true): BridgeConnectionResponse|Collec
];

$jobs = [];
$timeout = 0;
foreach ($this->targets as $target) {
$jobs[$target->serverId] = $this->handler($target->address, $target->port, $options);
if ($wantResponse) {
$timeout += $this->timeout * 3; // connect, send, read
} else {
$timeout += $this->timeout * 2; // connect, send
}
}

$responses = collect(Octane::concurrently($jobs));
$response = null;
try {
$responses = collect(Octane::concurrently($jobs, $timeout * 1000));
$response = count($responses) === 1 ? $responses->first() : $responses;
} catch (TaskTimeoutException $e) {
$response = new BridgeConnectionResponse($e->getMessage(), true);
}

return count($responses) === 1 ? $responses->first() : $responses;
return $response;
}

public function sendAndForget()
Expand Down

0 comments on commit 8be94bb

Please sign in to comment.