Skip to content

Commit

Permalink
change timer to public api #525 (#527)
Browse files Browse the repository at this point in the history
* change acquireTimer and releaseTimer to public api
  • Loading branch information
xuecai authored and erikdubbelboer committed Feb 3, 2019
1 parent aaec9b0 commit 627d63d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion args.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func AcquireArgs() *Args {
return argsPool.Get().(*Args)
}

// ReleaseArgs returns the object acquired via AquireArgs to the pool.
// ReleaseArgs returns the object acquired via AcquireArgs to the pool.
//
// Do not access the released Args object, otherwise data races may occur.
func ReleaseArgs(a *Args) {
Expand Down
8 changes: 4 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ func clientGetURLDeadline(dst []byte, url string, deadline time.Time, c clientDo
}
}()

tc := acquireTimer(timeout)
tc := AcquireTimer(timeout)
select {
case resp := <-ch:
ReleaseRequest(req)
Expand All @@ -762,7 +762,7 @@ func clientGetURLDeadline(dst []byte, url string, deadline time.Time, c clientDo
body = dst
err = ErrTimeout
}
releaseTimer(tc)
ReleaseTimer(tc)

return statusCode, body, err
}
Expand Down Expand Up @@ -1003,7 +1003,7 @@ func clientDoDeadline(req *Request, resp *Response, deadline time.Time, c client
}
}()

tc := acquireTimer(timeout)
tc := AcquireTimer(timeout)
var err error
select {
case err = <-ch:
Expand All @@ -1019,7 +1019,7 @@ func clientDoDeadline(req *Request, resp *Response, deadline time.Time, c client
atomic.StoreInt32(&cleanup, 1)
err = ErrTimeout
}
releaseTimer(tc)
ReleaseTimer(tc)

return err
}
Expand Down
8 changes: 4 additions & 4 deletions tcpdialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,14 @@ func tryDial(network string, addr *net.TCPAddr, deadline time.Time, concurrencyC
select {
case concurrencyCh <- struct{}{}:
default:
tc := acquireTimer(timeout)
tc := AcquireTimer(timeout)
isTimeout := false
select {
case concurrencyCh <- struct{}{}:
case <-tc.C:
isTimeout = true
}
releaseTimer(tc)
ReleaseTimer(tc)
if isTimeout {
return nil, ErrDialTimeout
}
Expand Down Expand Up @@ -242,7 +242,7 @@ func tryDial(network string, addr *net.TCPAddr, deadline time.Time, concurrencyC
err error
)

tc := acquireTimer(timeout)
tc := AcquireTimer(timeout)
select {
case dr := <-ch:
conn = dr.conn
Expand All @@ -251,7 +251,7 @@ func tryDial(network string, addr *net.TCPAddr, deadline time.Time, concurrencyC
case <-tc.C:
err = ErrDialTimeout
}
releaseTimer(tc)
ReleaseTimer(tc)

return conn, err
}
Expand Down
14 changes: 12 additions & 2 deletions timer.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ func stopTimer(t *time.Timer) {
}
}

func acquireTimer(timeout time.Duration) *time.Timer {
// AcquireTimer returns a time.Timer from the pool and updates it to
// send the current time on its channel after at least timeout.
//
// The returned Timer may be returned to the pool with ReleaseTimer
// when no longer needed. This allows reducing GC load.
func AcquireTimer(timeout time.Duration) *time.Timer {
v := timerPool.Get()
if v == nil {
return time.NewTimer(timeout)
Expand All @@ -36,7 +41,12 @@ func acquireTimer(timeout time.Duration) *time.Timer {
return t
}

func releaseTimer(t *time.Timer) {
// ReleaseTimer returns the time.Timer acquired via AcquireTimer to the pool
// and prevents the Timer from firing.
//
// Do not access the released time.Timer or read from it's channel otherwise
// data races may occur.
func ReleaseTimer(t *time.Timer) {
stopTimer(t)
timerPool.Put(t)
}
Expand Down

0 comments on commit 627d63d

Please sign in to comment.