generated from cloudwego/.github
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from ViolaPioggia/main
feat:add client && fix etcd config watcher
- Loading branch information
Showing
11 changed files
with
499 additions
and
36 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,114 @@ | ||
// Copyright 2023 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 client | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
|
||
"github.com/cloudwego/kitex/pkg/klog" | ||
|
||
"github.com/cloudwego/kitex/client" | ||
"github.com/cloudwego/kitex/pkg/circuitbreak" | ||
"github.com/cloudwego/kitex/pkg/rpcinfo" | ||
"github.com/kitex-contrib/config-etcd/etcd" | ||
"github.com/kitex-contrib/config-etcd/utils" | ||
) | ||
|
||
// WithCircuitBreaker sets the circuit breaker policy from etcd configuration center. | ||
func WithCircuitBreaker(dest, src string, etcdClient etcd.Client, uniqueID int64, opts utils.Options) []client.Option { | ||
param, err := etcdClient.ClientConfigParam(&etcd.ConfigParamConfig{ | ||
Category: circuitBreakerConfigName, | ||
ServerServiceName: dest, | ||
ClientServiceName: src, | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
for _, f := range opts.EtcdCustomFunctions { | ||
f(¶m) | ||
} | ||
key := param.Prefix + "/" + param.Path | ||
cbSuite := initCircuitBreaker(key, dest, src, etcdClient, uniqueID) | ||
|
||
return []client.Option{ | ||
client.WithCircuitBreaker(cbSuite), | ||
client.WithCloseCallbacks(func() error { | ||
// cancel the configuration listener when client is closed. | ||
etcdClient.DeregisterConfig(key, uniqueID) | ||
err = cbSuite.Close() | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
}), | ||
} | ||
} | ||
|
||
// keep consistent when initialising the circuit breaker suit and updating | ||
// the circuit breaker policy. | ||
func genServiceCBKeyWithRPCInfo(ri rpcinfo.RPCInfo) string { | ||
if ri == nil { | ||
return "" | ||
} | ||
return genServiceCBKey(ri.To().ServiceName(), ri.To().Method()) | ||
} | ||
|
||
func genServiceCBKey(toService, method string) string { | ||
sum := len(toService) + len(method) + 2 | ||
var buf strings.Builder | ||
buf.Grow(sum) | ||
buf.WriteString(toService) | ||
buf.WriteByte('/') | ||
buf.WriteString(method) | ||
return buf.String() | ||
} | ||
|
||
func initCircuitBreaker(key, dest, src string, | ||
etcdClient etcd.Client, uniqueID int64, | ||
) *circuitbreak.CBSuite { | ||
cb := circuitbreak.NewCBSuite(genServiceCBKeyWithRPCInfo) | ||
lcb := utils.ThreadSafeSet{} | ||
|
||
onChangeCallback := func(restoreDefault bool, data string, parser etcd.ConfigParser) { | ||
set := utils.Set{} | ||
configs := map[string]circuitbreak.CBConfig{} | ||
|
||
if !restoreDefault { | ||
err := parser.Decode(data, &configs) | ||
if err != nil { | ||
klog.Warnf("[etcd] %s client etcd circuit breaker: unmarshal data %s failed: %s, skip...", key, data, err) | ||
return | ||
} | ||
} | ||
|
||
for method, config := range configs { | ||
set[method] = true | ||
key := genServiceCBKey(dest, method) | ||
cb.UpdateServiceCBConfig(key, config) | ||
} | ||
|
||
for _, method := range lcb.DiffAndEmplace(set) { | ||
key := genServiceCBKey(dest, method) | ||
// For deleted method configs, set to default policy | ||
cb.UpdateServiceCBConfig(key, circuitbreak.GetDefaultCBConfig()) | ||
} | ||
} | ||
|
||
etcdClient.RegisterConfigCallback(context.Background(), key, uniqueID, onChangeCallback) | ||
|
||
return cb | ||
} |
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,92 @@ | ||
// Copyright 2023 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 client | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/cloudwego/kitex/client" | ||
"github.com/cloudwego/kitex/pkg/klog" | ||
"github.com/cloudwego/kitex/pkg/retry" | ||
"github.com/kitex-contrib/config-etcd/etcd" | ||
"github.com/kitex-contrib/config-etcd/utils" | ||
) | ||
|
||
// WithRetryPolicy sets the retry policy from etcd configuration center. | ||
func WithRetryPolicy(dest, src string, etcdClient etcd.Client, uniqueID int64, opts utils.Options) []client.Option { | ||
param, err := etcdClient.ClientConfigParam(&etcd.ConfigParamConfig{ | ||
Category: retryConfigName, | ||
ServerServiceName: dest, | ||
ClientServiceName: src, | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
for _, f := range opts.EtcdCustomFunctions { | ||
f(¶m) | ||
} | ||
key := param.Prefix + "/" + param.Path | ||
rc := initRetryContainer(key, dest, etcdClient, uniqueID) | ||
return []client.Option{ | ||
client.WithRetryContainer(rc), | ||
client.WithCloseCallbacks(func() error { | ||
// cancel the configuration listener when client is closed. | ||
etcdClient.DeregisterConfig(key, uniqueID) | ||
return nil | ||
}), | ||
client.WithCloseCallbacks(rc.Close), | ||
} | ||
} | ||
|
||
func initRetryContainer(key, dest string, | ||
etcdClient etcd.Client, uniqueID int64, | ||
) *retry.Container { | ||
retryContainer := retry.NewRetryContainerWithPercentageLimit() | ||
|
||
ts := utils.ThreadSafeSet{} | ||
|
||
onChangeCallback := func(restoreDefault bool, data string, parser etcd.ConfigParser) { | ||
// the key is method name, wildcard "*" can match anything. | ||
rcs := map[string]*retry.Policy{} | ||
|
||
if !restoreDefault { | ||
err := parser.Decode(data, &rcs) | ||
if err != nil { | ||
klog.Warnf("[etcd] %s client etcd retry: unmarshal data %s failed: %s, skip...", key, data, err) | ||
return | ||
} | ||
} | ||
|
||
set := utils.Set{} | ||
for method, policy := range rcs { | ||
set[method] = true | ||
if policy.Enable && policy.BackupPolicy == nil && policy.FailurePolicy == nil { | ||
klog.Warnf("[etcd] %s client policy for method %s BackupPolicy and FailurePolicy must not be empty at same time", | ||
dest, method) | ||
continue | ||
} | ||
retryContainer.NotifyPolicyChange(method, *policy) | ||
} | ||
|
||
for _, method := range ts.DiffAndEmplace(set) { | ||
retryContainer.DeletePolicy(method) | ||
} | ||
} | ||
|
||
etcdClient.RegisterConfigCallback(context.Background(), key, uniqueID, onChangeCallback) | ||
|
||
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,73 @@ | ||
// Copyright 2023 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 client | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/cloudwego/kitex/client" | ||
"github.com/cloudwego/kitex/pkg/klog" | ||
"github.com/cloudwego/kitex/pkg/rpcinfo" | ||
"github.com/cloudwego/kitex/pkg/rpctimeout" | ||
"github.com/kitex-contrib/config-etcd/etcd" | ||
"github.com/kitex-contrib/config-etcd/utils" | ||
) | ||
|
||
// WithRPCTimeout sets the RPC timeout policy from etcd configuration center. | ||
func WithRPCTimeout(dest, src string, etcdClient etcd.Client, uniqueID int64, opts utils.Options) []client.Option { | ||
param, err := etcdClient.ClientConfigParam(&etcd.ConfigParamConfig{ | ||
Category: rpcTimeoutConfigName, | ||
ServerServiceName: dest, | ||
ClientServiceName: src, | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
for _, f := range opts.EtcdCustomFunctions { | ||
f(¶m) | ||
} | ||
key := param.Prefix + "/" + param.Path | ||
return []client.Option{ | ||
client.WithTimeoutProvider(initRPCTimeoutContainer(key, dest, etcdClient, uniqueID)), | ||
client.WithCloseCallbacks(func() error { | ||
// cancel the configuration listener when client is closed. | ||
etcdClient.DeregisterConfig(key, uniqueID) | ||
return nil | ||
}), | ||
} | ||
} | ||
|
||
func initRPCTimeoutContainer(key, dest string, | ||
etcdClient etcd.Client, uniqueID int64, | ||
) rpcinfo.TimeoutProvider { | ||
rpcTimeoutContainer := rpctimeout.NewContainer() | ||
|
||
onChangeCallback := func(restoreDefault bool, data string, parser etcd.ConfigParser) { | ||
configs := map[string]*rpctimeout.RPCTimeout{} | ||
if !restoreDefault { | ||
err := parser.Decode(data, &configs) | ||
if err != nil { | ||
klog.Warnf("[etcd] %s client etcd rpc timeout: unmarshal data %s failed: %s, skip...", key, data, err) | ||
return | ||
} | ||
} | ||
rpcTimeoutContainer.NotifyPolicyChange(configs) | ||
} | ||
|
||
etcdClient.RegisterConfigCallback(context.Background(), key, uniqueID, onChangeCallback) | ||
|
||
return rpcTimeoutContainer | ||
} |
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,62 @@ | ||
// Copyright 2023 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 client | ||
|
||
import ( | ||
"github.com/cloudwego/kitex/client" | ||
"github.com/kitex-contrib/config-etcd/etcd" | ||
"github.com/kitex-contrib/config-etcd/utils" | ||
) | ||
|
||
const ( | ||
retryConfigName = "retry" | ||
rpcTimeoutConfigName = "rpc_timeout" | ||
circuitBreakerConfigName = "circuit_break" | ||
) | ||
|
||
// EtcdClientSuite etcd client config suite, configure retry timeout limit and circuitbreak dynamically from etcd. | ||
type EtcdClientSuite struct { | ||
uid int64 | ||
etcdClient etcd.Client | ||
service string | ||
client string | ||
opts utils.Options | ||
} | ||
|
||
// NewSuite service is the destination service name and client is the local identity. | ||
func NewSuite(service, client string, cli etcd.Client, | ||
opts ...utils.Option, | ||
) *EtcdClientSuite { | ||
uid := etcd.AllocateUniqueID() | ||
su := &EtcdClientSuite{ | ||
uid: uid, | ||
service: service, | ||
client: client, | ||
etcdClient: cli, | ||
} | ||
for _, opt := range opts { | ||
opt.Apply(&su.opts) | ||
} | ||
return su | ||
} | ||
|
||
// Options return a list client.Option | ||
func (s *EtcdClientSuite) Options() []client.Option { | ||
opts := make([]client.Option, 0, 7) | ||
opts = append(opts, WithRetryPolicy(s.service, s.client, s.etcdClient, s.uid, s.opts)...) | ||
opts = append(opts, WithRPCTimeout(s.service, s.client, s.etcdClient, s.uid, s.opts)...) | ||
opts = append(opts, WithCircuitBreaker(s.service, s.client, s.etcdClient, s.uid, s.opts)...) | ||
return opts | ||
} |
Oops, something went wrong.