Skip to content

Commit

Permalink
lease: use monotime in time.Time for Go 1.9
Browse files Browse the repository at this point in the history
The golang/time package tracks monotonic time in each time.Time
returned by time.Now function for Go 1.9.

Use time.Time to measure whether a lease is expired and remove
previous pkg/monotime. Use zero time.Time to mean forever. No
expiration when expiry.IsZero() is true.
  • Loading branch information
lorneli committed Sep 7, 2017
1 parent 589a7a1 commit 63aa64d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 93 deletions.
38 changes: 23 additions & 15 deletions lease/lessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,18 @@ import (
"math"
"sort"
"sync"
"sync/atomic"
"time"

"github.com/coreos/etcd/lease/leasepb"
"github.com/coreos/etcd/mvcc/backend"
"github.com/coreos/etcd/pkg/monotime"
)

const (
// NoLease is a special LeaseID representing the absence of a lease.
NoLease = LeaseID(0)

forever = monotime.Time(math.MaxInt64)
)
// NoLease is a special LeaseID representing the absence of a lease.
const NoLease = LeaseID(0)

var (
forever = time.Time{}

leaseBucketName = []byte("lease")

// maximum number of leases to revoke per second; configurable for tests
Expand Down Expand Up @@ -560,8 +556,10 @@ func (le *lessor) initAndRecover() {
type Lease struct {
ID LeaseID
ttl int64 // time to live in seconds
// expiry is time when lease should expire; must be 64-bit aligned.
expiry monotime.Time
// expiryMu protects concurrent accesses to expiry
expiryMu sync.RWMutex
// expiry is time when lease should expire. no expiration when expiry.IsZero() is true
expiry time.Time

// mu protects concurrent accesses to itemSet
mu sync.RWMutex
Expand Down Expand Up @@ -594,12 +592,18 @@ func (l *Lease) TTL() int64 {

// refresh refreshes the expiry of the lease.
func (l *Lease) refresh(extend time.Duration) {
t := monotime.Now().Add(extend + time.Duration(l.ttl)*time.Second)
atomic.StoreUint64((*uint64)(&l.expiry), uint64(t))
newExpiry := time.Now().Add(extend + time.Duration(l.ttl)*time.Second)
l.expiryMu.Lock()
defer l.expiryMu.Unlock()
l.expiry = newExpiry
}

// forever sets the expiry of lease to be forever.
func (l *Lease) forever() { atomic.StoreUint64((*uint64)(&l.expiry), uint64(forever)) }
func (l *Lease) forever() {
l.expiryMu.Lock()
defer l.expiryMu.Unlock()
l.expiry = forever
}

// Keys returns all the keys attached to the lease.
func (l *Lease) Keys() []string {
Expand All @@ -614,8 +618,12 @@ func (l *Lease) Keys() []string {

// Remaining returns the remaining time of the lease.
func (l *Lease) Remaining() time.Duration {
t := monotime.Time(atomic.LoadUint64((*uint64)(&l.expiry)))
return time.Duration(t - monotime.Now())
l.expiryMu.RLock()
defer l.expiryMu.RUnlock()
if l.expiry.IsZero() {
return time.Duration(math.MaxInt64)
}
return l.expiry.Sub(time.Now())
}

type LeaseItem struct {
Expand Down
6 changes: 0 additions & 6 deletions pkg/monotime/issue15006.s

This file was deleted.

26 changes: 0 additions & 26 deletions pkg/monotime/monotime.go

This file was deleted.

24 changes: 0 additions & 24 deletions pkg/monotime/nanotime.go

This file was deleted.

22 changes: 0 additions & 22 deletions pkg/monotime/nanotime_test.go

This file was deleted.

0 comments on commit 63aa64d

Please sign in to comment.