Skip to content

Commit c6b9419

Browse files
committed
Switch token validity cache to redis
1 parent b7806c6 commit c6b9419

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

src/Service/UpdaterWorker.php

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use App\Entity\User;
1717
use App\SecurityAdvisory\FriendsOfPhpSecurityAdvisoriesSource;
1818
use Composer\Pcre\Preg;
19+
use Psr\Cache\CacheItemPoolInterface;
1920
use Psr\Log\LoggerInterface;
2021
use Composer\Package\Loader\ArrayLoader;
2122
use Composer\Package\Loader\ValidatingArrayLoader;
@@ -24,6 +25,8 @@
2425
use Composer\Repository\InvalidRepositoryException;
2526
use Composer\Repository\VcsRepository;
2627
use Composer\IO\BufferIO;
28+
use Psr\SimpleCache\CacheInterface;
29+
use Symfony\Component\Cache\Psr16Cache;
2730
use Symfony\Component\Console\Output\OutputInterface;
2831
use App\Entity\Package;
2932
use App\Entity\Version;
@@ -62,6 +65,7 @@ class UpdaterWorker
6265
private Scheduler $scheduler;
6366
private PackageManager $packageManager;
6467
private DownloadManager $downloadManager;
68+
private CacheInterface $cache;
6569
/** For use in fixtures loader only */
6670
private bool $loadMinimalVersions = false;
6771

@@ -75,7 +79,9 @@ public function __construct(
7579
DownloadManager $downloadManager,
7680
private StatsDClient $statsd,
7781
private readonly FallbackGitHubAuthProvider $fallbackGitHubAuthProvider,
82+
CacheItemPoolInterface $cache,
7883
) {
84+
$this->cache = new Psr16Cache($cache);
7985
$this->logger = $logger;
8086
$this->doctrine = $doctrine;
8187
$this->updater = $updater;
@@ -127,7 +133,6 @@ public function process(Job $job, SignalHandler $signal): array
127133
$usesPackagistToken = false;
128134
if (Preg::isMatch('{^https://github\.com/(?P<repo>[^/]+/[^/]+?)(?:\.git)?$}i', $package->getRepository(), $matches)) {
129135
$usesPackagistToken = true;
130-
$apc = extension_loaded('apcu');
131136

132137
foreach ($package->getMaintainers() as $maintainer) {
133138
if ($maintainer->getId() === 1) {
@@ -137,26 +142,26 @@ public function process(Job $job, SignalHandler $signal): array
137142
continue;
138143
}
139144

140-
$valid = null;
141-
if ($apc) {
142-
$valid = apcu_fetch('is_token_valid_'.$maintainer->getUsernameCanonical());
143-
}
144-
145-
if (true !== $valid) {
146-
$context = stream_context_create(['http' => ['header' => ['User-agent: packagist-token-check', 'Authorization: token '.$newGithubToken]]]);
147-
$rate = json_decode((string) @file_get_contents('https://api.github.com/repos/'.$matches['repo'].'/git/refs/heads?per_page=1', false, $context), true);
145+
$valid = $this->cache->get('is_token_valid_'.$maintainer->getUsernameCanonical());
146+
if ('1' !== $valid) {
147+
$context = stream_context_create(['http' => [
148+
'header' => ['User-agent: packagist-token-check', 'Authorization: token '.$newGithubToken],
149+
'ignore_errors' => true,
150+
]]);
151+
$rateResponse = json_decode((string) @file_get_contents('https://api.github.com/repos/'.$matches['repo'].'/git/refs/heads?per_page=1', false, $context), true);
148152
// invalid/outdated token, wipe it so we don't try it again
149-
if (!$rate && isset($http_response_header[0]) && (strpos($http_response_header[0], '403') || strpos($http_response_header[0], '401'))) {
150-
$maintainer->setGithubToken(null);
151-
$em->persist($maintainer);
152-
$em->flush();
153-
continue;
153+
if (!$rateResponse || (isset($http_response_header[0]) && Preg::isMatch('{HTTP/\s+ 4[0-9][0-9] }', $http_response_header[0]))) {
154+
if (str_contains($http_response_header[0], '403') || str_contains($http_response_header[0], '401')) {
155+
$this->logger->error('Invalid token check response for '.$maintainer->getUsernameCanonical().' on '.$matches['repo'], ['headers' => $http_response_header, 'response' => $rateResponse]);
156+
$maintainer->setGithubToken(null);
157+
$em->persist($maintainer);
158+
$em->flush();
159+
continue;
160+
}
154161
}
155162
}
156163

157-
if ($apc) {
158-
apcu_store('is_token_valid_'.$maintainer->getUsernameCanonical(), true, 86400);
159-
}
164+
$this->cache->set('is_token_valid_'.$maintainer->getUsernameCanonical(), '1', 86400);
160165

161166
$usesPackagistToken = false;
162167
$io->setAuthentication('github.com', $newGithubToken, 'x-oauth-basic');

0 commit comments

Comments
 (0)