generated from cloudwego/.github
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(circuitbreak): support config circuitbreak policy with xds
- Loading branch information
Showing
4 changed files
with
150 additions
and
4 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
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,84 @@ | ||
/* | ||
* Copyright 2024 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 xdssuite | ||
|
||
import ( | ||
"sync/atomic" | ||
|
||
"github.com/cloudwego/kitex/client" | ||
"github.com/cloudwego/kitex/pkg/circuitbreak" | ||
"github.com/cloudwego/kitex/pkg/rpcinfo" | ||
) | ||
|
||
type circuitBreaker struct { | ||
lastPolicies atomic.Value | ||
cb *circuitbreak.CBSuite | ||
} | ||
|
||
func (cb *circuitBreaker) updateAllCircuitConfigs(configs map[string]circuitbreak.CBConfig) { | ||
if cb.cb == nil { | ||
return | ||
} | ||
policies := make(map[string]struct{}) | ||
for k, v := range configs { | ||
cb.cb.UpdateServiceCBConfig(k, v) | ||
policies[k] = struct{}{} | ||
} | ||
|
||
defer cb.lastPolicies.Store(policies) | ||
|
||
val := cb.lastPolicies.Load() | ||
if val == nil { | ||
return | ||
} | ||
lastPolicies, ok := val.(map[string]struct{}) | ||
if !ok { | ||
return | ||
} | ||
// disable the old policies that are not in the new configs. | ||
for k := range lastPolicies { | ||
if _, ok := policies[k]; !ok { | ||
cb.cb.UpdateServiceCBConfig(k, circuitbreak.CBConfig{ | ||
Enable: false, | ||
}) | ||
} | ||
} | ||
} | ||
|
||
// NewCircuitBreaker integrate xds config and kitex circuitbreaker | ||
func NewCircuitBreaker() client.Option { | ||
m := xdsResourceManager.getManager() | ||
if m == nil { | ||
panic("xds resource manager has not been initialized") | ||
} | ||
|
||
cb := &circuitBreaker{ | ||
cb: circuitbreak.NewCBSuite(genServiceCBKey), | ||
} | ||
m.RegisterCircuitBreaker(cb.updateAllCircuitConfigs) | ||
return client.WithCircuitBreaker(cb.cb) | ||
} | ||
|
||
// keep consistent when initialising the circuit breaker suit and updating | ||
// the circuit breaker policy. | ||
func genServiceCBKey(ri rpcinfo.RPCInfo) string { | ||
if ri == nil { | ||
return "" | ||
} | ||
key, _ := ri.To().Tag(RouterClusterKey) | ||
return key | ||
} |
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