diff --git a/jsonrpc/http.go b/jsonrpc/http.go index aa249e7a3b2b4547853512d9af69eec987f5a7fd..53bece91f4c9f75a9882346b323eb5019b360890 100644 --- a/jsonrpc/http.go +++ b/jsonrpc/http.go @@ -9,6 +9,7 @@ import ( "net" "net/http" "net/url" + "os" "strings" "sync/atomic" "time" @@ -80,7 +81,7 @@ func NewHTTPClient(opt *HTTPOptions) *HTTPClient { t := time.Now() return &HTTPClient{ - ID: int64(uint32(t.Second() * t.Nanosecond())), + ID: int64(uint32(os.Getpid() * t.Second() * t.Nanosecond())), options: *opt, } } @@ -104,13 +105,8 @@ func (c *HTTPClient) Call(ctx context.Context, service registry.ServiceURL, req httpHeader.Set("Accept", "application/json") reqTimeout := c.options.HTTPTimeout - if len(service.Query.Get("timeout")) != 0 { - - if timeout, err := time.ParseDuration(service.Query.Get("timeout") + "s"); err == nil { - if timeout < reqTimeout { - reqTimeout = timeout - } - } + if service.Timeout != 0 && service.Timeout < reqTimeout { + reqTimeout = time.Duration(service.Timeout) } if reqTimeout <= 0 { reqTimeout = 1e8 diff --git a/registry/service_url.go b/registry/service_url.go index 66dd6f95f5538beb36c89722185cfe22d5b35338..ecce3781162bdfb6f2399ae31e6e6d1e072e4825 100644 --- a/registry/service_url.go +++ b/registry/service_url.go @@ -5,12 +5,11 @@ import ( "math/rand" "net" "net/url" + "strconv" "strings" "sync/atomic" "time" -) -import ( jerrors "github.com/juju/errors" ) @@ -90,6 +89,7 @@ type ServiceURL struct { Path string // like /com.ikurento.dubbo.UserProvider3 Ip string Port string + Timeout time.Duration Version string Group string Query url.Values @@ -100,9 +100,9 @@ type ServiceURL struct { func (s ServiceURL) String() string { return fmt.Sprintf( "ServiceURL{Protocol:%s, Location:%s, Path:%s, Ip:%s, Port:%s, "+ - "Version:%s, Group:%s, Weight:%d, Query:%+v}", + "Timeout:%s, Version:%s, Group:%s, Weight:%d, Query:%+v}", s.Protocol, s.Location, s.Path, s.Ip, s.Port, - s.Version, s.Group, s.Weight, s.Query) + s.Timeout, s.Version, s.Group, s.Weight, s.Query) } func NewServiceURL(urlString string) (*ServiceURL, error) { @@ -140,6 +140,16 @@ func NewServiceURL(urlString string) (*ServiceURL, error) { } s.Group = s.Query.Get("group") s.Version = s.Query.Get("version") + timeoutStr := s.Query.Get("timeout") + if len(timeoutStr) == 0 { + timeoutStr = s.Query.Get("default.timeout") + } + if len(timeoutStr) != 0 { + timeout, err := strconv.Atoi(timeoutStr) + if err == nil && timeout != 0 { + s.Timeout = time.Duration(timeout * 1e6) // timeout unit is millisecond + } + } return s, nil }