【问题标题】:Issue with S3Client on DigitalOcean - Unable to execute HTTP request: Timeout waiting for connection from poolDigitalOcean 上的 S3Client 问题 - 无法执行 HTTP 请求:等待来自池的连接超时
【发布时间】:2022-09-25 17:03:18
【问题描述】:

在我们的应用程序中,我们有应该保护的附件。这些附件存储在 DO Spaces 中,我们正在生成已签名的 URL 以允许安全访问。

签署 URL 的逻辑如下所示:

String getSignUrl(String objectKey, Long expirationMillis) {
        String signedUrl = null;
        try {
            AmazonS3 s3Client = awsClientProviderService.getS3Client()

            // Set the presigned URL to expire after one hour.
            Date expiration = new Date()
            Long expTimeMillis = expiration.getTime();
            expTimeMillis += expirationMillis;
            expiration.setTime(expTimeMillis);

            GeneratePresignedUrlRequest generatePresignedUrlRequest =
                    new GeneratePresignedUrlRequest(spaceName, objectKey)
                            .withMethod(HttpMethod.GET)
                            .withExpiration(expiration);
            URL url = s3Client.generatePresignedUrl(generatePresignedUrlRequest);

            signedUrl = url.toString()
        } catch (AmazonServiceException e) {
            log.error(e.message, e)
        } catch (SdkClientException e) {
            log.error(e.message, e)
        }

        signedUrl
    }

其中getS3Client 是:

AmazonS3 getS3Client() {
        if (!s3Client) {
            AWSStaticCredentialsProvider credentials =
                    new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secret))
            s3Client = AmazonS3ClientBuilder
                    .standard()
                    .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endpoint, region))
                    .withCredentials(credentials)
                    .build()
        }

        s3Client
    }

到目前为止一切都很好,而且效果很好。

但是,当我们开始对 URL 进行签名时,在 50 个这样的请求之后,我们会收到以下错误:

Unable to execute HTTP request: Timeout waiting for connection from pool

稍微挖掘了一下,我尝试将maxConnections 添加到客户端: .withClientConfiguration(new ClientConfiguration().withMaxConnections(20))

这导致在 20 次此类请求后出现错误。顺便说一句,即使我们先签署 5 个 URL,然后等待 10 分钟,再尝试另一批。该错误发生在总共 20 个请求之后。

试过了,100个连接。 100 个请求后的相同故事。

这让我觉得连接由于某种原因没有被释放。我唯一能找到的是人们提议将maxConnections 增加到 1000。但是,这只会推迟问题。

这里的解决方案是什么?我们如何释放 S3Client 的连接?

    标签: java digital-ocean aws-java-sdk digital-ocean-spaces


    【解决方案1】:

    通常,当池中的所有连接都“忙”并且没有可用于新请求时,会出现“等待连接池连接超时”。

    检查这个问题。

    根据您使用的版本,您可以尝试使用“关闭”方法,如果您的版本有可用的方法。

    https://stackoverflow.com/questions/26866739/how-do-i-close-an-aws-s3-client-connection#:~:text=You%20don't%20need%20to,to%20S3%20when%20using%20AmazonS3Client

    【讨论】:

    • 关闭客户端没有帮助。我需要它保持活跃并正确管理连接。
    【解决方案2】:

    我不知道哪个 API 级别,但方法过期(到期) 当 API 文档指定参数必须是 Java.util.date 时以毫秒填充

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-14
      • 2017-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-25
      相关资源
      最近更新 更多