add variable timeout
All checks were successful
Gitea/etcdmtr/pipeline/head This commit looks good

This commit is contained in:
dave 2024-02-20 01:06:44 -08:00
parent 07ddbed7e3
commit fa89dae820

View File

@ -13,12 +13,15 @@ import (
"time"
)
var operationTimeout = 5 * time.Second
type EtcdMtrArgs struct {
Version bool `long:"version" description:"Show version information"`
ClientCert string `short:"c" long:"client-cert" description:"Client certificate pem" value-name:"FILE"`
ClientKey string `short:"k" long:"client-key" description:"Client certificate key" value-name:"FILE"`
ServerCA string `short:"a" long:"ca" description:"Certificate authority" value-name:"FILE"`
Insecure bool `short:"i" long:"insecure" description:"Do not verify against CA"`
Timeout int `short:"t" long:"timeout" description:"Operation timeout in seconds"`
}
type Client struct {
@ -74,7 +77,7 @@ func (c *Client) GetEtcd() (*clientv3.Client, error) {
func (c *Client) GetEtcdWithEndpoints(endpoints []string) (*clientv3.Client, error) {
return clientv3.New(clientv3.Config{
Endpoints: endpoints,
DialTimeout: 5 * time.Second,
DialTimeout: operationTimeout,
TLS: c.tlsConf,
})
}
@ -107,6 +110,10 @@ func main() {
}
}
if args.Timeout > 0 {
operationTimeout = time.Duration(args.Timeout) * time.Second
}
client, err := NewClient(positionals, args.ClientCert, args.ClientKey, args.ServerCA)
if err != nil {
panic(err)
@ -117,7 +124,7 @@ func main() {
panic(err)
}
ctx, _ := context.WithTimeout(context.TODO(), 5*time.Second)
ctx, _ := context.WithTimeout(context.TODO(), operationTimeout)
err = etcd.Sync(ctx)
if err != nil {
panic(err)