add connect/request timeout + options

This commit is contained in:
Irfan 2020-07-10 17:07:16 +05:00
parent f6de29826d
commit bf789210fb
3 changed files with 23 additions and 1 deletions

View File

@ -37,6 +37,12 @@ SOURCE_BAD_HEALTH_RECHECK=10
SOURCE_BAD_HEALTH_RANGE=30
# Max Fail stores
SOURCE_BAD_HEALTH_MAX_STORE=50
# Max time request is allowed to take
# https://curl.haxx.se/libcurl/c/CURLOPT_TIMEOUT.html
SOURCE_TIMEOUT=5
# Timeout for connect phase
# https://curl.haxx.se/libcurl/c/CURLOPT_CONNECTTIMEOUT.html
SOURCE_CONNECT_TIMEOUT=5
###
# Caching (File, Redis, etc)

View File

@ -6,6 +6,7 @@ use App\Events\SourceHealthEvent;
use App\Http\HttpHelper;
use Exception;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ConnectException;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\Request;
@ -75,6 +76,16 @@ class Handler extends ExceptionHandler
], 500);
}
if ($e instanceof ConnectException) {
return response()
->json([
'status' => $e->getCode(),
'type' => 'BadResponseException',
'message' => 'Jikan failed to connect to MyAnimeList. MyAnimeList may be down/unavailable or refuses to connect',
'error' => $e->getMessage()
], 503);
}
// ParserException from Jikan PHP API
if ($e instanceof ParserException) {
$githubReport->setRepo(env('GITHUB_API', 'jikan-me/jikan'));
@ -88,6 +99,8 @@ class Handler extends ExceptionHandler
], 500);
}
// BadResponseException from Guzzle dep via Jikan PHP API
// This is basically the response MyAnimeList returns to Jikan
if ($e instanceof BadResponseException || $e instanceof ClientException) {

View File

@ -106,7 +106,10 @@ if (env('CACHING')) {
$app->register(Flipbox\LumenGenerator\LumenGeneratorServiceProvider::class);
$guzzleClient = new \GuzzleHttp\Client();
$guzzleClient = new \GuzzleHttp\Client([
'timeout' => env('SOURCE_TIMEOUT', 5),
'connect_timeout' => env('SOURCE_CONNECT_TIMEOUT', 5)
]);
$app->instance('GuzzleClient', $guzzleClient);
$jikan = new \Jikan\MyAnimeList\MalClient(app('GuzzleClient'));