From dd22fdb1c82ae93446f5e1166d500331c0ad6a1d Mon Sep 17 00:00:00 2001 From: zhangmeng <775834166@qq.com> Date: 星期一, 29 七月 2019 13:52:16 +0800 Subject: [PATCH] add timeout interface --- deliver.go | 32 ++++++++++++++++++++++++++++++++ nngopt.go | 6 +++++- 2 files changed, 37 insertions(+), 1 deletions(-) diff --git a/deliver.go b/deliver.go index d518da2..c22dc79 100644 --- a/deliver.go +++ b/deliver.go @@ -76,3 +76,35 @@ return nil, errors.New("please choose a suitable deliver.mode") } + +// NewServerWithTimeout with timeout +func NewServerWithTimeout(m Mode, url string, tm int, args ...interface{}) (Deliver, error) { + if m > ModeStart && m < ModeNNG { + setTimeOut(tm) + s := nngServer(m, url, args...) + if s == nil { + return nil, errors.New("nng create server error") + } + return s, nil + } else if m == Shm { + return shmServer(m, url, args...) + } + return nil, errors.New("please choose a suitable deliver.mode") +} + +// NewClientWithTimeout with timeout, tm unit millisecond +func NewClientWithTimeout(m Mode, url string, tm int, args ...interface{}) (Deliver, error) { + if m > ModeStart && m < ModeNNG { + setTimeOut(tm) + + s := nngClient(m, url, args...) + if s == nil { + return nil, errors.New("nng create client error") + } + return s, nil + } else if m == Shm { + return shmClient(m, url, args...) + } + + return nil, errors.New("please choose a suitable deliver.mode") +} diff --git a/nngopt.go b/nngopt.go index 0ffb761..bd7aa65 100644 --- a/nngopt.go +++ b/nngopt.go @@ -10,10 +10,14 @@ var ( maxRecvSize = 33 * 1024 * 1024 surveyorTime = 0 + timeout = time.Duration(25) ) +func setTimeOut(tm int) { + timeout = time.Duration(tm) +} + func optDefault() map[string]interface{} { - timeout := time.Duration(25) options := make(map[string]interface{}) -- Gitblit v1.8.0