Skip to content

Commit 5e4c65e

Browse files
committed
fix ddliu#34
1 parent ccb53ec commit 5e4c65e

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

default_client.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
// Powerful and easy to use http client
55
package httpclient
66

7+
import "sync"
8+
79
// The default client for convenience
810
var defaultClient = &HttpClient{
911
reuseTransport: true,
1012
reuseJar: true,
13+
lock: new(sync.Mutex),
1114
}
1215

1316
var Defaults = defaultClient.Defaults

httpclient.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ func NewHttpClient() *HttpClient {
385385
c := &HttpClient{
386386
reuseTransport: true,
387387
reuseJar: true,
388+
lock: new(sync.Mutex),
388389
}
389390

390391
return c
@@ -423,6 +424,8 @@ type HttpClient struct {
423424

424425
// Make requests of one client concurrent safe.
425426
lock *sync.Mutex
427+
428+
withLock bool
426429
}
427430

428431
// Set default options and headers.
@@ -453,10 +456,8 @@ func (this *HttpClient) Defaults(defaults Map) *HttpClient {
453456
// Begin marks the begining of a request, it's necessary for concurrent
454457
// requests.
455458
func (this *HttpClient) Begin() *HttpClient {
456-
if this.lock == nil {
457-
this.lock = new(sync.Mutex)
458-
}
459459
this.lock.Lock()
460+
this.withLock = true
460461

461462
return this
462463
}
@@ -471,7 +472,7 @@ func (this *HttpClient) reset() {
471472

472473
// nil means the Begin has not been called, asume requests are not
473474
// concurrent.
474-
if this.lock != nil {
475+
if this.withLock {
475476
this.lock.Unlock()
476477
}
477478
}

0 commit comments

Comments
 (0)