5
5
using System . IO ;
6
6
using System . Threading . Tasks ;
7
7
using System . Collections . Generic ;
8
+ using Nest . Domain . Connection ;
8
9
9
10
namespace Nest
10
11
{
@@ -14,6 +15,7 @@ public class Connection : IConnection
14
15
15
16
private IConnectionSettings _ConnectionSettings { get ; set ; }
16
17
private Semaphore _ResourceLock ;
18
+ private readonly bool _enableTrace ;
17
19
18
20
public Connection ( IConnectionSettings settings )
19
21
{
@@ -22,6 +24,7 @@ public Connection(IConnectionSettings settings)
22
24
23
25
this . _ConnectionSettings = settings ;
24
26
this . _ResourceLock = new Semaphore ( settings . MaximumAsyncConnections , settings . MaximumAsyncConnections ) ;
27
+ this . _enableTrace = settings . TraceEnabled ;
25
28
}
26
29
27
30
public ConnectionStatus GetSync ( string path )
@@ -162,61 +165,81 @@ private HttpWebRequest CreateWebRequest(string path, string method)
162
165
163
166
protected virtual ConnectionStatus DoSynchronousRequest ( HttpWebRequest request , string data = null )
164
167
{
165
- var timeout = this . _ConnectionSettings . Timeout ;
166
- if ( data != null )
168
+ using ( var tracer = new ConnectionStatusTracer ( this . _ConnectionSettings . TraceEnabled ) )
167
169
{
168
- using ( var r = request . GetRequestStream ( ) )
170
+ ConnectionStatus cs = null ;
171
+ if ( data != null )
169
172
{
170
- byte [ ] buffer = Encoding . UTF8 . GetBytes ( data ) ;
171
- r . Write ( buffer , 0 , buffer . Length ) ;
173
+ using ( var r = request . GetRequestStream ( ) )
174
+ {
175
+ byte [ ] buffer = Encoding . UTF8 . GetBytes ( data ) ;
176
+ r . Write ( buffer , 0 , buffer . Length ) ;
177
+ }
172
178
}
173
- }
174
- try
175
- {
176
- using ( var response = ( HttpWebResponse ) request . GetResponse ( ) )
177
- using ( var responseStream = response . GetResponseStream ( ) )
178
- using ( var streamReader = new StreamReader ( responseStream ) )
179
+ try
180
+ {
181
+ using ( var response = ( HttpWebResponse ) request . GetResponse ( ) )
182
+ using ( var responseStream = response . GetResponseStream ( ) )
183
+ using ( var streamReader = new StreamReader ( responseStream ) )
184
+ {
185
+ string result = streamReader . ReadToEnd ( ) ;
186
+ cs = new ConnectionStatus ( result )
187
+ {
188
+ Request = data ,
189
+ RequestUrl = request . RequestUri . ToString ( ) ,
190
+ RequestMethod = request . Method
191
+ } ;
192
+ tracer . SetResult ( cs ) ;
193
+ return cs ;
194
+ }
195
+ }
196
+ catch ( WebException webException )
179
197
{
180
- string result = streamReader . ReadToEnd ( ) ;
181
- var cs = new ConnectionStatus ( result )
198
+ cs = new ConnectionStatus ( webException )
182
199
{
183
200
Request = data ,
184
201
RequestUrl = request . RequestUri . ToString ( ) ,
185
202
RequestMethod = request . Method
186
203
} ;
204
+ tracer . SetResult ( cs ) ;
187
205
return cs ;
188
206
}
189
207
}
190
- catch ( WebException webException )
191
- {
192
- return new ConnectionStatus ( webException ) { Request = data , RequestUrl = request . RequestUri . ToString ( ) , RequestMethod = request . Method } ;
193
- }
194
208
}
195
209
196
210
protected virtual Task < ConnectionStatus > DoAsyncRequest ( HttpWebRequest request , string data = null )
197
211
{
198
- var timeout = this . _ConnectionSettings . Timeout ;
199
-
200
212
var tcs = new TaskCompletionSource < ConnectionStatus > ( ) ;
213
+ var timeout = this . _ConnectionSettings . Timeout ;
201
214
if ( ! this . _ResourceLock . WaitOne ( timeout ) )
202
215
{
203
- var m = "Could not start the operation before the timeout of " + timeout + "ms completed while waiting for the semaphore" ;
204
- tcs . SetResult ( new ConnectionStatus ( new TimeoutException ( m ) ) ) ;
205
- return tcs . Task ;
216
+ using ( var tracer = new ConnectionStatusTracer ( this . _ConnectionSettings . TraceEnabled ) )
217
+ {
218
+ var m = "Could not start the operation before the timeout of " + timeout +
219
+ "ms completed while waiting for the semaphore" ;
220
+ var cs = new ConnectionStatus ( new TimeoutException ( m ) ) ;
221
+ tcs . SetResult ( cs ) ;
222
+ tracer . SetResult ( cs ) ;
223
+ return tcs . Task ;
224
+ }
206
225
}
207
226
try
208
227
{
209
228
return Task . Factory . StartNew ( ( ) =>
210
229
{
211
- this . Iterate ( this . _AsyncSteps ( request , tcs , data ) , tcs ) ;
212
- return tcs . Task . Result ;
230
+ using ( var tracer = new ConnectionStatusTracer ( this . _ConnectionSettings . TraceEnabled ) )
231
+ {
232
+ this . Iterate ( this . _AsyncSteps ( request , tcs , data ) , tcs ) ;
233
+ var cs = tcs . Task . Result ;
234
+ tracer . SetResult ( cs ) ;
235
+ return cs ;
236
+ }
213
237
} , TaskCreationOptions . LongRunning ) ;
214
238
}
215
239
finally
216
240
{
217
241
this . _ResourceLock . Release ( ) ;
218
242
}
219
-
220
243
}
221
244
222
245
private IEnumerable < Task > _AsyncSteps ( HttpWebRequest request , TaskCompletionSource < ConnectionStatus > tcs , string data = null )
@@ -284,7 +307,7 @@ public void Iterate(IEnumerable<Task> asyncIterator, TaskCompletionSource<Connec
284
307
//none of the individual steps in _AsyncSteps run in parallel for 1 request
285
308
//as this would be impossible we can assume Aggregate Exception.InnerException
286
309
var exception = completedTask . Exception . InnerException ;
287
-
310
+
288
311
//cleanly exit from exceptions in stages if the exception is a webexception
289
312
if ( exception is WebException )
290
313
tcs . SetResult ( new ConnectionStatus ( exception ) ) ;
0 commit comments