node: keepAlive with timeout

pull/1/head
miraclesu 8 years ago
parent ecc6883e20
commit c207e96a51

@ -80,6 +80,18 @@ func (c *Client) Watch(key string, opts ...client.OpOption) client.WatchChan {
return c.Client.Watch(context.Background(), key, opts...)
}
func (c *Client) Grant(ttl int64) (*client.LeaseGrantResponse, error) {
ctx, cancel := context.WithTimeout(context.Background(), c.reqTimeout)
defer cancel()
return c.Client.Grant(ctx, ttl)
}
func (c *Client) KeepAliveOnce(id client.LeaseID) (*client.LeaseKeepAliveResponse, error) {
ctx, cancel := context.WithTimeout(context.Background(), c.reqTimeout)
defer cancel()
return c.Client.KeepAliveOnce(ctx, id)
}
func IsValidAsKeyPath(s string) bool {
return strings.IndexByte(s, '/') == -1
}

@ -1,7 +1,6 @@
package models
import (
"context"
"fmt"
"sync"
"time"
@ -78,7 +77,7 @@ func (l *leaseID) get() client.LeaseID {
func (l *leaseID) set() error {
id := client.LeaseID(-1)
resp, err := DefalutClient.Grant(context.TODO(), l.ttl+2)
resp, err := DefalutClient.Grant(l.ttl + 2)
if err == nil {
id = resp.ID
}
@ -103,7 +102,7 @@ func (l *leaseID) keepAlive() {
id := l.get()
if id > 0 {
_, err := DefalutClient.KeepAliveOnce(context.TODO(), l.ID)
_, err := DefalutClient.KeepAliveOnce(l.ID)
if err == nil {
timer.Reset(duration)
continue

@ -6,8 +6,6 @@ import (
"strconv"
"time"
"golang.org/x/net/context"
client "github.com/coreos/etcd/clientv3"
"sunteng/commons/log"
@ -77,7 +75,7 @@ func (n *Node) Register() (err error) {
}
func (n *Node) set() error {
resp, err := n.Client.Grant(context.TODO(), n.ttl+2)
resp, err := n.Client.Grant(n.ttl + 2)
if err != nil {
return err
}
@ -100,7 +98,7 @@ func (n *Node) keepAlive() {
return
case <-timer.C:
if n.lID > 0 {
_, err := n.Client.KeepAliveOnce(context.TODO(), n.lID)
_, err := n.Client.KeepAliveOnce(n.lID)
if err == nil {
timer.Reset(duration)
continue

Loading…
Cancel
Save