Skip to content
Permalink
Browse files

change timer to public api #525 (#527)

* change acquireTimer and releaseTimer to public api
  • Loading branch information...
xuecai authored and erikdubbelboer committed Feb 3, 2019
1 parent aaec9b0 commit 627d63dd25bce25772a610342afd632238463c33
Showing with 21 additions and 11 deletions.
  1. +1 −1 args.go
  2. +4 −4 client.go
  3. +4 −4 tcpdialer.go
  4. +12 −2 timer.go
@@ -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) {
@@ -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)
@@ -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
}
@@ -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:
@@ -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
}
@@ -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
}
@@ -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
@@ -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
}
@@ -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)
@@ -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)
}

0 comments on commit 627d63d

Please sign in to comment.
You can’t perform that action at this time.