@@ -11,12 +11,12 @@ internal static class BackgroundConnectionResetHelper
11
11
{
12
12
public static void AddSession ( ServerSession session , MySqlConnection ? owningConnection )
13
13
{
14
- var resetTask = session . TryResetConnectionAsync ( session . Pool ! . ConnectionSettings , IOBehavior . Asynchronous , default ) ;
14
+ var resetTask = session . TryResetConnectionAsync ( session . Pool ! . ConnectionSettings , owningConnection , true , IOBehavior . Asynchronous , default ) ;
15
15
lock ( s_lock )
16
- s_sessions . Add ( new SessionResetTask ( session , resetTask , owningConnection ) ) ;
16
+ s_resetTasks . Add ( resetTask ) ;
17
17
18
18
if ( Log . IsDebugEnabled ( ) )
19
- Log . Debug ( "Started Session{0} reset in background; waiting SessionCount : {1}." , session . Id , s_sessions . Count ) ;
19
+ Log . Debug ( "Started Session{0} reset in background; waiting TaskCount : {1}." , session . Id , s_resetTasks . Count ) ;
20
20
21
21
// release only if it is likely to succeed
22
22
if ( s_semaphore . CurrentCount == 0 )
@@ -69,7 +69,6 @@ public static async Task ReturnSessionsAsync()
69
69
Log . Info ( "Started BackgroundConnectionResetHelper worker." ) ;
70
70
71
71
List < Task < bool > > localTasks = new ( ) ;
72
- List < SessionResetTask > localSessions = new ( ) ;
73
72
74
73
// keep running until stopped
75
74
while ( ! s_cancellationTokenSource . IsCancellationRequested )
@@ -85,36 +84,18 @@ public static async Task ReturnSessionsAsync()
85
84
{
86
85
lock ( s_lock )
87
86
{
88
- if ( s_sessions . Count == 0 )
89
- {
90
- if ( localTasks . Count == 0 )
91
- break ;
92
- }
93
- else
94
- {
95
- foreach ( var session in s_sessions )
96
- {
97
- localSessions . Add ( session ) ;
98
- localTasks . Add ( session . ResetTask ) ;
99
- }
100
- s_sessions . Clear ( ) ;
101
- }
87
+ localTasks . AddRange ( s_resetTasks ) ;
88
+ s_resetTasks . Clear ( ) ;
102
89
}
103
90
91
+ if ( localTasks . Count == 0 )
92
+ break ;
93
+
104
94
if ( Log . IsDebugEnabled ( ) )
105
- Log . Debug ( "Found SessionCount {0} session (s) to return ." , localSessions . Count ) ;
95
+ Log . Debug ( "Found TaskCount {0} task (s) to process ." , localTasks . Count ) ;
106
96
107
- while ( localTasks . Count != 0 )
108
- {
109
- var completedTask = await Task . WhenAny ( localTasks ) . ConfigureAwait ( false ) ;
110
- var index = localTasks . IndexOf ( completedTask ) ;
111
- var session = localSessions [ index ] . Session ;
112
- var connection = localSessions [ index ] . OwningConnection ;
113
- localSessions . RemoveAt ( index ) ;
114
- localTasks . RemoveAt ( index ) ;
115
- await session . Pool ! . ReturnAsync ( IOBehavior . Asynchronous , session ) . ConfigureAwait ( false ) ;
116
- GC . KeepAlive ( connection ) ;
117
- }
97
+ await Task . WhenAll ( localTasks ) ;
98
+ localTasks . Clear ( ) ;
118
99
}
119
100
}
120
101
catch ( Exception ex ) when ( ! ( ex is OperationCanceledException oce && oce . CancellationToken == s_cancellationTokenSource . Token ) )
@@ -124,25 +105,11 @@ public static async Task ReturnSessionsAsync()
124
105
}
125
106
}
126
107
127
- internal readonly struct SessionResetTask
128
- {
129
- public SessionResetTask ( ServerSession session , Task < bool > resetTask , MySqlConnection ? owningConnection )
130
- {
131
- Session = session ;
132
- ResetTask = resetTask ;
133
- OwningConnection = owningConnection ;
134
- }
135
-
136
- public ServerSession Session { get ; }
137
- public Task < bool > ResetTask { get ; }
138
- public MySqlConnection ? OwningConnection { get ; }
139
- }
140
-
141
108
static readonly IMySqlConnectorLogger Log = MySqlConnectorLogManager . CreateLogger ( nameof ( BackgroundConnectionResetHelper ) ) ;
142
109
static readonly object s_lock = new ( ) ;
143
110
static readonly SemaphoreSlim s_semaphore = new ( 1 , 1 ) ;
144
111
static readonly CancellationTokenSource s_cancellationTokenSource = new ( ) ;
145
- static readonly List < SessionResetTask > s_sessions = new ( ) ;
112
+ static readonly List < Task < bool > > s_resetTasks = new ( ) ;
146
113
static Task ? s_workerTask ;
147
114
}
148
115
}
0 commit comments