generated from cloudwego/.github
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat(retry): support retry policy by nacos config
- Loading branch information
Showing
8 changed files
with
876 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package client | ||
|
||
import ( | ||
"github.com/cloudwego/kitex/client" | ||
"github.com/nacos-group/nacos-sdk-go/clients/config_client" | ||
|
||
"github.com/kitex-contrib/config-nacos/nacos" | ||
) | ||
|
||
// NacosClientSuite nacos client config suit, configure retry timeout limit and circuitbreak dynamically from nacos. | ||
type NacosClientSuite struct { | ||
nacosClient config_client.IConfigClient | ||
service string | ||
fns []nacos.CustomFunction | ||
} | ||
|
||
// NewSuit ... | ||
func NewSuit(service string, cli config_client.IConfigClient, cfs ...nacos.CustomFunction) *NacosClientSuite { | ||
return &NacosClientSuite{ | ||
service: service, | ||
nacosClient: cli, | ||
fns: cfs, | ||
} | ||
} | ||
|
||
// Options return a list client.Option | ||
func (s *NacosClientSuite) Options() []client.Option { | ||
opts := make([]client.Option, 0, 5) | ||
opts = append(opts, WithRetryPolicy(s.service, s.nacosClient)...) | ||
return opts | ||
} |
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,84 @@ | ||
package client | ||
|
||
import ( | ||
"github.com/kitex-contrib/config-nacos/nacos" | ||
"github.com/nacos-group/nacos-sdk-go/clients/config_client" | ||
"github.com/nacos-group/nacos-sdk-go/vo" | ||
"sigs.k8s.io/yaml" | ||
|
||
"github.com/cloudwego/kitex/client" | ||
"github.com/cloudwego/kitex/pkg/klog" | ||
"github.com/cloudwego/kitex/pkg/retry" | ||
) | ||
|
||
const ( | ||
retryPilocyName = "retry_config" | ||
) | ||
|
||
// WithRetryPolicy sets the retry policy from nacos config center. | ||
// | ||
// ConfigParam explain: | ||
// 1. Type: data format, only support json and yaml, JSON by default. customize it use CustomFunction. | ||
// 2. Context: empty by default, customize it use CustomFunction. | ||
// 3. Group: DEFAULT_GROUP by default. Customize it by CustomFunction or use specified foramt. ref: nacos/env.go:46 | ||
// 4. DataId: {{.ClientServiceName}}.{{.ServerServiceName}}.{{.PolicyName}} by default. Customize it by CustomFunction or | ||
// use specified foramt. ref: nacos/env.go:46 | ||
func WithRetryPolicy(dest string, nacosClient config_client.IConfigClient, cfs ...nacos.CustomFunction) []client.Option { | ||
param := nacos.NaocsConfigParam(&nacos.ConfigParamConfig{ | ||
PolicyName: retryPilocyName, | ||
ClientServiceName: dest, | ||
}) | ||
|
||
for _, cf := range cfs { | ||
cf(¶m) | ||
} | ||
|
||
return []client.Option{ | ||
client.WithRetryContainer(initRetryContainer(param, dest, nacosClient)), | ||
client.WithCloseCallbacks(func() error { | ||
// cancel the config listener when client is closed. | ||
return nacosClient.CancelListenConfig(param) | ||
}), | ||
} | ||
} | ||
|
||
func initRetryContainer(param vo.ConfigParam, dest string, nacosClient config_client.IConfigClient) *retry.Container { | ||
retryContainer := retry.NewRetryContainer() | ||
|
||
onChange := func(data string) { | ||
policies := map[string]*retry.Policy{} | ||
// Since YAML is a superset of JSON, it can parse JSON using a YAML parser | ||
err := yaml.Unmarshal([]byte(data), &policies) | ||
if err != nil { | ||
klog.Warnf("[nacos] %s client nacos retry: unmarshal data %s failed: %s, skip...", dest, data, err) | ||
return | ||
} | ||
for method, pilocy := range policies { | ||
if pilocy.BackupPolicy != nil && pilocy.FailurePolicy != nil { | ||
klog.Warnf("[nacos] %s client policy for method %s BackupPolicy and FailurePolicy must not be set at same time", | ||
dest, method) | ||
continue | ||
} | ||
retryContainer.NotifyPolicyChange(method, *pilocy) | ||
} | ||
} | ||
|
||
param.OnChange = func(namespace, group, dataId, data string) { | ||
klog.Debugf("[nacos] %s client retry config updated, namespace %s group %s dataId %s data %s", | ||
dest, namespace, group, dataId, data) | ||
onChange(data) | ||
} | ||
|
||
data, err := nacosClient.GetConfig(param) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
onChange(data) | ||
|
||
err = nacosClient.ListenConfig(param) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return retryContainer | ||
} |
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,57 @@ | ||
module github.com/kitex-contrib/config-nacos | ||
|
||
go 1.19 | ||
|
||
require ( | ||
github.com/cloudwego/kitex v0.6.1 | ||
github.com/nacos-group/nacos-sdk-go v1.1.4 | ||
github.com/stretchr/testify v1.8.2 | ||
sigs.k8s.io/yaml v1.3.0 | ||
) | ||
|
||
require ( | ||
github.com/aliyun/alibaba-cloud-sdk-go v1.61.18 // indirect | ||
github.com/apache/thrift v0.13.0 // indirect | ||
github.com/buger/jsonparser v1.1.1 // indirect | ||
github.com/bytedance/gopkg v0.0.0-20230531144706-a12972768317 // indirect | ||
github.com/bytedance/sonic v1.8.8 // indirect | ||
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect | ||
github.com/chenzhuoyu/iasm v0.0.0-20230222070914-0b1b64b0e762 // indirect | ||
github.com/choleraehyq/pid v0.0.16 // indirect | ||
github.com/cloudwego/configmanager v0.2.0 // indirect | ||
github.com/cloudwego/fastpb v0.0.4 // indirect | ||
github.com/cloudwego/frugal v0.1.6 // indirect | ||
github.com/cloudwego/netpoll v0.4.0 // indirect | ||
github.com/cloudwego/thriftgo v0.2.11 // indirect | ||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect | ||
github.com/go-errors/errors v1.0.1 // indirect | ||
github.com/golang/protobuf v1.5.2 // indirect | ||
github.com/google/pprof v0.0.0-20220608213341-c488b8fa1db3 // indirect | ||
github.com/jhump/protoreflect v1.8.2 // indirect | ||
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af // indirect | ||
github.com/json-iterator/go v1.1.12 // indirect | ||
github.com/klauspost/cpuid/v2 v2.2.4 // indirect | ||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect | ||
github.com/modern-go/reflect2 v1.0.2 // indirect | ||
github.com/oleiade/lane v1.0.1 // indirect | ||
github.com/pkg/errors v0.9.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/tidwall/gjson v1.9.3 // indirect | ||
github.com/tidwall/match v1.1.1 // indirect | ||
github.com/tidwall/pretty v1.2.0 // indirect | ||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect | ||
go.uber.org/atomic v1.6.0 // indirect | ||
go.uber.org/multierr v1.5.0 // indirect | ||
go.uber.org/zap v1.15.0 // indirect | ||
golang.org/x/arch v0.2.0 // indirect | ||
golang.org/x/net v0.0.0-20221014081412-f15817d10f9b // indirect | ||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect | ||
golang.org/x/sys v0.0.0-20220817070843-5a390386f1f2 // indirect | ||
golang.org/x/text v0.6.0 // indirect | ||
google.golang.org/genproto v0.0.0-20210513213006-bf773b8c8384 // indirect | ||
google.golang.org/protobuf v1.28.1 // indirect | ||
gopkg.in/ini.v1 v1.42.0 // indirect | ||
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect | ||
gopkg.in/yaml.v2 v2.4.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
Oops, something went wrong.