44using OrchardCore . Locking ;
55using OrchardCore . Locking . Distributed ;
66using StackExchange . Redis ;
7- using System . Diagnostics ;
87using System . Net ;
98
109namespace OrchardCoreContrib . Garnet . Services ;
@@ -25,9 +24,9 @@ public class GarnetLock(
2524 private static readonly double _baseDelay = 100 ;
2625 private static readonly double _maxDelay = 10000 ;
2726
28- private readonly GarnetOptions _garnetOptions = garnetOptions . Value ;
2927 private readonly string _hostName = Dns . GetHostName ( ) + ':' + Environment . ProcessId ;
3028 private readonly string _prefix = garnetService . InstancePrefix + shellSettings . Name + ':' ;
29+ private readonly ConfigurationOptions _configurationOptions = GarnetOptionsConverter . ConvertToConfigurationOptions ( garnetOptions . Value ) ;
3130
3231 /// <summary>
3332 /// Waits indefinitely until acquiring a named lock with a given expiration for the current tenant
@@ -94,7 +93,7 @@ public async Task<bool> IsLockAcquiredAsync(string key)
9493 try
9594 {
9695 var database = ( await ConnectionMultiplexer
97- . ConnectAsync ( GetConfigurationOptions ( _garnetOptions ) ) )
96+ . ConnectAsync ( _configurationOptions ) )
9897 . GetDatabase ( ) ;
9998
10099 return ( await database . LockQueryAsync ( _prefix + key ) ) . HasValue ;
@@ -124,7 +123,7 @@ private async Task<bool> LockAsync(string key, TimeSpan expiry)
124123 try
125124 {
126125 var database = ( await ConnectionMultiplexer
127- . ConnectAsync ( GetConfigurationOptions ( _garnetOptions ) ) )
126+ . ConnectAsync ( _configurationOptions ) )
128127 . GetDatabase ( ) ;
129128
130129 return await database . LockTakeAsync ( _prefix + key , _hostName , expiry ) ;
@@ -142,7 +141,7 @@ private async ValueTask ReleaseAsync(string key)
142141 try
143142 {
144143 var database = ( await ConnectionMultiplexer
145- . ConnectAsync ( GetConfigurationOptions ( _garnetOptions ) ) )
144+ . ConnectAsync ( _configurationOptions ) )
146145 . GetDatabase ( ) ;
147146
148147 await database . LockReleaseAsync ( _prefix + key , _hostName ) ;
@@ -158,7 +157,7 @@ private void Release(string key)
158157 try
159158 {
160159 var database = ConnectionMultiplexer
161- . ConnectAsync ( GetConfigurationOptions ( _garnetOptions ) )
160+ . ConnectAsync ( _configurationOptions )
162161 . GetAwaiter ( )
163162 . GetResult ( )
164163 . GetDatabase ( ) ;
@@ -206,34 +205,4 @@ private static TimeSpan GetDelay(double retries)
206205
207206 return TimeSpan . FromMilliseconds ( Math . Min ( delay , _maxDelay ) ) ;
208207 }
209-
210- // TODO: Use explicit conversion operators to convert between GarnetOptions and ConfigurationOptions
211- private static ConfigurationOptions GetConfigurationOptions ( GarnetOptions garnetOptions )
212- {
213- var endPoints = new EndPointCollection
214- {
215- new DnsEndPoint ( garnetOptions . Host , garnetOptions . Port )
216- } ;
217- var configOptions = new ConfigurationOptions
218- {
219- EndPoints = endPoints ,
220- ConnectTimeout = ( int ) TimeSpan . FromSeconds ( 2 ) . TotalMilliseconds ,
221- SyncTimeout = ( int ) TimeSpan . FromSeconds ( 30 ) . TotalMilliseconds ,
222- AsyncTimeout = ( int ) TimeSpan . FromSeconds ( 30 ) . TotalMilliseconds ,
223- ReconnectRetryPolicy = new LinearRetry ( ( int ) TimeSpan . FromSeconds ( 10 ) . TotalMilliseconds ) ,
224- ConnectRetry = 5 ,
225- IncludeDetailInExceptions = true ,
226- AbortOnConnectFail = true ,
227- User = garnetOptions . UserName ,
228- Password = garnetOptions . Password
229- } ;
230-
231- if ( Debugger . IsAttached )
232- {
233- configOptions . SyncTimeout = ( int ) TimeSpan . FromHours ( 2 ) . TotalMilliseconds ;
234- configOptions . AsyncTimeout = ( int ) TimeSpan . FromHours ( 2 ) . TotalMilliseconds ;
235- }
236-
237- return configOptions ;
238- }
239208}
0 commit comments