@@ -61,6 +61,8 @@ public partial class RedisManagerPool
6161
6262 public int MaxPoolSize { get ; private set ; }
6363
64+ public bool AssertAccessOnlyOnSameThread { get ; set ; }
65+
6466 public RedisManagerPool ( ) : this ( RedisConfig . DefaultHost ) { }
6567 public RedisManagerPool ( string host ) : this ( new [ ] { host } ) { }
6668 public RedisManagerPool ( string host , RedisPoolConfig config ) : this ( new [ ] { host } , config ) { }
@@ -69,7 +71,7 @@ public RedisManagerPool(IEnumerable<string> hosts) : this(hosts, null) { }
6971 public RedisManagerPool ( IEnumerable < string > hosts , RedisPoolConfig config )
7072 {
7173 if ( hosts == null )
72- throw new ArgumentNullException ( " hosts" ) ;
74+ throw new ArgumentNullException ( nameof ( hosts ) ) ;
7375
7476 RedisResolver = new RedisResolver ( hosts , null ) ;
7577
@@ -83,6 +85,8 @@ public RedisManagerPool(IEnumerable<string> hosts, RedisPoolConfig config)
8385 clients = new RedisClient [ MaxPoolSize ] ;
8486 poolIndex = 0 ;
8587
88+ this . AssertAccessOnlyOnSameThread = RedisConfig . AssertAccessOnlyOnSameThread ;
89+
8690 JsConfig . InitStatics ( ) ;
8791 }
8892
@@ -123,7 +127,7 @@ public void FailoverTo(IEnumerable<string> readWriteHosts, IEnumerable<string> r
123127 {
124128 FailoverTo ( readWriteHosts . ToArray ( ) ) ; //only use readWriteHosts
125129 }
126-
130+
127131 /// <summary>
128132 /// Returns a Read/Write client (The default) using the hosts defined in ReadWriteHosts
129133 /// </summary>
@@ -137,17 +141,18 @@ public IRedisClient GetClient()
137141 {
138142 AssertValidPool ( ) ;
139143
140- RedisClient inActiveClient ;
141144 //-1 when no available clients otherwise index of reservedSlot or existing Client
142- inactivePoolIndex = GetInActiveClient ( out inActiveClient ) ;
145+ inactivePoolIndex = GetInActiveClient ( out var inActiveClient ) ;
143146
144147 //inActiveClient != null only for Valid InActive Clients
145148 if ( inActiveClient != null )
146149 {
147150 poolIndex ++ ;
148151 inActiveClient . Active = true ;
149152
150- return inActiveClient ;
153+ return ! AssertAccessOnlyOnSameThread
154+ ? inActiveClient
155+ : inActiveClient . LimitAccessToThread ( Thread . CurrentThread . ManagedThreadId , Environment . StackTrace ) ;
151156 }
152157 }
153158
@@ -188,7 +193,10 @@ public IRedisClient GetClient()
188193
189194 poolIndex ++ ;
190195 clients [ inactivePoolIndex ] = newClient ;
191- return newClient ;
196+
197+ return ! AssertAccessOnlyOnSameThread
198+ ? newClient
199+ : newClient . LimitAccessToThread ( Thread . CurrentThread . ManagedThreadId , Environment . StackTrace ) ;
192200 }
193201 }
194202 catch
@@ -291,6 +299,7 @@ public void DisposeClient(RedisNativeClient client)
291299 }
292300 else
293301 {
302+ client . TrackThread = null ;
294303 client . Active = false ;
295304 }
296305
0 commit comments