Skip to content

Commit

Permalink
pd-client: use sync pool for tso request
Browse files Browse the repository at this point in the history
  • Loading branch information
siddontang committed Apr 7, 2017
1 parent 4f50045 commit 647194f
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions pd-client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,13 +401,19 @@ func (c *client) GetClusterID(context.Context) uint64 {
return c.clusterID
}

var tsoReqPool = sync.Pool{
New: func() interface{} {
return &tsoRequest{
done: make(chan error, 1),
}
},
}

func (c *client) GetTS(ctx context.Context) (int64, int64, error) {
start := time.Now()
defer func() { cmdDuration.WithLabelValues("tso").Observe(time.Since(start).Seconds()) }()

req := &tsoRequest{
done: make(chan error, 1),
}
req := tsoReqPool.Get().(*tsoRequest)
c.tsoRequests <- req

select {
Expand All @@ -416,7 +422,9 @@ func (c *client) GetTS(ctx context.Context) (int64, int64, error) {
cmdFailedDuration.WithLabelValues("tso").Observe(time.Since(start).Seconds())
return 0, 0, errors.Trace(err)
}
return req.physical, req.logical, err
physical, logical := req.physical, req.logical
tsoReqPool.Put(req)
return physical, logical, err
case <-ctx.Done():
return 0, 0, errors.Trace(ctx.Err())
}
Expand Down

0 comments on commit 647194f

Please sign in to comment.