Skip to content

Commit

Permalink
feat: add config items for retry/cb/rcptimeout/limiter
Browse files Browse the repository at this point in the history
  • Loading branch information
felix021 committed May 16, 2023
1 parent acea491 commit 0ce886c
Show file tree
Hide file tree
Showing 6 changed files with 330 additions and 1 deletion.
2 changes: 2 additions & 0 deletions go.mod
Expand Up @@ -6,7 +6,9 @@ require (
github.com/apache/thrift v0.13.0
github.com/bytedance/gopkg v0.0.0-20220817015305-b879a72dc90f
github.com/bytedance/mockey v1.2.0
github.com/bytedance/sonic v1.8.8
github.com/choleraehyq/pid v0.0.16
github.com/cloudwego/configmanager v0.1.0
github.com/cloudwego/fastpb v0.0.4
github.com/cloudwego/frugal v0.1.6
github.com/cloudwego/netpoll v0.3.2
Expand Down
19 changes: 18 additions & 1 deletion go.sum
Expand Up @@ -17,7 +17,13 @@ github.com/bytedance/gopkg v0.0.0-20220817015305-b879a72dc90f h1:U3Bk6S9UyqFM5tU
github.com/bytedance/gopkg v0.0.0-20220817015305-b879a72dc90f/go.mod h1:2ZlV9BaUH4+NXIBF0aMdKKAnHTzqH+iMU4KUjAbL23Q=
github.com/bytedance/mockey v1.2.0 h1:847+X2fBSM4s/AIN4loO5d16PCgEj53j7Q8YVB+8P6c=
github.com/bytedance/mockey v1.2.0/go.mod h1:+Jm/fzWZAuhEDrPXVjDf/jLM2BlLXJkwk94zf2JZ3X4=
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
github.com/bytedance/sonic v1.8.8 h1:Kj4AYbZSeENfyXicsYppYKO0K2YWab+i2UTSY7Ukz9Q=
github.com/bytedance/sonic v1.8.8/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhDF8vLC+iwCD4WpbV1EBDSzWkJODFLams=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
github.com/chenzhuoyu/iasm v0.0.0-20230222070914-0b1b64b0e762 h1:4+00EOUb1t9uxAbgY8VvgfKJKDpim3co4MqsAbelIbs=
github.com/chenzhuoyu/iasm v0.0.0-20230222070914-0b1b64b0e762/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog=
github.com/choleraehyq/pid v0.0.16 h1:1/714sMH9IBlE/aK6xM0acTagGKSzpiR0bDt7l0cG7o=
Expand All @@ -26,6 +32,8 @@ github.com/chzyer/logex v1.2.0/go.mod h1:9+9sk7u7pGNWYMkh0hdiL++6OeibzJccyQU4p4M
github.com/chzyer/readline v1.5.0/go.mod h1:x22KAscuvRqlLoK9CsoYsmxoXZMMFVyOl86cAH8qUic=
github.com/chzyer/test v0.0.0-20210722231415-061457976a23/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cloudwego/configmanager v0.1.0 h1:mOffVH7GPffGgVHmGFWmT1crTYHYGSn3dw05I8dwoYI=
github.com/cloudwego/configmanager v0.1.0/go.mod h1:FLIQTjxsZRGjnmDhTttWQTy6f6DghPTatfBVOs2gQLk=
github.com/cloudwego/fastpb v0.0.4 h1:/ROVVfoFtpfc+1pkQLzGs+azjxUbSOsAqSY4tAAx4mg=
github.com/cloudwego/fastpb v0.0.4/go.mod h1:/V13XFTq2TUkxj2qWReV8MwfPC4NnPcy6FsrojnsSG0=
github.com/cloudwego/frugal v0.1.6 h1:aXJ7W0Omion1WTCe4JHAWinQmjXDYzHt03sabu3Rabo=
Expand Down Expand Up @@ -100,6 +108,7 @@ github.com/jung-kurt/gofpdf v1.0.3-0.20190309125859-24315acbbda5/go.mod h1:7Id9E
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk=
github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY=
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
Expand Down Expand Up @@ -130,24 +139,32 @@ github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/tidwall/gjson v1.9.3 h1:hqzS9wAHMO+KVBBkLxYdkEeeFHuqr95GfClRLKlgK0E=
github.com/tidwall/gjson v1.9.3/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/arch v0.0.0-20201008161808-52c3e6f60cff/go.mod h1:flIaEI6LNU6xOCD5PaJvn9wGP0agmIOqjrtsKGRguv4=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/arch v0.2.0 h1:W1sUEHXiJTfjaFJ5SLo0N6lZn+0eO5gWD1MFeTGqQEY=
golang.org/x/arch v0.2.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down
64 changes: 64 additions & 0 deletions pkg/circuitbreak/item_circuit_breaker.go
@@ -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")
}
74 changes: 74 additions & 0 deletions pkg/limiter/item_limiter.go
@@ -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")
}
72 changes: 72 additions & 0 deletions pkg/retry/item_retry.go
@@ -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")
}
100 changes: 100 additions & 0 deletions pkg/rpctimeout/item_rpc_timeout.go
@@ -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")
}

0 comments on commit 0ce886c

Please sign in to comment.