将Symfony 6.2升级到6.3后的Varnish缓存问题(通过Fastly的Google Cloud)

uz75evzq  于 2023-11-22  发布在  Go
关注(0)|答案(2)|浏览(151)

我已经将我的Symfony应用从6.2升级到6.3。本地一切都很完美,但我在暂存环境上遇到了问题。当我试图通过API登录(或获取一些资源)时,我从Varnish Cache Server收到了503 First Byte Error。在Kubernetes日志中,一切都很好(响应状态代码为204,并且存在头)。
我已经改变了一些Normalizer类来删除弃用,例如UserNormalizer
升级前版本:

<?php

declare(strict_types=1);

namespace App\Api\User\Serializer;

use App\Entity\User;
use App\Repository\CategoryRepository;
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

class UserNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface
{
    public function __construct(
        private readonly NormalizerInterface $itemNormalizer,
        private readonly CategoryRepository $categoryRepository,
    ) {
    }

    /** @param User $object */
    public function normalize($object, $format = null, array $context = []): array|string
    {
        $object->categoryIds = $this->categoryRepository->findIdsWithJobsByUser($object);

        return $this->itemNormalizer->normalize($object, $format, $context);
    }

    public function supportsNormalization($data, $format = null, array $context = []): bool
    {
        return $data instanceof User;
    }

    public function hasCacheableSupportsMethod(): bool
    {
        return true;
    }
}

字符串
升级后

<?php

declare(strict_types=1);

namespace App\Api\User\Serializer;

use App\Entity\User;
use App\Repository\CategoryRepository;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

final readonly class UserNormalizer implements NormalizerInterface
{
    public function __construct(
        private NormalizerInterface $itemNormalizer,
        private CategoryRepository $categoryRepository,
    ) {
    }

    /** @param User $object */
    public function normalize($object, $format = null, array $context = []): array|string
    {
        $object->categoryIds = $this->categoryRepository->findIdsWithJobsByUser($object);

        return $this->itemNormalizer->normalize($object, $format, $context);
    }

    public function supportsNormalization($data, $format = null, array $context = []): bool
    {
        return $data instanceof User;
    }

    public function getSupportedTypes(?string $format): array
    {
        return [
            '*' => false,
            User::class => true,
        ];
    }
}


我已经删除了CacheableSupportsMethodInterface的实现(使用方法hasCacheableSupportsMethod()),并根据文档和built-in normalizer将其替换为getSupportedTypes()
我的应用程序在Google Kubernetes Engine(GCP)上运行,并通过Fastly交付。
有人遇到过同样的问题吗?
PS.一些资源是正确的(例如静态页面,也被缓存,并有类似的getSupportedTypes()实现)。

klr1opcd

klr1opcd1#

Fastly允许您配置“到第一个字节的时间”超时.

默认值是15秒,这(对我来说)听起来应该足够了。所以我还建议调查任何可能减慢响应的网络连接问题。

ux6nzvsh

ux6nzvsh2#

解决了
问题出在pcntl扩展上,这是Messenger版本6.3.5所必需的。
在更新信使到6.3.7后,我从我的Docker映像中删除了pcntl扩展,所有缓存问题都消失了。

相关问题