File tree Expand file tree Collapse file tree 2 files changed +8
-4
lines changed Expand file tree Collapse file tree 2 files changed +8
-4
lines changed Original file line number Diff line number Diff line change 4
4
// Powerful and easy to use http client
5
5
package httpclient
6
6
7
+ import "sync"
8
+
7
9
// The default client for convenience
8
10
var defaultClient = & HttpClient {
9
11
reuseTransport : true ,
10
12
reuseJar : true ,
13
+ lock : new (sync.Mutex ),
11
14
}
12
15
13
16
var Defaults = defaultClient .Defaults
Original file line number Diff line number Diff line change @@ -385,6 +385,7 @@ func NewHttpClient() *HttpClient {
385
385
c := & HttpClient {
386
386
reuseTransport : true ,
387
387
reuseJar : true ,
388
+ lock : new (sync.Mutex ),
388
389
}
389
390
390
391
return c
@@ -423,6 +424,8 @@ type HttpClient struct {
423
424
424
425
// Make requests of one client concurrent safe.
425
426
lock * sync.Mutex
427
+
428
+ withLock bool
426
429
}
427
430
428
431
// Set default options and headers.
@@ -453,10 +456,8 @@ func (this *HttpClient) Defaults(defaults Map) *HttpClient {
453
456
// Begin marks the begining of a request, it's necessary for concurrent
454
457
// requests.
455
458
func (this * HttpClient ) Begin () * HttpClient {
456
- if this .lock == nil {
457
- this .lock = new (sync.Mutex )
458
- }
459
459
this .lock .Lock ()
460
+ this .withLock = true
460
461
461
462
return this
462
463
}
@@ -471,7 +472,7 @@ func (this *HttpClient) reset() {
471
472
472
473
// nil means the Begin has not been called, asume requests are not
473
474
// concurrent.
474
- if this .lock != nil {
475
+ if this .withLock {
475
476
this .lock .Unlock ()
476
477
}
477
478
}
You can’t perform that action at this time.
0 commit comments