Skip to content

Commit

Permalink
Merge pull request #8507 from lorneli/lease_monotime
Browse files Browse the repository at this point in the history
lease: use monotime in time.Time for Go 1.9
  • Loading branch information
Anthony Romano committed Sep 7, 2017
2 parents ff31fb4 + 63aa64d commit eb55917
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 @@ -564,8 +560,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 @@ -598,12 +596,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 @@ -618,8 +622,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 eb55917

Please sign in to comment.