Skip to content

Commit

Permalink
Fix client caching with app installation tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyberboss committed Sep 1, 2024
1 parent 29993cf commit 02dbc29
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/Tgstation.Server.Host/Utils/GitHub/GitHubClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ namespace Tgstation.Server.Host.Utils.GitHub
sealed class GitHubClientFactory : IGitHubClientFactory, IDisposable
{
/// <summary>
/// Limit to the amount of days a <see cref="GitHubClient"/> can live in the <see cref="clientCache"/>.
/// Limit to the amount of hours a <see cref="GitHubClient"/> can live in the <see cref="clientCache"/>.
/// </summary>
/// <remarks>God forbid someone leak server memory by constantly changing an instance's GitHub token.</remarks>
const uint ClientCacheDays = 7;
/// <remarks>Set to app installation token lifetime, which is the lowest. See https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/generating-an-installation-access-token-for-a-github-app.</remarks>
const uint ClientCacheHours = 1;

/// <summary>
/// The <see cref="clientCache"/> <see cref="KeyValuePair{TKey, TValue}.Key"/> used in place of <see langword="null"/> when accessing a configuration-based client with no token set in <see cref="GeneralConfiguration.GitHubAccessToken"/>.
Expand Down Expand Up @@ -220,7 +220,7 @@ public async ValueTask<IGitHubClient> CreateClient(string accessToken, Cancellat

// Prune the cache
var purgeCount = 0U;
var purgeAfter = now.AddDays(-ClientCacheDays);
var purgeAfter = now.AddHours(-ClientCacheHours);
foreach (var key in clientCache.Keys.ToList())
{
if (key == cacheKey)
Expand All @@ -236,9 +236,9 @@ public async ValueTask<IGitHubClient> CreateClient(string accessToken, Cancellat

if (purgeCount > 0)
logger.LogDebug(
"Pruned {count} expired GitHub client(s) from cache that haven't been used in {purgeAfterHours} days.",
"Pruned {count} expired GitHub client(s) from cache that haven't been used in {purgeAfterHours} hours.",
purgeCount,
ClientCacheDays);
ClientCacheHours);
}

var rateLimitInfo = client.GetLastApiInfo()?.RateLimit;
Expand Down

0 comments on commit 02dbc29

Please sign in to comment.