目录
Reyhan Koyun

Fix blocking sleep and incorrect backoff durations in RefreshNowAsync (#196)

Description

Why is this change being made?

  1. RefreshNowAsync uses Thread.Sleep which blocks a thread pool thread, causing thread pool starvation and ignoring the CancellationToken.
  2. exceptionCount is never incremented, so exponential backoff never grows — every retry uses the same base delay.
  3. .Milliseconds (sub-second component only, 0–999) is used instead of the full TimeSpan, truncating intended ~7s delays to ~400ms.
  4. Environment.TickCount (32-bit signed int) overflows after ~24.9 days, causing incorrect retry timing in long-running services.

What is changing?

  1. Replace Thread.Sleep with await Task.Delay(TimeSpan, CancellationToken) so the wait is non-blocking and cancellable.
  2. Increment exceptionCount in the catch block so backoff actually grows exponentially.
  3. Use TimeSpan directly instead of extracting .Milliseconds, fixing the truncated delay durations.
  4. Replace Environment.TickCount arithmetic with DateTime.UtcNow + TimeSpan for overflow-safe timing.
  • Issue #, if available: N/A

Testing

How was this tested?

  1. Added RefreshNowAsyncRespectsTokenCancellation — verifies cancellation is immediate (<100ms) during the jitter delay.
  2. Added RefreshNowAsyncAfterExceptionWithExpiredBackoff — covers the branch where nextRetryTime is in the past (wait is negative), confirming recovery works.
  3. Added RefreshNowAsyncAfterExceptionWithActiveBackoff — covers the branch where backoff wait exceeds base jitter, verified via cancellation.
  4. All 17 existing tests pass.

When testing locally, provide testing artifact(s):

  1. dotnet test — all tests pass including the 3 new ones.

Reviewee Checklist

Update the checklist after submitting the PR

  • I have reviewed, tested and understand all changes If not, why:
  • I have filled out the Description and Testing sections above If not, why:
  • Build and Unit tests are passing If not, why:
  • Unit test coverage check is passing If not, why:
  • Integration tests pass locally If not, why: No integration test suite in this repository.
  • I have updated integration tests (if needed)

If not, why: Not applicable — changes are to caching internals, no integration tests exist.

  • I have ensured no sensitive information is leaking (i.e., no logging of sensitive fields, or otherwise) If not, why:
  • I have added explanatory comments for complex logic, new classes/methods and new tests If not, why:
  • I have updated README/documentation (if needed)

If not, why: No public API changes; behavioral change (longer jitter delay) is noted in the PR description.

  • I have clearly called out breaking changes (if any)

If not, why: Behavioral change: RefreshNowAsync now waits the originally intended ~7s jitter (was ~400ms due to the .Milliseconds bug). This is a correction, not a regression.


Reviewer Checklist

All reviewers please ensure the following are true before reviewing:

  • Reviewee checklist has been accurately filled out
  • Code changes align with stated purpose in description
  • Test coverage adequately validates the changes

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.


Co-authored-by: Simon Marty simon.marty0@gmail.com

18天前73次提交

AWS Secrets Manager Caching Client for .NET

NuGet .NET codecov

The AWS Secrets Manager caching client enables in-process caching of secrets for .NET applications.

Required Prerequisites

To use this client, you must have:

  • A .NET project with one of the following:

    • .NET Framework 4.6.2 or higher
    • .NET Standard 2.0 or higher
  • An Amazon Web Services (AWS) account to access secrets stored in AWS Secrets Manager and use AWS SDK for .NET.

    • To create an AWS account, go to Sign In or Create an AWS Account and then choose I am a new user. Follow the instructions to create an AWS account.

    • To create a secret in AWS Secrets Manager, go to Creating Secrets and follow the instructions on that page.

    • To download and install the AWS SDK for .NET, go to Installing the AWS SDK for .NET in the AWS SDK for .NET documentation and then follow the instructions on that page.

Download

You can get the latest release from Nuget:

<ItemGroup>
     <PackageReference Include="AWSSDK.SecretsManager.Caching" Version="2.*" />
</ItemGroup>

Getting Started

The following code sample demonstrates how to start using the caching client:

using System;
using Amazon.SecretsManager.Extensions.Caching.SecretsManagerCache;

namespace LambdaExample {
    public class CachingExample 
    {
        private SecretsManagerCache cache = new SecretsManagerCache();
        private const String MySecretName = "MySecret";

        public async Task<Response> FunctionHandlerAsync(String input, ILambdaContext context)
        {
            String MySecret = await cache.GetSecretString(MySecretName);
            ...
        }
    }
}
  • After instantiating the cache, retrieve your secret using GetSecretString or GetSecretBinary.
  • On successive retrievals, the cache will return the cached copy of the secret.
  • Learn more about AWS Lambda Function Handlers in C#.

Cache Configuration

You can configure the SecretCacheConfiguration object with the following parameters:

  • CacheItemTTL - The TTL of a Cache item in milliseconds. The default value is 3600000 ms, or 1 hour.
  • MaxCacheSize - The maximum number of items the Cache can contain before evicting using LRU. The default value is 1024.
  • VersionStage - The Version Stage the Cache will request when retrieving secrets from Secrets Manager. The default value is AWSCURRENT.
  • Client - The Secrets Manager client to be used by the Cache. The default value is null, which causes the Cache to instantiate a new Secrets Manager client.
  • CacheHook - An implementation of the ISecretCacheHook interface. The default value is null.

Post-Quantum TLS Support

This library automatically supports Post-Quantum TLS when available on the underlying platform:

  • macOS: Requires .NET 10+ and export DOTNET_SYSTEM_NET_SECURITY_USENETWORKFRAMEWORK=1
  • Linux: Works with OpenSSL 3.5+
  • Windows: Not yet supported

Getting Help

We use GitHub issues for tracking bugs and caching library feature requests and have limited bandwidth to address them. Please use these community resources for getting help:

License

This library is licensed under the Apache 2.0 License.

关于
583.0 KB
邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

版权所有:中国计算机学会技术支持:开源发展技术委员会
京ICP备13000930号-9 京公网安备 11010802047560号