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...) 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 { func IsValidAsKeyPath(s string) bool {
return strings.IndexByte(s, '/') == -1 return strings.IndexByte(s, '/') == -1
} }

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

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

Loading…
Cancel
Save