-
Notifications
You must be signed in to change notification settings - Fork 1.3k
CSHARP-5712: withTransaction API retries too frequently #1841
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
1069e25
9577f33
c2cef3b
c5c3dcd
fcc6211
3d13518
2d7db04
ec5819d
e93a147
ae36281
a806040
dccac18
b532afb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /* Copyright 2010-present MongoDB Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| using System.Security.Cryptography; | ||
|
|
||
| namespace MongoDB.Driver.Core.Misc; | ||
|
|
||
| internal sealed class DefaultRandom : IRandom | ||
| { | ||
| public static DefaultRandom Instance { get; } = new DefaultRandom(); | ||
|
|
||
| public string GenerateString(int length, string legalCharacters) | ||
| { | ||
| Ensure.IsGreaterThanOrEqualToZero(length, nameof(length)); | ||
| Ensure.IsNotNullOrEmpty(legalCharacters, nameof(legalCharacters)); | ||
|
|
||
| if (length == 0) | ||
| { | ||
| return string.Empty; | ||
| } | ||
|
|
||
| #if NET472 | ||
| var randomData = new byte[length]; | ||
| using (var rng = RandomNumberGenerator.Create()) | ||
| { | ||
| rng.GetBytes(randomData); | ||
| } | ||
|
|
||
| var sb = new System.Text.StringBuilder(length); | ||
| for (var i = 0; i < length; i++) | ||
| { | ||
| sb.Append(GetResultChar(legalCharacters, randomData[i])); | ||
| } | ||
|
|
||
| return sb.ToString(); | ||
| #else | ||
| return string.Create(length, legalCharacters, (buffer, charset) => | ||
| { | ||
| var randomData = buffer.Length < 1024 ? stackalloc byte[buffer.Length] : new byte[buffer.Length]; | ||
| RandomNumberGenerator.Fill(randomData); | ||
| for (var i = 0; i < buffer.Length; i++) | ||
| { | ||
| buffer[i] = GetResultChar(charset, randomData[i]); | ||
| } | ||
| }); | ||
| #endif | ||
|
|
||
| static char GetResultChar(string charset, byte randomValue) => charset[randomValue % charset.Length]; | ||
| } | ||
|
|
||
| public double NextDouble() | ||
| { | ||
| #if NET6_0_OR_GREATER | ||
| return System.Random.Shared.NextDouble(); | ||
| #else | ||
| return ThreadStaticRandom.NextDouble(); | ||
| #endif | ||
| } | ||
| } | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -108,6 +108,18 @@ public static void AddRetryableWriteErrorLabelIfRequired(MongoException exceptio | |||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| public static int GetRetryDelayMs(IRandom random, int attempt, double backoffBase, int backoffInitial, int backoffMax) | ||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||
| Ensure.IsNotNull(random, nameof(random)); | ||||||||||||||||||||||||||||||||||||
| Ensure.IsGreaterThanZero(attempt, nameof(attempt)); | ||||||||||||||||||||||||||||||||||||
| Ensure.IsGreaterThanZero(backoffBase, nameof(backoffBase)); | ||||||||||||||||||||||||||||||||||||
| Ensure.IsGreaterThanZero(backoffInitial, nameof(backoffInitial)); | ||||||||||||||||||||||||||||||||||||
| Ensure.IsGreaterThan(backoffMax, backoffInitial, nameof(backoffMax)); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| var j = random.NextDouble(); | ||||||||||||||||||||||||||||||||||||
| return (int)(j * Math.Min(backoffMax, backoffInitial * Math.Pow(backoffBase, attempt - 1))); | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
| return (int)(j * Math.Min(backoffMax, backoffInitial * Math.Pow(backoffBase, attempt - 1))); | |
| // compute the largest exponent such that backoffInitial * backoffBase^exponent <= backoffMax | |
| var maxExponent = Math.Log(backoffMax / (double)backoffInitial, backoffBase); | |
| var effectiveExponent = attempt - 1; | |
| double delayWithoutJitter; | |
| if (effectiveExponent >= maxExponent) | |
| { | |
| delayWithoutJitter = backoffMax; | |
| } | |
| else | |
| { | |
| delayWithoutJitter = backoffInitial * Math.Pow(backoffBase, effectiveExponent); | |
| } | |
| return (int)(j * delayWithoutJitter); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!