add flags for register ttl and interval

This commit is contained in:
Asim Aslam 2018-04-06 14:03:39 +01:00
parent 73e22eb5b1
commit 56735b4427
2 changed files with 30 additions and 1 deletions

View File

@ -78,6 +78,16 @@ var (
EnvVar: "MICRO_CLIENT_POOL_TTL", EnvVar: "MICRO_CLIENT_POOL_TTL",
Usage: "Sets the client connection pool ttl. e.g 500ms, 5s, 1m. Default: 1m", Usage: "Sets the client connection pool ttl. e.g 500ms, 5s, 1m. Default: 1m",
}, },
cli.IntFlag{
Name: "register_ttl",
EnvVar: "MICRO_REGISTER_TTL",
Usage: "Register TTL in seconds",
},
cli.IntFlag{
Name: "register_interval",
EnvVar: "MICRO_REGISTER_INTERVAL",
Usage: "Register interval in seconds",
},
cli.StringFlag{ cli.StringFlag{
Name: "server_name", Name: "server_name",
EnvVar: "MICRO_SERVER_NAME", EnvVar: "MICRO_SERVER_NAME",
@ -370,6 +380,10 @@ func (c *cmd) Before(ctx *cli.Context) error {
serverOpts = append(serverOpts, server.Advertise(ctx.String("server_advertise"))) serverOpts = append(serverOpts, server.Advertise(ctx.String("server_advertise")))
} }
if ttl := time.Duration(ctx.GlobalInt("register_ttl")); ttl > 0 {
serverOpts = append(serverOpts, server.RegisterTTL(ttl*time.Second))
}
// client opts // client opts
if r := ctx.Int("client_retries"); r > 0 { if r := ctx.Int("client_retries"); r > 0 {
clientOpts = append(clientOpts, client.Retries(r)) clientOpts = append(clientOpts, client.Retries(r))

View File

@ -7,7 +7,8 @@ import (
"syscall" "syscall"
"time" "time"
log "github.com/micro/go-log" "github.com/micro/cli"
"github.com/micro/go-log"
"github.com/micro/go-micro/client" "github.com/micro/go-micro/client"
"github.com/micro/go-micro/cmd" "github.com/micro/go-micro/cmd"
"github.com/micro/go-micro/metadata" "github.com/micro/go-micro/metadata"
@ -66,6 +67,20 @@ func (s *service) Init(opts ...Option) {
} }
s.once.Do(func() { s.once.Do(func() {
// save user action
action := s.opts.Cmd.App().Action
// set service action
s.opts.Cmd.App().Action = func(c *cli.Context) {
// set register interval
if i := time.Duration(c.GlobalInt("register_interval")); i > 0 {
s.opts.RegisterInterval = i * time.Second
}
// user action
action(c)
}
// Initialise the command flags, overriding new service // Initialise the command flags, overriding new service
_ = s.opts.Cmd.Init( _ = s.opts.Cmd.Init(
cmd.Broker(&s.opts.Broker), cmd.Broker(&s.opts.Broker),