mirror of https://github.com/shunfei/cronsun
node: 请求超时可配置
parent
9639300be0
commit
3bc0c0558e
|
@ -39,7 +39,8 @@ type Conf struct {
|
||||||
Cmd string // cmd 路径
|
Cmd string // cmd 路径
|
||||||
Sep string // etcd key 的连接符
|
Sep string // etcd key 的连接符
|
||||||
|
|
||||||
Ttl int64 // 节点超时时间,单位秒
|
Ttl int64 // 节点超时时间,单位秒
|
||||||
|
ReqTimeout int // 请求超时时间,单位秒
|
||||||
|
|
||||||
Log log.Config
|
Log log.Config
|
||||||
Etcd client.Config
|
Etcd client.Config
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
"Cmd": "/cronsun/cmd",
|
"Cmd": "/cronsun/cmd",
|
||||||
"Sep": "/",
|
"Sep": "/",
|
||||||
"Ttl": 10,
|
"Ttl": 10,
|
||||||
|
"ReqTimeout": 2,
|
||||||
"Log": "@extend:log.json",
|
"Log": "@extend:log.json",
|
||||||
"Etcd": "@extend:etcd.json"
|
"Etcd": "@extend:etcd.json"
|
||||||
}
|
}
|
||||||
|
|
18
node/node.go
18
node/node.go
|
@ -16,16 +16,13 @@ import (
|
||||||
"syscall"
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
ReqTimeout = 2 * time.Second
|
|
||||||
)
|
|
||||||
|
|
||||||
// Node 执行 cron 命令服务的结构体
|
// Node 执行 cron 命令服务的结构体
|
||||||
type Node struct {
|
type Node struct {
|
||||||
*client.Client
|
*client.Client
|
||||||
|
|
||||||
ttl time.Duration
|
ttl int64
|
||||||
prefix string
|
reqTimeout time.Duration
|
||||||
|
prefix string
|
||||||
|
|
||||||
Key string
|
Key string
|
||||||
PID string
|
PID string
|
||||||
|
@ -50,8 +47,9 @@ func NewNode(cfg *conf.Conf) (n *Node, err error) {
|
||||||
n = &Node{
|
n = &Node{
|
||||||
Client: cli,
|
Client: cli,
|
||||||
|
|
||||||
ttl: time.Duration(cfg.Ttl) * time.Second,
|
ttl: cfg.Ttl,
|
||||||
prefix: cfg.Proc + cfg.Sep,
|
reqTimeout: time.Duration(cfg.ReqTimeout) * time.Second,
|
||||||
|
prefix: cfg.Proc + cfg.Sep,
|
||||||
|
|
||||||
Key: cfg.Proc + cfg.Sep + ip.String(),
|
Key: cfg.Proc + cfg.Sep + ip.String(),
|
||||||
PID: strconv.Itoa(os.Getpid()),
|
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)
|
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 {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -97,7 +95,7 @@ func (n *Node) Register() (err error) {
|
||||||
// 判断 node 是否已注册到 etcd
|
// 判断 node 是否已注册到 etcd
|
||||||
// 存在则返回进行 pid,不存在返回 -1
|
// 存在则返回进行 pid,不存在返回 -1
|
||||||
func (n *Node) Exist() (pid int, err error) {
|
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())
|
resp, err := n.Client.Get(ctx, n.Key, client.WithFromKey())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue