Skip to content

Commit

Permalink
🚑 add timeout for most interactions with mastodon servers (#1964)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeyemwey authored Oct 7, 2023
1 parent 8e674f3 commit 18d4b7e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 39 deletions.
6 changes: 5 additions & 1 deletion app/Http/Controllers/Backend/Social/MastodonController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Error;
use Exception;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\RequestOptions;
use Illuminate\Support\Facades\Log;
use Laravel\Socialite\Contracts\User as SocialiteUser;
use Revolution\Mastodon\Facades\Mastodon;
Expand Down Expand Up @@ -182,7 +183,7 @@ public static function getEndOfChain(User $user, string $mastodonPostId): ?strin
$client = self::getClient($user);

try {
$context = $client->get('/statuses/' . $mastodonPostId . '/context');
$context = $client->call("GET", "/statuses/{$mastodonPostId}/context", options: self::getRequestOptions());
} catch (GuzzleException $e) {
Log::info("Unable to chain toot because of an issue with the connecting mastodon server.");
if ($e->getCode() == 404) {
Expand Down Expand Up @@ -228,4 +229,7 @@ public static function getLastSavedPostIdFromUserStatuses(User $user) {
->latest()
->first();
}
public static function getRequestOptions(): array {
return [RequestOptions::TIMEOUT => config("trwl.mastodon_timeout_seconds")];
}
}
11 changes: 8 additions & 3 deletions app/Http/Controllers/Backend/Social/MastodonProfileDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function getProfileUrl(): ?string {
}

private function getData(): ?array {
return Cache::remember('mastodon_'.$this->user->username, 6000, function () {
return Cache::remember("mastodon_{$this->user->username}", 60 * 60 /* 1 hour */, function() {
return $this->fetchProfileInformation();
});
}
Expand All @@ -34,12 +34,17 @@ private function fetchProfileInformation(): ?array {
$mastodonServer = MastodonServer::where('id', $this->user->socialProfile->mastodon_server)->first();
if ($mastodonServer) {
return Mastodon::domain($mastodonServer->domain)
->token($this->user->socialProfile->mastodon_token)
->get("/accounts/" . $this->user->socialProfile->mastodon_id);
->token($this->user->socialProfile->mastodon_token)
->call(
method: "GET",
api: "/accounts/" . $this->user->socialProfile->mastodon_id,
options: MastodonController::getRequestOptions()
);
}
} catch (Exception $exception) {
// The connection might be broken, or the instance is down, or $user has removed the api rights
// but has not told us yet.
Log::warning("Unable to fetch mastodon information for user#{$this->user->id} for Mastodon-Server '{$mastodonServer->domain}' and mastodon_id#{$this->user->socialProfile->mastodon_id}");
Log::warning($exception);
}
}
Expand Down
31 changes: 16 additions & 15 deletions config/trwl.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
<?php

return [
'post_social' => env('POST_SOCIAL', false),
'post_social' => env('POST_SOCIAL', false),

# Mastodon
'mastodon_domain' => env('MASTODON_DOMAIN'),
'mastodon_id' => env('MASTODON_ID'),
'mastodon_secret' => env('MASTODON_SECRET'),
'mastodon_redirect' => env('MASTODON_REDIRECT'),
'mastodon_appname' => env('MASTODON_APPNAME'),
'mastodon_domain' => env('MASTODON_DOMAIN'),
'mastodon_id' => env('MASTODON_ID'),
'mastodon_secret' => env('MASTODON_SECRET'),
'mastodon_redirect' => env('MASTODON_REDIRECT'),
'mastodon_appname' => env('MASTODON_APPNAME'),
'mastodon_timeout_seconds' => env("MASTODON_TIMEOUT_SECONDS", 5),

# Brouter
'brouter_url' => env('BROUTER_URL', 'https://brouter.de/'),
'brouter_timeout' => env('BROUTER_TIMEOUT', 10),
'brouter_url' => env('BROUTER_URL', 'https://brouter.de/'),
'brouter_timeout' => env('BROUTER_TIMEOUT', 10),

# DB_REST
'db_rest' => env('DB_REST', 'https://v5.db.transport.rest/'),
'db_rest_timeout' => env('DB_REST_TIMEOUT', 10),
'db_rest' => env('DB_REST', 'https://v5.db.transport.rest/'),
'db_rest_timeout' => env('DB_REST_TIMEOUT', 10),

# Points
'base_points' => [
'base_points' => [
'time_window' => [
# time windows before and after a journey to get points
'good_enough' => [
Expand All @@ -44,13 +45,13 @@
'nationalExpress' => env('BASE_POINTS_TRAIN_NATIONALEXPRESS', 10),
]
],
'refresh' => [
'refresh' => [
'max_trips_per_minute' => env('REFRESH_TRIPS_PER_MINUTE', 1)
],
'cache' => [
'cache' => [
'global-statistics-retention-seconds' => env('GLOBAL_STATISTICS_CACHE_RETENTION_SECONDS', 60 * 60),
'leaderboard-retention-seconds' => env('LEADERBOARD_CACHE_RETENTION_SECONDS', 5 * 60)
],
'year_in_review_active' => env('YEAR_IN_REVIEW_ACTIVE', false),
'webhooks_active' => env('WEBHOOKS_ACTIVE', false),
'year_in_review_active' => env('YEAR_IN_REVIEW_ACTIVE', false),
'webhooks_active' => env('WEBHOOKS_ACTIVE', false),
];
40 changes: 20 additions & 20 deletions tests/Feature/Social/MastodonControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ public function testFindEndOfChainIfThereAreNoAnswers(): void {
* - end.
*/

Mastodon::shouldReceive('get')
Mastodon::shouldReceive('call')
->once()
->with(self::OP_CONTEXT_URL)
->with("GET", self::OP_CONTEXT_URL, MastodonController::getRequestOptions())
->andReturn(["descendants" => []]);

$this->assertEquals(self::TOOTID_OP, MastodonController::getEndOfChain($user, self::TOOTID_OP));
Expand All @@ -75,9 +75,9 @@ public function testFindEndOfChainIfThereIsOneAnswerFromOtherPerson(): void {
* - toodid-answer from userid-answer
*/

Mastodon::shouldReceive('get')
Mastodon::shouldReceive('call')
->once()
->with(self::OP_CONTEXT_URL)
->with("GET", self::OP_CONTEXT_URL, MastodonController::getRequestOptions())
->andReturn(
[
"descendants" => [
Expand Down Expand Up @@ -105,9 +105,9 @@ public function testFindEndOfChainIfThereIsAConversationWithAnotherPerson(): voi
* - tootid-answer2 from userid-op
*/

Mastodon::shouldReceive('get')
Mastodon::shouldReceive('call')
->once()
->with(self::OP_CONTEXT_URL)
->with("GET", self::OP_CONTEXT_URL, MastodonController::getRequestOptions())
->andReturn(
[
"descendants" => [
Expand Down Expand Up @@ -142,9 +142,9 @@ public function testFindEndOfChainIfThereIsAThreadWithOnePost(): void {
* - tootid-answer from userid-op <== THIS ONE
*/

Mastodon::shouldReceive('get')
Mastodon::shouldReceive('call')
->once()
->with(self::OP_CONTEXT_URL)
->with("GET", self::OP_CONTEXT_URL, MastodonController::getRequestOptions())
->andReturn(
[
"descendants" => [
Expand All @@ -171,9 +171,9 @@ public function testFindEndOfChainIfThereIsAThreadWithTwoPostsAndSomeoneIsMentio
* - tootid-answer from userid-op with mention of userid-bob <== THIS ONE
*/

Mastodon::shouldReceive('get')
Mastodon::shouldReceive('call')
->once()
->with(self::OP_CONTEXT_URL)
->with("GET", self::OP_CONTEXT_URL, MastodonController::getRequestOptions())
->andReturn(
[
"descendants" => [
Expand All @@ -200,9 +200,9 @@ public function testFindEndOfChainIfThereIsAThreadWithTwoPostsAndSomeoneIsMentio
* - tootid-answer from userid-op with mention of userid-bob which is a DM
*/

Mastodon::shouldReceive('get')
Mastodon::shouldReceive('call')
->once()
->with(self::OP_CONTEXT_URL)
->with("GET", self::OP_CONTEXT_URL, MastodonController::getRequestOptions())
->andReturn(
[
"descendants" => [
Expand Down Expand Up @@ -230,9 +230,9 @@ public function testFindEndOfChainIfThereIsAThreadWithMultiplePosts(): void {
* - tootid-answer2 from userid-op <== THIS ONE
*/

Mastodon::shouldReceive('get')
Mastodon::shouldReceive('call')
->once()
->with(self::OP_CONTEXT_URL)
->with("GET", self::OP_CONTEXT_URL, MastodonController::getRequestOptions())
->andReturn(
[
"descendants" => [
Expand Down Expand Up @@ -271,9 +271,9 @@ public function testFindEndOfChainIfThereIsAThreadWithMultiplePostsAndSomeAnswer
* - tootid-answer3 from userid-answer
*/

Mastodon::shouldReceive('get')
Mastodon::shouldReceive('call')
->once()
->with(self::OP_CONTEXT_URL)
->with("GET", self::OP_CONTEXT_URL, MastodonController::getRequestOptions())
->andReturn(
[
"descendants" => [
Expand Down Expand Up @@ -319,9 +319,9 @@ public function testFindEndOfChainIfOriginalPostIsNotFound() {
* Original post is deleted.
*/

Mastodon::shouldReceive('get')
Mastodon::shouldReceive('call')
->once()
->with(self::OP_CONTEXT_URL)
->with("GET", self::OP_CONTEXT_URL, MastodonController::getRequestOptions())
->andThrowExceptions([new ClientException(
'{"error":"Record not found"}',
new Request('GET', self::OP_CONTEXT_URL),
Expand All @@ -340,9 +340,9 @@ public function testFindEndOfChainIfMastodonServerUnreachable() {
* Original post is deleted.
*/

Mastodon::shouldReceive('get')
Mastodon::shouldReceive('call')
->once()
->with(self::OP_CONTEXT_URL)
->with("GET", self::OP_CONTEXT_URL, MastodonController::getRequestOptions())
->andThrowExceptions([new ConnectException("server not available",
new Request('GET', self::OP_CONTEXT_URL)
)]);
Expand Down

0 comments on commit 18d4b7e

Please sign in to comment.