Skip to content

Commit 3f5386e

Browse files
Improve redis connection management (#79)
1 parent c96da56 commit 3f5386e

2 files changed

Lines changed: 38 additions & 20 deletions

File tree

src/ByteSync.Client/Services/Communications/SafetyWordsComputer.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
namespace ByteSync.Services.Communications;
55

66
/// <summary>
7-
/// Cette classe permet de déterminer une liste de mots à partir d'un MD5.
8-
/// Elle se base sur mnemonicode pour associer des mots au MD5.
7+
/// This class allows you to determine a list of words from an MD5.
8+
/// It is based on mnemonicode to associate words with MD5.
99
/// Infos : https://github.com/singpolyma/mnemonicode // https://github.com/singpolyma/mnemonicode/blob/master/mn_wordlist.c
1010
/// </summary>
1111
public class SafetyWordsComputer
@@ -19,7 +19,7 @@ public SafetyWordsComputer(string[] availableWords)
1919

2020
public string[] Compute(string hexInput)
2121
{
22-
// On contrôle la valeur entrante
22+
// The incoming value is checked.
2323
if (hexInput.IsNullOrEmpty())
2424
{
2525
throw new ArgumentOutOfRangeException(nameof(hexInput), "input can not be empty");
@@ -29,17 +29,17 @@ public string[] Compute(string hexInput)
2929
throw new ArgumentOutOfRangeException(nameof(hexInput), "wrong input format");
3030
}
3131

32-
// On convertit hexInput en decimal
33-
// On détermine combien de mots seront nécessaires pour couvrir la
34-
// Puis on le convertit en base 1633 / mnemonicode en effectuant une division
35-
// On complète éventuellement avec les mots manquants
36-
37-
// Précédemment, on découpait la chaîne en groupes de 4 caractères, mais cela entraînait des sauts lors de la division
38-
// et diminuait la qualité de la conversion
32+
// Convert hexInput to decimal
33+
// We determine how many words will be needed to cover the
34+
// Then we convert it to base 1633 / mnemonicode by performing a division
35+
// We fill in any missing words
36+
37+
// Previously, we split the string into groups of 4 characters, but this caused jumps during division
38+
// and reduced the quality of the conversion
3939

4040
var result = new List<string>();
4141

42-
// On calcule combien de valeurs possibles existent en fonction de la longueur de la chaîne en entrée
42+
// We calculate how many possible values exist based on the length of the input string
4343
var hexaInputPossibleValues = Math.Pow(16, hexInput.Length);
4444
var coverWordCount = 1;
4545
while (Math.Pow(AvailableWords.Length, coverWordCount) < hexaInputPossibleValues)
@@ -51,9 +51,9 @@ public string[] Compute(string hexInput)
5151
string word;
5252
while (quotient > AvailableWords.Length)
5353
{
54-
// On procède à des divisions successives pour le changement de base
55-
// Le reste est ajouté à la liste, le quotient est ensuite redivisé
56-
// On continu tant que "quotient est divisable", c'est à dire tant que "quotient > AvailableWords.Length"
54+
// Successive divisions are performed to change the base
55+
// The remainder is added to the list, and the quotient is then divided again
56+
// This is continued as long as “quotient is divisible,” i.e., as long as “quotient > AvailableWords.Length
5757

5858
var modulo = (int) (quotient % AvailableWords.Length);
5959
word = AvailableWords[modulo];
@@ -62,18 +62,18 @@ public string[] Compute(string hexInput)
6262
quotient = quotient / AvailableWords.Length;
6363
}
6464

65-
// A la fin, on ajoute le dernier quotient à la liste
65+
// At the end, add the last quotient to the list
6666
word = AvailableWords[(int) quotient];
6767
result.Add(word);
6868

69-
// Si le nombre en entrée est trop petit, on n'a pas atteint le nombre de mots attendu
70-
// On complète
69+
// If the number entered is too small, the expected number of words has not been reached
70+
// Complete
7171
while (result.Count < coverWordCount)
7272
{
7373
result.Add(AvailableWords[0]);
7474
}
7575

76-
// On a ajouté les mots à l'envers, on retourne la liste
76+
// We added the words backwards, then reversed the list
7777
result.Reverse();
7878

7979
return result.ToArray();

src/ByteSync.ServerCommon/Services/RedisInfrastructureService.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,18 @@ public class RedisInfrastructureService : IRedisInfrastructureService
1717
{
1818
private readonly RedisSettings _redisSettings;
1919
private readonly ICacheKeyFactory _cacheKeyFactory;
20-
private readonly ConnectionMultiplexer _connectionMultiplexer;
2120
private readonly RedLockFactory _redLockFactory;
21+
private static string? _cachedConnectionString;
22+
private readonly ConnectionMultiplexer _connectionMultiplexer;
2223

2324
public RedisInfrastructureService(IOptions<RedisSettings> redisSettings, ICacheKeyFactory cacheKeyFactory, ILoggerFactory loggerFactory)
2425
{
2526
_redisSettings = redisSettings.Value;
2627
_cacheKeyFactory = cacheKeyFactory;
2728

28-
_connectionMultiplexer = ConnectionMultiplexer.Connect(_redisSettings.ConnectionString);
29+
_cachedConnectionString ??= _redisSettings.ConnectionString;
30+
31+
_connectionMultiplexer = _lazyMultiplexer.Value;
2932

3033
var multiplexers = new List<RedLockMultiplexer>
3134
{
@@ -35,6 +38,21 @@ public RedisInfrastructureService(IOptions<RedisSettings> redisSettings, ICacheK
3538
RedLockRetryConfiguration redLockRetryConfiguration = new RedLockRetryConfiguration(5, 500);
3639
_redLockFactory = RedLockFactory.Create(multiplexers, redLockRetryConfiguration, loggerFactory);
3740
}
41+
42+
private static readonly Lazy<ConnectionMultiplexer> _lazyMultiplexer = new(() =>
43+
{
44+
var options = ConfigurationOptions.Parse(_cachedConnectionString!);
45+
46+
options.Ssl = true;
47+
options.AbortOnConnectFail = false;
48+
49+
if (options.ConnectTimeout < 10000)
50+
{
51+
options.ConnectTimeout = 10000;
52+
}
53+
54+
return ConnectionMultiplexer.Connect(options);
55+
});
3856

3957
public ITransaction OpenTransaction()
4058
{

0 commit comments

Comments
 (0)