Skip to content

Commit

Permalink
Revert "Remove CoarseTime"
Browse files Browse the repository at this point in the history
This reverts commit f2ddaff.
  • Loading branch information
kirillDanshin committed Aug 17, 2018
1 parent f2ddaff commit 4c7269e
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 16 deletions.
14 changes: 7 additions & 7 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,7 @@ func (c *HostClient) doNonNilReqResp(req *Request, resp *Response) (bool, error)
panic("BUG: resp cannot be nil")
}

atomic.StoreUint32(&c.lastUseTime, uint32(time.Now().Unix()-startTimeUnix))
atomic.StoreUint32(&c.lastUseTime, uint32(CoarseTimeNow().Unix()-startTimeUnix))

// Free up resources occupied by response before sending the request,
// so the GC may reclaim these resources (e.g. response body).
Expand All @@ -1057,7 +1057,7 @@ func (c *HostClient) doNonNilReqResp(req *Request, resp *Response) (bool, error)
// Optimization: update write deadline only if more than 25%
// of the last write deadline exceeded.
// See https://github.com/golang/go/issues/15133 for details.
currentTime := time.Now()
currentTime := CoarseTimeNow()
if currentTime.Sub(cc.lastWriteDeadlineTime) > (c.WriteTimeout >> 2) {
if err = conn.SetWriteDeadline(currentTime.Add(c.WriteTimeout)); err != nil {
c.closeConn(cc)
Expand Down Expand Up @@ -1101,7 +1101,7 @@ func (c *HostClient) doNonNilReqResp(req *Request, resp *Response) (bool, error)
// Optimization: update read deadline only if more than 25%
// of the last read deadline exceeded.
// See https://github.com/golang/go/issues/15133 for details.
currentTime := time.Now()
currentTime := CoarseTimeNow()
if currentTime.Sub(cc.lastReadDeadlineTime) > (c.ReadTimeout >> 2) {
if err = conn.SetReadDeadline(currentTime.Add(c.ReadTimeout)); err != nil {
c.closeConn(cc)
Expand Down Expand Up @@ -1276,7 +1276,7 @@ func acquireClientConn(conn net.Conn) *clientConn {
}
cc := v.(*clientConn)
cc.c = conn
cc.createdTime = time.Now()
cc.createdTime = CoarseTimeNow()
return cc
}

Expand All @@ -1288,7 +1288,7 @@ func releaseClientConn(cc *clientConn) {
var clientConnPool sync.Pool

func (c *HostClient) releaseConn(cc *clientConn) {
cc.lastUseTime = time.Now()
cc.lastUseTime = CoarseTimeNow()
c.connsLock.Lock()
c.conns = append(c.conns, cc)
c.connsLock.Unlock()
Expand Down Expand Up @@ -1991,7 +1991,7 @@ func (c *pipelineConnClient) writer(conn net.Conn, stopCh <-chan struct{}) error
// Optimization: update write deadline only if more than 25%
// of the last write deadline exceeded.
// See https://github.com/golang/go/issues/15133 for details.
currentTime := time.Now()
currentTime := CoarseTimeNow()
if currentTime.Sub(lastWriteDeadlineTime) > (writeTimeout >> 2) {
if err = conn.SetWriteDeadline(currentTime.Add(writeTimeout)); err != nil {
w.err = err
Expand Down Expand Up @@ -2072,7 +2072,7 @@ func (c *pipelineConnClient) reader(conn net.Conn, stopCh <-chan struct{}) error
// Optimization: update read deadline only if more than 25%
// of the last read deadline exceeded.
// See https://github.com/golang/go/issues/15133 for details.
currentTime := time.Now()
currentTime := CoarseTimeNow()
if currentTime.Sub(lastReadDeadlineTime) > (readTimeout >> 2) {
if err = conn.SetReadDeadline(currentTime.Add(readTimeout)); err != nil {
w.err = err
Expand Down
28 changes: 28 additions & 0 deletions coarseTime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package fasthttp

import (
"sync/atomic"
"time"
)

// CoarseTimeNow returns the current time truncated to the nearest second.
//
// This is a faster alternative to time.Now().
func CoarseTimeNow() time.Time {
tp := coarseTime.Load().(*time.Time)
return *tp
}

func init() {
t := time.Now().Truncate(time.Second)
coarseTime.Store(&t)
go func() {
for {
time.Sleep(time.Second)
t := time.Now().Truncate(time.Second)
coarseTime.Store(&t)
}
}()
}

var coarseTime atomic.Value
37 changes: 37 additions & 0 deletions coarseTime_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package fasthttp

import (
"sync/atomic"
"testing"
"time"
)

func BenchmarkCoarseTimeNow(b *testing.B) {
var zeroTimeCount uint64
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
t := CoarseTimeNow()
if t.IsZero() {
atomic.AddUint64(&zeroTimeCount, 1)
}
}
})
if zeroTimeCount > 0 {
b.Fatalf("zeroTimeCount must be zero")
}
}

func BenchmarkTimeNow(b *testing.B) {
var zeroTimeCount uint64
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
t := time.Now()
if t.IsZero() {
atomic.AddUint64(&zeroTimeCount, 1)
}
}
})
if zeroTimeCount > 0 {
b.Fatalf("zeroTimeCount must be zero")
}
}
21 changes: 13 additions & 8 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,13 +598,18 @@ func (ctx *RequestCtx) ConnID() uint64 {
return ctx.connID
}

// Time returns RequestHandler call time.
// Time returns RequestHandler call time truncated to the nearest second.
//
// Call time.Now() at the beginning of RequestHandler in order to obtain
// precise RequestHandler call time.
func (ctx *RequestCtx) Time() time.Time {
return ctx.time
}

// ConnTime returns the time server starts serving the connection
// the current request came from.
//
// The returned time is truncated to the nearest second.
func (ctx *RequestCtx) ConnTime() time.Time {
return ctx.connTime
}
Expand Down Expand Up @@ -1314,7 +1319,7 @@ func (s *Server) Serve(ln net.Listener) error {
if time.Since(lastOverflowErrorTime) > time.Minute {
s.logger().Printf("The incoming connection cannot be served, because %d concurrent connections are served. "+
"Try increasing Server.Concurrency", maxWorkersCount)
lastOverflowErrorTime = time.Now()
lastOverflowErrorTime = CoarseTimeNow()
}

// The current server reached concurrency limit,
Expand Down Expand Up @@ -1356,7 +1361,7 @@ func acceptConn(s *Server, ln net.Listener, lastPerIPErrorTime *time.Time) (net.
if time.Since(*lastPerIPErrorTime) > time.Minute {
s.logger().Printf("The number of connections from %s exceeds MaxConnsPerIP=%d",
getConnIP4(c), s.MaxConnsPerIP)
*lastPerIPErrorTime = time.Now()
*lastPerIPErrorTime = CoarseTimeNow()
}
continue
}
Expand Down Expand Up @@ -1471,7 +1476,7 @@ func (s *Server) serveConn(c net.Conn) error {
serverName := s.getServerName()
connRequestNum := uint64(0)
connID := nextConnID()
currentTime := time.Now()
currentTime := CoarseTimeNow()
connTime := currentTime
maxRequestBodySize := s.MaxRequestBodySize
if maxRequestBodySize <= 0 {
Expand Down Expand Up @@ -1528,7 +1533,7 @@ func (s *Server) serveConn(c net.Conn) error {
}
}

currentTime = time.Now()
currentTime = CoarseTimeNow()
ctx.lastReadDuration = currentTime.Sub(ctx.time)

if err != nil {
Expand Down Expand Up @@ -1669,7 +1674,7 @@ func (s *Server) serveConn(c net.Conn) error {
break
}

currentTime = time.Now()
currentTime = CoarseTimeNow()
}

if br != nil {
Expand Down Expand Up @@ -1725,7 +1730,7 @@ func (s *Server) updateWriteDeadline(c net.Conn, ctx *RequestCtx, lastDeadlineTi
// Optimization: update write deadline only if more than 25%
// of the last write deadline exceeded.
// See https://github.com/golang/go/issues/15133 for details.
currentTime := time.Now()
currentTime := CoarseTimeNow()
if currentTime.Sub(lastDeadlineTime) > (writeTimeout >> 2) {
if err := c.SetWriteDeadline(currentTime.Add(writeTimeout)); err != nil {
panic(fmt.Sprintf("BUG: error in SetWriteDeadline(%s): %s", writeTimeout, err))
Expand Down Expand Up @@ -1908,7 +1913,7 @@ func (ctx *RequestCtx) Init2(conn net.Conn, logger Logger, reduceMemoryUsage boo
ctx.connID = nextConnID()
ctx.s = fakeServer
ctx.connRequestNum = 0
ctx.connTime = time.Now()
ctx.connTime = CoarseTimeNow()
ctx.time = ctx.connTime

keepBodyBuffer := !reduceMemoryUsage
Expand Down
2 changes: 1 addition & 1 deletion workerpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func (wp *workerPool) getCh() *workerChan {
}

func (wp *workerPool) release(ch *workerChan) bool {
ch.lastUseTime = time.Now()
ch.lastUseTime = CoarseTimeNow()
wp.lock.Lock()
if wp.mustStop {
wp.lock.Unlock()
Expand Down

0 comments on commit 4c7269e

Please sign in to comment.