From e72bc82991bf8036732fae2f18182a9d3d9948a3 Mon Sep 17 00:00:00 2001 From: trunglt251292 <34877159+trunglt251292@users.noreply.github.com> Date: Tue, 19 Apr 2022 09:58:27 +0700 Subject: [PATCH] Develop (#3) * [Update] * [Update] Config --- config.go | 5 +++++ natsio.go | 3 ++- server_reqres.go | 6 +++++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/config.go b/config.go index 295f751..898b767 100644 --- a/config.go +++ b/config.go @@ -1,5 +1,7 @@ package natsio +import "time" + // Config ... type Config struct { // Connect url @@ -13,6 +15,9 @@ type Config struct { // TLS config TLS *TLSConfig + + // RequestTimeout + RequestTimeout time.Duration } // TLSConfig ... diff --git a/natsio.go b/natsio.go index 33bdbb2..5ccc45d 100644 --- a/natsio.go +++ b/natsio.go @@ -3,7 +3,6 @@ package natsio import ( "errors" "fmt" - "github.com/logrusorgru/aurora" "github.com/nats-io/nats.go" ) @@ -11,6 +10,7 @@ import ( // Server ... type Server struct { instance *nats.Conn + Config Config } // JetStream ... @@ -53,6 +53,7 @@ func Connect(cfg Config) error { // Set client natsServer.instance = nc + natsServer.Config = cfg // Create jet stream context js, err := nc.JetStream(nats.PublishAsyncMaxPending(256)) diff --git a/server_reqres.go b/server_reqres.go index e099a41..cb50938 100644 --- a/server_reqres.go +++ b/server_reqres.go @@ -13,7 +13,11 @@ const requestTimeout = 10 * time.Second // Request ... func (sv Server) Request(subject string, payload []byte) (*nats.Msg, error) { - return sv.instance.Request(subject, payload, requestTimeout) + timeout := requestTimeout + if sv.Config.RequestTimeout > 0 { + timeout = sv.Config.RequestTimeout + } + return sv.instance.Request(subject, payload, timeout) } // Reply ...