55using System . IO ;
66using System . Threading . Tasks ;
77using System . Collections . Generic ;
8+ using Nest . Domain . Connection ;
89
910namespace Nest
1011{
@@ -14,6 +15,7 @@ public class Connection : IConnection
1415
1516 private IConnectionSettings _ConnectionSettings { get ; set ; }
1617 private Semaphore _ResourceLock ;
18+ private readonly bool _enableTrace ;
1719
1820 public Connection ( IConnectionSettings settings )
1921 {
@@ -22,6 +24,7 @@ public Connection(IConnectionSettings settings)
2224
2325 this . _ConnectionSettings = settings ;
2426 this . _ResourceLock = new Semaphore ( settings . MaximumAsyncConnections , settings . MaximumAsyncConnections ) ;
27+ this . _enableTrace = settings . TraceEnabled ;
2528 }
2629
2730 public ConnectionStatus GetSync ( string path )
@@ -162,61 +165,81 @@ private HttpWebRequest CreateWebRequest(string path, string method)
162165
163166 protected virtual ConnectionStatus DoSynchronousRequest ( HttpWebRequest request , string data = null )
164167 {
165- var timeout = this . _ConnectionSettings . Timeout ;
166- if ( data != null )
168+ using ( var tracer = new ConnectionStatusTracer ( this . _ConnectionSettings . TraceEnabled ) )
167169 {
168- using ( var r = request . GetRequestStream ( ) )
170+ ConnectionStatus cs = null ;
171+ if ( data != null )
169172 {
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+ }
172178 }
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 )
179197 {
180- string result = streamReader . ReadToEnd ( ) ;
181- var cs = new ConnectionStatus ( result )
198+ cs = new ConnectionStatus ( webException )
182199 {
183200 Request = data ,
184201 RequestUrl = request . RequestUri . ToString ( ) ,
185202 RequestMethod = request . Method
186203 } ;
204+ tracer . SetResult ( cs ) ;
187205 return cs ;
188206 }
189207 }
190- catch ( WebException webException )
191- {
192- return new ConnectionStatus ( webException ) { Request = data , RequestUrl = request . RequestUri . ToString ( ) , RequestMethod = request . Method } ;
193- }
194208 }
195209
196210 protected virtual Task < ConnectionStatus > DoAsyncRequest ( HttpWebRequest request , string data = null )
197211 {
198- var timeout = this . _ConnectionSettings . Timeout ;
199-
200212 var tcs = new TaskCompletionSource < ConnectionStatus > ( ) ;
213+ var timeout = this . _ConnectionSettings . Timeout ;
201214 if ( ! this . _ResourceLock . WaitOne ( timeout ) )
202215 {
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+ }
206225 }
207226 try
208227 {
209228 return Task . Factory . StartNew ( ( ) =>
210229 {
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+ }
213237 } , TaskCreationOptions . LongRunning ) ;
214238 }
215239 finally
216240 {
217241 this . _ResourceLock . Release ( ) ;
218242 }
219-
220243 }
221244
222245 private IEnumerable < Task > _AsyncSteps ( HttpWebRequest request , TaskCompletionSource < ConnectionStatus > tcs , string data = null )
@@ -284,7 +307,7 @@ public void Iterate(IEnumerable<Task> asyncIterator, TaskCompletionSource<Connec
284307 //none of the individual steps in _AsyncSteps run in parallel for 1 request
285308 //as this would be impossible we can assume Aggregate Exception.InnerException
286309 var exception = completedTask . Exception . InnerException ;
287-
310+
288311 //cleanly exit from exceptions in stages if the exception is a webexception
289312 if ( exception is WebException )
290313 tcs . SetResult ( new ConnectionStatus ( exception ) ) ;
0 commit comments