@@ -9,6 +9,11 @@ internal class RedisSentinelWorker : IDisposable
99 {
1010 protected static readonly ILog Log = LogManager . GetLogger ( typeof ( RedisSentinelWorker ) ) ;
1111
12+ static int IdCounter = 0 ;
13+ public int Id { get ; }
14+
15+ private readonly object oLock = new object ( ) ;
16+
1217 private readonly RedisSentinel sentinel ;
1318 private readonly RedisClient sentinelClient ;
1419 private RedisPubSubServer sentinePubSub ;
@@ -17,6 +22,7 @@ internal class RedisSentinelWorker : IDisposable
1722
1823 public RedisSentinelWorker ( RedisSentinel sentinel , RedisEndpoint sentinelEndpoint )
1924 {
25+ this . Id = Interlocked . Increment ( ref IdCounter ) ;
2026 this . sentinel = sentinel ;
2127 this . sentinelClient = new RedisClient ( sentinelEndpoint ) {
2228 Db = 0 , //Sentinel Servers doesn't support DB, reset to 0
@@ -95,7 +101,10 @@ internal string GetMasterHost(string masterName)
95101
96102 private string GetMasterHostInternal ( string masterName )
97103 {
98- var masterInfo = sentinelClient . SentinelGetMasterAddrByName ( masterName ) ;
104+ List < string > masterInfo ;
105+ lock ( oLock )
106+ masterInfo = sentinelClient . SentinelGetMasterAddrByName ( masterName ) ;
107+
99108 return masterInfo . Count > 0
100109 ? SanitizeMasterConfig ( masterInfo )
101110 : null ;
@@ -114,12 +123,21 @@ private string SanitizeMasterConfig(List<string> masterInfo)
114123
115124 internal List < string > GetSentinelHosts ( string masterName )
116125 {
117- return SanitizeHostsConfig ( this . sentinelClient . SentinelSentinels ( sentinel . MasterName ) ) ;
126+ List < Dictionary < string , string > > sentinelSentinels ;
127+ lock ( oLock )
128+ sentinelSentinels = this . sentinelClient . SentinelSentinels ( sentinel . MasterName ) ;
129+
130+ return SanitizeHostsConfig ( sentinelSentinels ) ;
118131 }
119132
120133 internal List < string > GetSlaveHosts ( string masterName )
121134 {
122- return SanitizeHostsConfig ( this . sentinelClient . SentinelSlaves ( sentinel . MasterName ) ) ;
135+ List < Dictionary < string , string > > sentinelSlaves ;
136+
137+ lock ( oLock )
138+ sentinelSlaves = sentinelClient . SentinelSlaves ( sentinel . MasterName ) ;
139+
140+ return SanitizeHostsConfig ( sentinelSlaves ) ;
123141 }
124142
125143 private List < string > SanitizeHostsConfig ( IEnumerable < Dictionary < string , string > > slaves )
@@ -146,27 +164,30 @@ public void BeginListeningForConfigurationChanges()
146164 {
147165 try
148166 {
149- if ( this . sentinePubSub == null )
167+ lock ( oLock )
150168 {
151- var sentinelManager = new BasicRedisClientManager ( sentinel . SentinelHosts , sentinel . SentinelHosts )
169+ if ( this . sentinePubSub == null )
152170 {
153- //Use BasicRedisResolver which doesn't validate non-Master Sentinel instances
154- RedisResolver = new BasicRedisResolver ( sentinel . SentinelEndpoints , sentinel . SentinelEndpoints )
155- } ;
156- this . sentinePubSub = new RedisPubSubServer ( sentinelManager )
157- {
158- HeartbeatInterval = null ,
159- IsSentinelSubscription = true ,
160- ChannelsMatching = new [ ] { RedisPubSubServer . AllChannelsWildCard } ,
161- OnMessage = SentinelMessageReceived
162- } ;
171+ var sentinelManager = new BasicRedisClientManager ( sentinel . SentinelHosts , sentinel . SentinelHosts )
172+ {
173+ //Use BasicRedisResolver which doesn't validate non-Master Sentinel instances
174+ RedisResolver = new BasicRedisResolver ( sentinel . SentinelEndpoints , sentinel . SentinelEndpoints )
175+ } ;
176+ this . sentinePubSub = new RedisPubSubServer ( sentinelManager )
177+ {
178+ HeartbeatInterval = null ,
179+ IsSentinelSubscription = true ,
180+ ChannelsMatching = new [ ] { RedisPubSubServer . AllChannelsWildCard } ,
181+ OnMessage = SentinelMessageReceived
182+ } ;
183+ }
163184 }
185+
164186 this . sentinePubSub . Start ( ) ;
165187 }
166188 catch ( Exception ex )
167189 {
168- Log . Error ( "Error Subscribing to Redis Channel on {0}:{1}"
169- . Fmt ( this . sentinelClient . Host , this . sentinelClient . Port ) , ex ) ;
190+ Log . Error ( $ "Error Subscribing to Redis Channel on { this . sentinelClient . Host } :{ this . sentinelClient . Port } ", ex ) ;
170191
171192 if ( OnSentinelError != null )
172193 OnSentinelError ( ex ) ;
@@ -175,7 +196,8 @@ public void BeginListeningForConfigurationChanges()
175196
176197 public void ForceMasterFailover ( string masterName )
177198 {
178- this . sentinelClient . SentinelFailover ( masterName ) ;
199+ lock ( oLock )
200+ this . sentinelClient . SentinelFailover ( masterName ) ;
179201 }
180202
181203 public void Dispose ( )
0 commit comments