From 3bc0c0558e33f0c6f86049b4c5f754d46efe4f8b Mon Sep 17 00:00:00 2001 From: miraclesu Date: Fri, 6 Jan 2017 16:59:31 +0800 Subject: [PATCH] =?UTF-8?q?node:=20=E8=AF=B7=E6=B1=82=E8=B6=85=E6=97=B6?= =?UTF-8?q?=E5=8F=AF=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/conf.go | 3 ++- conf/files/base.json.sample | 1 + node/node.go | 18 ++++++++---------- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/conf/conf.go b/conf/conf.go index 25e80cd..7ef14b2 100644 --- a/conf/conf.go +++ b/conf/conf.go @@ -39,7 +39,8 @@ type Conf struct { Cmd string // cmd 路径 Sep string // etcd key 的连接符 - Ttl int64 // 节点超时时间,单位秒 + Ttl int64 // 节点超时时间,单位秒 + ReqTimeout int // 请求超时时间,单位秒 Log log.Config Etcd client.Config diff --git a/conf/files/base.json.sample b/conf/files/base.json.sample index 6d46dbb..aab2ee8 100644 --- a/conf/files/base.json.sample +++ b/conf/files/base.json.sample @@ -3,6 +3,7 @@ "Cmd": "/cronsun/cmd", "Sep": "/", "Ttl": 10, + "ReqTimeout": 2, "Log": "@extend:log.json", "Etcd": "@extend:etcd.json" } diff --git a/node/node.go b/node/node.go index 979abe6..7b92831 100644 --- a/node/node.go +++ b/node/node.go @@ -16,16 +16,13 @@ import ( "syscall" ) -const ( - ReqTimeout = 2 * time.Second -) - // Node 执行 cron 命令服务的结构体 type Node struct { *client.Client - ttl time.Duration - prefix string + ttl int64 + reqTimeout time.Duration + prefix string Key string PID string @@ -50,8 +47,9 @@ func NewNode(cfg *conf.Conf) (n *Node, err error) { n = &Node{ Client: cli, - ttl: time.Duration(cfg.Ttl) * time.Second, - prefix: cfg.Proc + cfg.Sep, + ttl: cfg.Ttl, + reqTimeout: time.Duration(cfg.ReqTimeout) * time.Second, + prefix: cfg.Proc + cfg.Sep, Key: cfg.Proc + cfg.Sep + ip.String(), PID: strconv.Itoa(os.Getpid()), @@ -76,7 +74,7 @@ func (n *Node) Register() (err error) { return fmt.Errorf("node[%s] pid[%d] exist", n.Key[len(n.prefix):], pid) } - resp, err := n.Client.Grant(context.TODO(), conf.Config.Ttl) + resp, err := n.Client.Grant(context.TODO(), n.ttl) if err != nil { return } @@ -97,7 +95,7 @@ func (n *Node) Register() (err error) { // 判断 node 是否已注册到 etcd // 存在则返回进行 pid,不存在返回 -1 func (n *Node) Exist() (pid int, err error) { - ctx, cancel := context.WithTimeout(context.Background(), ReqTimeout) + ctx, cancel := context.WithTimeout(context.Background(), n.reqTimeout) resp, err := n.Client.Get(ctx, n.Key, client.WithFromKey()) defer cancel() if err != nil {