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

问题描述 投票:0回答:1

我已将我的 Symfony 应用程序从 6.2 升级到 6.3。 本地一切工作都很完美,但我在暂存环境上遇到问题。 当我尝试通过 API 登录(或获取一些资源)时,我从 Varnish 缓存服务器收到503 First Byte Timeout。 在 Kubernetes 日志中,一切正常(响应状态代码为 204 并且存在标头)。

我更改了一些规范化器类以删除弃用的内容,例如。用户标准化器

升级前版本:

<?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()
),并根据
文档
内置标准化器将其替换为getSupportedTypes()

我的应用程序在 Google Kubernetes Engine (GCP) 上运行,并通过 Fastly 交付。

有人遇到同样的问题吗?

PS。一些资源是正确的(例如静态页面,它们也被缓存并且具有类似的

getSupportedTypes()
实现)。

php symfony google-kubernetes-engine symfony6 fastly
1个回答
0
投票

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

默认值为 15 秒,(对我来说)听起来应该足够了。因此,我还建议调查任何可能会减慢响应速度的网络连接问题。

© www.soinside.com 2019 - 2024. All rights reserved.