Skip to content

Instantly share code, notes, and snippets.

@BackEndTea
Created June 17, 2021 09:18
Show Gist options
  • Save BackEndTea/10b123f1cc3164a23ffe5a25b90f2640 to your computer and use it in GitHub Desktop.
Save BackEndTea/10b123f1cc3164a23ffe5a25b90f2640 to your computer and use it in GitHub Desktop.
<?php
function connect_while(ApiClient $client): string
{
$tries = 0;
while(true) {
$tries++;
try {
$result = $client->doThing();
break;
}catch(ApiException $e) {
if($tries === 10) {
throw $e;
}
echo "error with api, trying again";
sleep(5);
}
}
return process($result);
}
function connect_goto(ApiClient $client): string
{
$tries = 0;
start:
$tries++;
try {
$result = $client->doThing();
break;
} catch(ApiException $e) {
if($tries === 10) {
throw $e;
}
echo "error with api, trying again";
sleep(5);
goto start;
}
return process($result);
}
@zerkms
Copy link

zerkms commented Jun 17, 2021

function connect_maybe(ApiClient $client): ???
{
  $tries = 0;
  while(true) {
    $tries++;
    try {
      return $client->doThing();
    }catch(ApiException $e) {
      if($tries === 10) {
        throw $e;
      }
      echo "error with api, trying again";
      sleep(5);
    }
  }
}

function connect_while(ApiClient $client): string
{
  return process(connect_maybe($client));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment