-
Notifications
You must be signed in to change notification settings - Fork 782
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add config items for retry/cb/rcptimeout/limiter
- Loading branch information
Showing
6 changed files
with
330 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright 2021 CloudWeGo Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package circuitbreak | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/bytedance/sonic" | ||
"github.com/cloudwego/configmanager/iface" | ||
"github.com/cloudwego/configmanager/util" | ||
) | ||
|
||
var ( | ||
_ iface.ConfigValueItem = (*CBConfigItem)(nil) | ||
_ json.Marshaler = (*CBConfigItem)(nil) | ||
) | ||
|
||
// TypeCircuitBreaker is used as itemKey in ConfigValueImpl | ||
const TypeCircuitBreaker iface.ItemType = "cb_config" | ||
|
||
// CBConfigItem is an alias of CBConfig to meet the requirement of iface.ConfigValueItem | ||
type CBConfigItem CBConfig | ||
|
||
// CopyDefaultCBConfig returns a copy of default CBConfig, thus avoiding default values changed by business | ||
func CopyDefaultCBConfig() iface.ConfigValueItem { | ||
c := CBConfigItem(GetDefaultCBConfig()) | ||
return c.DeepCopy() | ||
} | ||
|
||
// NewCBConfig decodes json bytes to a new CBConfigItem | ||
var NewCBConfig = util.JsonInitializer(func() iface.ConfigValueItem { | ||
return &CBConfigItem{} | ||
}) | ||
|
||
// DeepCopy returns a copy of CBConfigItem | ||
func (c *CBConfigItem) DeepCopy() iface.ConfigValueItem { | ||
cb := ((*CBConfig)(c)).DeepCopy() | ||
return (*CBConfigItem)(cb) | ||
} | ||
|
||
// EqualsTo compares two CBConfigItem | ||
func (c *CBConfigItem) EqualsTo(other iface.ConfigValueItem) bool { | ||
x := (*CBConfig)(c) | ||
y := (*CBConfig)(other.(*CBConfigItem)) | ||
return x.Equals(y) | ||
} | ||
|
||
func (c *CBConfigItem) MarshalJSON() ([]byte, error) { | ||
return sonic.ConfigStd.MarshalIndent(c, "", "\t") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 2021 CloudWeGo Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package limiter | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/bytedance/sonic" | ||
"github.com/cloudwego/configmanager/iface" | ||
"github.com/cloudwego/configmanager/util" | ||
) | ||
|
||
var ( | ||
_ iface.ConfigValueItem = (*LimiterConfig)(nil) | ||
_ json.Marshaler = (*LimiterConfig)(nil) | ||
) | ||
|
||
// TypeLimiter serves as itemKey in ConfigValueImpl | ||
const TypeLimiter iface.ItemType = "limiter_config" | ||
|
||
var defaultLimiterConfig = &LimiterConfig{} | ||
|
||
// LimiterConfig represents the configuration for kitex server limiter | ||
// zero value means no limit. | ||
type LimiterConfig struct { | ||
ConnectionLimit int64 `json:"connection_limit"` | ||
QPSLimit int64 `json:"qps_limit"` | ||
QPSIntervalMS int64 `json:"qps_interval_ms"` | ||
} | ||
|
||
// NewLimiterConfig decodes json bytes into a newly allocated LimiterConfig object | ||
var NewLimiterConfig = util.JsonInitializer(func() iface.ConfigValueItem { | ||
return &LimiterConfig{} | ||
}) | ||
|
||
// CopyDefaultLimitConfig copies the default limiter configuration and returns a new instance of it | ||
func CopyDefaultLimitConfig() iface.ConfigValueItem { | ||
return defaultLimiterConfig.DeepCopy() | ||
} | ||
|
||
// DeepCopy makes a deep copy of LimiterConfig struct and returns a new instance of iface.ConfigValueItem | ||
func (l *LimiterConfig) DeepCopy() iface.ConfigValueItem { | ||
return &LimiterConfig{ | ||
ConnectionLimit: l.ConnectionLimit, | ||
QPSLimit: l.QPSLimit, | ||
QPSIntervalMS: l.QPSIntervalMS, | ||
} | ||
} | ||
|
||
// EqualsTo determines if the LimiterConfig is equal to the given ConfigValueItem. | ||
func (l *LimiterConfig) EqualsTo(item iface.ConfigValueItem) bool { | ||
o := item.(*LimiterConfig) | ||
return l.ConnectionLimit == o.ConnectionLimit && | ||
l.QPSLimit == o.QPSLimit && | ||
l.QPSIntervalMS == o.QPSIntervalMS | ||
} | ||
|
||
func (l *LimiterConfig) MarshalJSON() ([]byte, error) { | ||
return sonic.ConfigStd.MarshalIndent(l, "", "\t") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright 2021 CloudWeGo Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package retry | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/bytedance/sonic" | ||
"github.com/cloudwego/configmanager/iface" | ||
"github.com/cloudwego/configmanager/util" | ||
) | ||
|
||
var ( | ||
_ iface.ConfigValueItem = (*RetryConfig)(nil) | ||
_ json.Marshaler = (*RetryConfig)(nil) | ||
) | ||
|
||
// TypeRetry serves as itemKey in ConfigValueImpl | ||
const TypeRetry iface.ItemType = "retry_config" | ||
|
||
var defaultRetry = &RetryConfig{ | ||
Config: &Policy{}, | ||
} | ||
|
||
// RetryConfig serves as itemValue in ConfigValueImpl | ||
// It could have been an alias of Policy, but we need to keep compatibility with ByteDance's internal use | ||
type RetryConfig struct { | ||
Config *Policy `json:"config"` | ||
} | ||
|
||
// NewRetryConfig is a function decoding json bytes to a RetryConfig object | ||
var NewRetryConfig = util.JsonInitializer(func() iface.ConfigValueItem { | ||
return &RetryConfig{ | ||
Config: &Policy{}, | ||
} | ||
}) | ||
|
||
// CopyDefaultRetryConfig returns a copy of defaultRetry, thus avoiding default values changed by business | ||
func CopyDefaultRetryConfig() iface.ConfigValueItem { | ||
return defaultRetry.DeepCopy() | ||
} | ||
|
||
// DeepCopy returns a copy of the current RetryConfig | ||
func (c *RetryConfig) DeepCopy() iface.ConfigValueItem { | ||
return &RetryConfig{ | ||
Config: c.Config.DeepCopy(), | ||
} | ||
} | ||
|
||
// EqualsTo returns true if the current RetryConfig equals to the other one | ||
func (c *RetryConfig) EqualsTo(other iface.ConfigValueItem) bool { | ||
o := other.(*RetryConfig) | ||
return c.Config.Equals(*o.Config) | ||
} | ||
|
||
func (c *RetryConfig) MarshalJSON() ([]byte, error) { | ||
return sonic.ConfigStd.MarshalIndent(c, "", "\t") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* Copyright 2021 CloudWeGo Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package rpctimeout | ||
|
||
import ( | ||
"encoding/json" | ||
"time" | ||
|
||
"github.com/bytedance/sonic" | ||
"github.com/cloudwego/configmanager/iface" | ||
"github.com/cloudwego/configmanager/util" | ||
"github.com/cloudwego/kitex/pkg/rpcinfo" | ||
) | ||
|
||
var ( | ||
_ iface.ConfigValueItem = (*RPCTimeout)(nil) | ||
_ rpcinfo.Timeouts = (*RPCTimeout)(nil) | ||
_ json.Marshaler = (*RPCTimeout)(nil) | ||
) | ||
|
||
// TypeRPCTimeout is used as itemKey in ConfigValueImpl | ||
const TypeRPCTimeout iface.ItemType = "rpc_timeout" | ||
|
||
var defaultRPCTimeout = &RPCTimeout{ | ||
RPCTimeoutMS: 1000, | ||
ConnTimeoutMS: 1000, | ||
ReadTimeoutMS: 1000, | ||
WriteTimeoutMS: 1000, | ||
} | ||
|
||
// RPCTimeout is used as itemValue in ConfigValueImpl | ||
type RPCTimeout struct { | ||
RPCTimeoutMS int `json:"rpc_timeout_ms"` | ||
ConnTimeoutMS int `json:"conn_timeout_ms"` | ||
ReadTimeoutMS int `json:"read_timeout_ms"` | ||
WriteTimeoutMS int `json:"write_timeout_ms"` | ||
} | ||
|
||
// NewRPCTimeout is a function decoding json bytes to a RPCTimeout object | ||
var NewRPCTimeout = util.JsonInitializer(func() iface.ConfigValueItem { | ||
return &RPCTimeout{} | ||
}) | ||
|
||
// CopyDefaultRPCTimeout returns a copy of defaultRPCTimeout, thus avoiding default values changed by business | ||
func CopyDefaultRPCTimeout() iface.ConfigValueItem { | ||
return defaultRPCTimeout.DeepCopy() | ||
} | ||
|
||
// DeepCopy returns a copy of the current RPCTimeout | ||
func (r *RPCTimeout) DeepCopy() iface.ConfigValueItem { | ||
result := &RPCTimeout{ | ||
RPCTimeoutMS: r.RPCTimeoutMS, | ||
ConnTimeoutMS: r.ConnTimeoutMS, | ||
ReadTimeoutMS: r.ReadTimeoutMS, | ||
WriteTimeoutMS: r.WriteTimeoutMS, | ||
} | ||
return result | ||
} | ||
|
||
// EqualsTo returns true if the current RPCTimeout equals to the other RPCTimeout | ||
func (r *RPCTimeout) EqualsTo(other iface.ConfigValueItem) bool { | ||
o := other.(*RPCTimeout) | ||
return r.WriteTimeoutMS == o.WriteTimeoutMS && | ||
r.ReadTimeoutMS == o.ReadTimeoutMS && | ||
r.ConnTimeoutMS == o.ConnTimeoutMS && | ||
r.RPCTimeoutMS == o.RPCTimeoutMS | ||
} | ||
|
||
// RPCTimeout implements rpcinfo.Timeouts | ||
func (r *RPCTimeout) RPCTimeout() time.Duration { | ||
return time.Duration(r.RPCTimeoutMS) * time.Millisecond | ||
} | ||
|
||
// ConnectTimeout implements rpcinfo.Timeouts | ||
func (r *RPCTimeout) ConnectTimeout() time.Duration { | ||
return time.Duration(r.ConnTimeoutMS) * time.Millisecond | ||
} | ||
|
||
// ReadWriteTimeout implements rpcinfo.Timeouts | ||
func (r *RPCTimeout) ReadWriteTimeout() time.Duration { | ||
return time.Duration(r.ReadTimeoutMS) * time.Millisecond | ||
} | ||
|
||
func (r *RPCTimeout) MarshalJSON() ([]byte, error) { | ||
return sonic.ConfigStd.MarshalIndent(r, "", "\t") | ||
} |