diff --git a/cluster/cluster_impl/forking_cluster_invoker.go b/cluster/cluster_impl/forking_cluster_invoker.go index 168444881653ca38ef61a9bc8e50f2d4bc3e624c..3ffda580ce9847bb60a0a837c5fac6b420450141 100644 --- a/cluster/cluster_impl/forking_cluster_invoker.go +++ b/cluster/cluster_impl/forking_cluster_invoker.go @@ -56,7 +56,7 @@ func (invoker *forkingClusterInvoker) Invoke(ctx context.Context, invocation pro } var selected []protocol.Invoker - forks := int(invoker.GetUrl().GetParamInt(constant.FORKS_KEY, constant.DEFAULT_FORKS)) + forks := invoker.GetUrl().GetParamByIntValue(constant.FORKS_KEY, constant.DEFAULT_FORKS) timeouts := invoker.GetUrl().GetParamInt(constant.TIMEOUT_KEY, constant.DEFAULT_TIMEOUT) if forks < 0 || forks > len(invokers) { selected = invokers diff --git a/cluster/loadbalance/consistent_hash.go b/cluster/loadbalance/consistent_hash.go index 27ce2369f9de6dfe76bd35581ea26f0e0c24e480..3d036b4f3c5c50874e09efb8fd1ef34e969585d8 100644 --- a/cluster/loadbalance/consistent_hash.go +++ b/cluster/loadbalance/consistent_hash.go @@ -105,7 +105,7 @@ func newConsistentHashSelector(invokers []protocol.Invoker, methodName string, selector.virtualInvokers = make(map[uint32]protocol.Invoker) selector.hashCode = hashCode url := invokers[0].GetUrl() - selector.replicaNum = int(url.GetMethodParamInt(methodName, HashNodes, 160)) + selector.replicaNum = url.GetMethodParamIntValue(methodName, HashNodes, 160) indices := re.Split(url.GetMethodParam(methodName, HashArguments, "0"), -1) for _, index := range indices { i, err := strconv.Atoi(index) diff --git a/cluster/router/healthcheck/default_health_check.go b/cluster/router/healthcheck/default_health_check.go index 409408bb942485f71a782f9240e69c82410ab8b9..3effd779c989ed8e68c91eb6eab20d10d8b04e8c 100644 --- a/cluster/router/healthcheck/default_health_check.go +++ b/cluster/router/healthcheck/default_health_check.go @@ -96,6 +96,7 @@ func (c *DefaultHealthChecker) getCircuitBreakerSleepWindowTime(status *protocol return int64(sleepWindow) } + // GetRequestSuccessiveFailureThreshold return the requestSuccessiveFailureThreshold bound to this DefaultHealthChecker func (c *DefaultHealthChecker) GetRequestSuccessiveFailureThreshold() int32 { return c.requestSuccessiveFailureThreshold @@ -114,8 +115,8 @@ func (c *DefaultHealthChecker) GetOutStandingRequestCountLimit() int32 { // NewDefaultHealthChecker constructs a new DefaultHealthChecker based on the url func NewDefaultHealthChecker(url *common.URL) router.HealthChecker { return &DefaultHealthChecker{ - outStandingRequestConutLimit: int32(url.GetParamInt(constant.OUTSTANDING_REQUEST_COUNT_LIMIT_KEY, math.MaxInt32)), - requestSuccessiveFailureThreshold: int32(url.GetParamInt(constant.SUCCESSIVE_FAILED_REQUEST_THRESHOLD_KEY, constant.DEFAULT_SUCCESSIVE_FAILED_REQUEST_MAX_DIFF)), - circuitTrippedTimeoutFactor: int32(url.GetParamInt(constant.CIRCUIT_TRIPPED_TIMEOUT_FACTOR_KEY, constant.DEFAULT_CIRCUIT_TRIPPED_TIMEOUT_FACTOR)), + outStandingRequestConutLimit: url.GetParamInt32(constant.OUTSTANDING_REQUEST_COUNT_LIMIT_KEY, math.MaxInt32), + requestSuccessiveFailureThreshold: url.GetParamInt32(constant.SUCCESSIVE_FAILED_REQUEST_THRESHOLD_KEY, constant.DEFAULT_SUCCESSIVE_FAILED_REQUEST_MAX_DIFF), + circuitTrippedTimeoutFactor: url.GetParamInt32(constant.CIRCUIT_TRIPPED_TIMEOUT_FACTOR_KEY, constant.DEFAULT_CIRCUIT_TRIPPED_TIMEOUT_FACTOR), } } diff --git a/common/url.go b/common/url.go index 8297afdaad235cbd9a70b057e0f43db6c48dbf82..3a88001c5fb345aa004e5a3975df89cbec2c415a 100644 --- a/common/url.go +++ b/common/url.go @@ -484,22 +484,49 @@ func (c URL) GetParamBool(key string, d bool) bool { return r } -// GetParamInt gets int value by @key +// GetParamInt gets int64 value by @key func (c URL) GetParamInt(key string, d int64) int64 { - r, err := strconv.Atoi(c.GetParam(key, "")) - if r == 0 || err != nil { + r, err := strconv.ParseInt(c.GetParam(key, ""), 10, 64) + if err != nil { return d } - return int64(r) + return r +} + +// GetParamInt32 gets int32 value by @key +func (c URL) GetParamInt32(key string, d int32) int32 { + r, err := strconv.ParseInt(c.GetParam(key, ""), 10, 32) + if err != nil { + return d + } + return int32(r) +} + +// GetParamByIntValue gets int value by @key +func (c URL) GetParamByIntValue(key string, d int) int { + r, err := strconv.ParseInt(c.GetParam(key, ""), 10, 0) + if err != nil { + return d + } + return int(r) } // GetMethodParamInt gets int method param func (c URL) GetMethodParamInt(method string, key string, d int64) int64 { - r, err := strconv.Atoi(c.GetParam("methods."+method+"."+key, "")) - if r == 0 || err != nil { + r, err := strconv.ParseInt(c.GetParam("methods."+method+"."+key, ""), 10, 64) + if err != nil { + return d + } + return r +} + +// GetMethodParamIntValue gets int method param +func (c URL) GetMethodParamIntValue(method string, key string, d int) int { + r, err := strconv.ParseInt(c.GetParam("methods."+method+"."+key, ""), 10, 0) + if err != nil { return d } - return int64(r) + return int(r) } // GetMethodParamInt64 gets int64 method param diff --git a/common/url_test.go b/common/url_test.go index 6845190a7362571ebbd4738bd146c94f6d644253..12020f4289904d22c0d64a6df1cea2903394a016 100644 --- a/common/url_test.go +++ b/common/url_test.go @@ -167,10 +167,22 @@ func TestURLGetParam(t *testing.T) { func TestURLGetParamInt(t *testing.T) { params := url.Values{} - params.Set("key", "3") + params.Set("key", "") u := URL{baseUrl: baseUrl{params: params}} v := u.GetParamInt("key", 1) - assert.Equal(t, int64(3), v) + assert.Equal(t, int64(1), v) + + u = URL{} + v = u.GetParamInt("key", 1) + assert.Equal(t, int64(1), v) +} + +func TestURLGetParamIntValue(t *testing.T) { + params := url.Values{} + params.Set("key", "0") + u := URL{baseUrl: baseUrl{params: params}} + v := u.GetParamInt("key", 1) + assert.Equal(t, int64(0), v) u = URL{} v = u.GetParamInt("key", 1)