mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
contrib/recipes: use clientv3 lease API
This commit is contained in:
parent
936e991f9f
commit
16420cfe63
@ -15,11 +15,9 @@ package recipe
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
|
"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
|
||||||
"github.com/coreos/etcd/clientv3"
|
"github.com/coreos/etcd/clientv3"
|
||||||
pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
|
|
||||||
"github.com/coreos/etcd/lease"
|
"github.com/coreos/etcd/lease"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -32,8 +30,9 @@ type clientLeaseMgr struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type leaseKeepAlive struct {
|
type leaseKeepAlive struct {
|
||||||
id lease.LeaseID
|
id lease.LeaseID
|
||||||
donec chan struct{}
|
cancel context.CancelFunc
|
||||||
|
donec <-chan struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func SessionLease(client *clientv3.Client) (lease.LeaseID, error) {
|
func SessionLease(client *clientv3.Client) (lease.LeaseID, error) {
|
||||||
@ -49,13 +48,10 @@ func SessionLeaseTTL(client *clientv3.Client, ttl int64) (lease.LeaseID, error)
|
|||||||
// would fail) or if transferring lease ownership.
|
// would fail) or if transferring lease ownership.
|
||||||
func StopSessionLease(client *clientv3.Client) {
|
func StopSessionLease(client *clientv3.Client) {
|
||||||
clientLeases.mu.Lock()
|
clientLeases.mu.Lock()
|
||||||
lka, ok := clientLeases.leases[client]
|
lka := clientLeases.leases[client]
|
||||||
if ok {
|
|
||||||
delete(clientLeases.leases, client)
|
|
||||||
}
|
|
||||||
clientLeases.mu.Unlock()
|
clientLeases.mu.Unlock()
|
||||||
if lka != nil {
|
if lka != nil {
|
||||||
lka.donec <- struct{}{}
|
lka.cancel()
|
||||||
<-lka.donec
|
<-lka.donec
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -67,8 +63,7 @@ func RevokeSessionLease(client *clientv3.Client) (err error) {
|
|||||||
clientLeases.mu.Unlock()
|
clientLeases.mu.Unlock()
|
||||||
StopSessionLease(client)
|
StopSessionLease(client)
|
||||||
if lka != nil {
|
if lka != nil {
|
||||||
req := &pb.LeaseRevokeRequest{ID: int64(lka.id)}
|
_, err = clientv3.NewLease(client).Revoke(context.TODO(), lka.id)
|
||||||
_, err = client.Lease.LeaseRevoke(context.TODO(), req)
|
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -80,48 +75,37 @@ func (clm *clientLeaseMgr) sessionLease(client *clientv3.Client, ttl int64) (lea
|
|||||||
return lka.id, nil
|
return lka.id, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := client.Lease.LeaseCreate(context.TODO(), &pb.LeaseCreateRequest{TTL: ttl})
|
lc := clientv3.NewLease(client)
|
||||||
|
resp, err := lc.Create(context.TODO(), ttl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return lease.NoLease, err
|
return lease.NoLease, err
|
||||||
}
|
}
|
||||||
id := lease.LeaseID(resp.ID)
|
id := lease.LeaseID(resp.ID)
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
keepAlive, err := client.Lease.LeaseKeepAlive(ctx)
|
keepAlive, err := lc.KeepAlive(ctx, id)
|
||||||
if err != nil || keepAlive == nil {
|
if err != nil || keepAlive == nil {
|
||||||
return lease.NoLease, err
|
return lease.NoLease, err
|
||||||
}
|
}
|
||||||
|
|
||||||
lka := &leaseKeepAlive{id: id, donec: make(chan struct{})}
|
donec := make(chan struct{})
|
||||||
|
lka := &leaseKeepAlive{
|
||||||
|
id: id,
|
||||||
|
cancel: cancel,
|
||||||
|
donec: donec}
|
||||||
clm.leases[client] = lka
|
clm.leases[client] = lka
|
||||||
|
|
||||||
// keep the lease alive until client error
|
// keep the lease alive until client error or cancelled context
|
||||||
go func() {
|
go func() {
|
||||||
defer func() {
|
defer func() {
|
||||||
keepAlive.CloseSend()
|
|
||||||
clm.mu.Lock()
|
clm.mu.Lock()
|
||||||
delete(clm.leases, client)
|
delete(clm.leases, client)
|
||||||
clm.mu.Unlock()
|
clm.mu.Unlock()
|
||||||
cancel()
|
lc.Close()
|
||||||
close(lka.donec)
|
close(donec)
|
||||||
}()
|
}()
|
||||||
|
for range keepAlive {
|
||||||
ttl := resp.TTL
|
// eat messages until keep alive channel closes
|
||||||
for {
|
|
||||||
lreq := &pb.LeaseKeepAliveRequest{ID: int64(id)}
|
|
||||||
select {
|
|
||||||
case <-lka.donec:
|
|
||||||
return
|
|
||||||
case <-time.After(time.Duration(ttl/2) * time.Second):
|
|
||||||
}
|
|
||||||
if err := keepAlive.Send(lreq); err != nil {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
resp, err := keepAlive.Recv()
|
|
||||||
if err != nil {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
ttl = resp.TTL
|
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user