From 16cde1bfaefd985d90431be4071810697e084a78 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Wed, 10 Mar 2021 01:04:58 +0300 Subject: [PATCH] provide timeout option and lower default Signed-off-by: Vasiliy Tolstov --- options.go | 8 ++++++++ vault.go | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/options.go b/options.go index 085f032..a443f24 100644 --- a/options.go +++ b/options.go @@ -1,6 +1,8 @@ package vault import ( + "time" + "github.com/hashicorp/vault/api" "github.com/unistack-org/micro/v3/config" ) @@ -40,3 +42,9 @@ type secretIDKey struct{} func SecretID(secret string) config.Option { return config.SetOption(secretIDKey{}, secret) } + +type timeoutKey struct{} + +func Timeout(td time.Duration) config.Option { + return config.SetOption(timeoutKey{}, td) +} diff --git a/vault.go b/vault.go index 742758c..1ea02cc 100644 --- a/vault.go +++ b/vault.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "errors" + "time" "github.com/hashicorp/vault/api" "github.com/imdario/mergo" @@ -36,6 +37,7 @@ func (c *vaultConfig) Init(opts ...config.Option) error { } cfg := api.DefaultConfig() + cfg.Timeout = 3 * time.Second path := "" token := "" roleID := "" @@ -46,6 +48,10 @@ func (c *vaultConfig) Init(opts ...config.Option) error { cfg = v } + if v, ok := c.opts.Context.Value(timeoutKey{}).(time.Duration); ok { + cfg.Timeout = v + } + if v, ok := c.opts.Context.Value(addrKey{}).(string); ok { cfg.Address = v }