Add transport dial timeout
This commit is contained in:
@@ -276,17 +276,19 @@ func (h *httpTransportListener) Accept(fn func(Socket)) error {
|
||||
}
|
||||
|
||||
func (h *httpTransport) Dial(addr string, opts ...DialOption) (Client, error) {
|
||||
conn, err := net.Dial("tcp", addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
dopts := DialOptions{
|
||||
Timeout: DefaultDialTimeout,
|
||||
}
|
||||
|
||||
var dopts DialOptions
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(&dopts)
|
||||
}
|
||||
|
||||
conn, err := net.DialTimeout("tcp", addr, dopts.Timeout)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &httpTransportClient{
|
||||
ht: h,
|
||||
addr: addr,
|
||||
|
@@ -1,5 +1,9 @@
|
||||
package transport
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type Message struct {
|
||||
Header map[string]string
|
||||
Body []byte
|
||||
@@ -35,7 +39,8 @@ type Options struct {
|
||||
}
|
||||
|
||||
type DialOptions struct {
|
||||
Stream bool
|
||||
Stream bool
|
||||
Timeout time.Duration
|
||||
|
||||
// Other options to be used by broker implementations
|
||||
Options map[string]string
|
||||
@@ -47,6 +52,8 @@ type DialOption func(*DialOptions)
|
||||
|
||||
var (
|
||||
DefaultTransport Transport = newHttpTransport([]string{})
|
||||
|
||||
DefaultDialTimeout = time.Second * 5
|
||||
)
|
||||
|
||||
func WithStream() DialOption {
|
||||
@@ -55,6 +62,12 @@ func WithStream() DialOption {
|
||||
}
|
||||
}
|
||||
|
||||
func WithTimeout(d time.Duration) DialOption {
|
||||
return func(o *DialOptions) {
|
||||
o.Timeout = d
|
||||
}
|
||||
}
|
||||
|
||||
func NewTransport(addrs []string, opt ...Option) Transport {
|
||||
return newHttpTransport(addrs, opt...)
|
||||
}
|
||||
|
Reference in New Issue
Block a user