-
Notifications
You must be signed in to change notification settings - Fork 929
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* feat:support polaris tps limiter * feat:support polaris tps limiter * feat:support polaris limit ability * feat:support polaris limit ability * feat:support polaris limit ability * feat:support polaris limit ability
- Loading branch information
1 parent
d70b6c0
commit 40ce3ba
Showing
11 changed files
with
539 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 constant | ||
|
||
const ( | ||
PolarisKey = "polaris" | ||
PolarisDefaultRoleType = 3 | ||
PolarisServiceToken = "token" | ||
PolarisServiceNameSeparator = ":" | ||
PolarisDubboPath = "DUBBOPATH" | ||
PolarisInstanceID = "polaris.instanceID" | ||
PolarisDefaultNamespace = "default" | ||
PolarisDubboGroup = "dubbo.group" | ||
PolarisClientName = "polaris-client" | ||
) | ||
|
||
const ( | ||
PluginPolarisTpsLimiter = "polaris-limit" | ||
) |
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,30 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 limit | ||
|
||
import ( | ||
"dubbo.apache.org/dubbo-go/v3/common/constant" | ||
"dubbo.apache.org/dubbo-go/v3/common/extension" | ||
"dubbo.apache.org/dubbo-go/v3/filter" | ||
) | ||
|
||
func init() { | ||
extension.SetTpsLimiter(constant.PluginPolarisTpsLimiter, func() filter.TpsLimiter { | ||
return &polarisTpsLimiter{} | ||
}) | ||
} |
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,167 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 limit | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
import ( | ||
"github.com/dubbogo/gost/log/logger" | ||
|
||
"github.com/polarismesh/polaris-go" | ||
"github.com/polarismesh/polaris-go/pkg/flow/data" | ||
"github.com/polarismesh/polaris-go/pkg/model" | ||
v1 "github.com/polarismesh/polaris-go/pkg/model/pb/v1" | ||
) | ||
|
||
import ( | ||
"dubbo.apache.org/dubbo-go/v3/common" | ||
"dubbo.apache.org/dubbo-go/v3/common/constant" | ||
"dubbo.apache.org/dubbo-go/v3/config" | ||
"dubbo.apache.org/dubbo-go/v3/protocol" | ||
remotingpolaris "dubbo.apache.org/dubbo-go/v3/remoting/polaris" | ||
"dubbo.apache.org/dubbo-go/v3/remoting/polaris/parser" | ||
) | ||
|
||
type polarisTpsLimiter struct { | ||
limitApi polaris.LimitAPI | ||
} | ||
|
||
func (pl *polarisTpsLimiter) IsAllowable(url *common.URL, invocation protocol.Invocation) bool { | ||
var err error | ||
|
||
pl.limitApi, err = remotingpolaris.GetLimiterAPI() | ||
if err != nil { | ||
logger.Error("[TpsLimiter][Polaris] create polaris LimitAPI fail : %+v", err) | ||
return true | ||
} | ||
|
||
req := pl.buildQuotaRequest(url, invocation) | ||
if req == nil { | ||
return true | ||
} | ||
logger.Debugf("[TpsLimiter][Polaris] quota req : %+v", req) | ||
|
||
resp, err := pl.limitApi.GetQuota(req) | ||
if err != nil { | ||
logger.Error("[TpsLimiter][Polaris] ns:%s svc:%s get quota fail : %+v", remotingpolaris.GetNamespace(), url.Service(), err) | ||
return true | ||
} | ||
|
||
return resp.Get().Code == model.QuotaResultOk | ||
} | ||
|
||
func (pl *polarisTpsLimiter) buildQuotaRequest(url *common.URL, invoaction protocol.Invocation) polaris.QuotaRequest { | ||
ns := remotingpolaris.GetNamespace() | ||
applicationMode := false | ||
for _, item := range config.GetRootConfig().Registries { | ||
if item.Protocol == constant.PolarisKey { | ||
applicationMode = item.RegistryType == constant.ServiceKey | ||
} | ||
} | ||
|
||
svc := "providers:" + url.Service() | ||
method := invoaction.MethodName() | ||
if applicationMode { | ||
svc = config.GetApplicationConfig().Name | ||
method = url.Interface() + "/" + invoaction.MethodName() | ||
} | ||
|
||
req := polaris.NewQuotaRequest() | ||
req.SetNamespace(ns) | ||
req.SetService(svc) | ||
req.SetMethod(method) | ||
|
||
matchs, ok := pl.buildArguments(req.(*model.QuotaRequestImpl)) | ||
if !ok { | ||
return nil | ||
} | ||
|
||
attachement := invoaction.Attachments() | ||
arguments := invoaction.Arguments() | ||
|
||
for i := range matchs { | ||
item := matchs[i] | ||
switch item.GetType() { | ||
case v1.MatchArgument_HEADER: | ||
if val, ok := attachement[item.GetKey()]; ok { | ||
req.AddArgument(model.BuildHeaderArgument(item.GetKey(), fmt.Sprintf("%+v", val))) | ||
} | ||
case v1.MatchArgument_QUERY: | ||
if val := parser.ParseArgumentsByExpression(item.GetKey(), arguments); val != nil { | ||
req.AddArgument(model.BuildQueryArgument(item.GetKey(), fmt.Sprintf("%+v", val))) | ||
} | ||
case v1.MatchArgument_CALLER_IP: | ||
callerIp := url.GetParam(constant.RemoteAddr, "") | ||
if len(callerIp) != 0 { | ||
req.AddArgument(model.BuildCallerIPArgument(callerIp)) | ||
} | ||
case model.ArgumentTypeCallerService: | ||
} | ||
} | ||
|
||
return req | ||
} | ||
|
||
func (pl *polarisTpsLimiter) buildArguments(req *model.QuotaRequestImpl) ([]*v1.MatchArgument, bool) { | ||
engine := pl.limitApi.SDKContext().GetEngine() | ||
|
||
getRuleReq := &data.CommonRateLimitRequest{ | ||
DstService: model.ServiceKey{ | ||
Namespace: req.GetNamespace(), | ||
Service: req.GetService(), | ||
}, | ||
Trigger: model.NotifyTrigger{ | ||
EnableDstRateLimit: true, | ||
}, | ||
ControlParam: model.ControlParam{ | ||
Timeout: time.Millisecond * 500, | ||
}, | ||
} | ||
|
||
if err := engine.SyncGetResources(getRuleReq); err != nil { | ||
logger.Error("[TpsLimiter][Polaris] ns:%s svc:%s get RateLimit Rule fail : %+v", req.GetNamespace(), req.GetService(), err) | ||
return nil, false | ||
} | ||
|
||
svcRule := getRuleReq.RateLimitRule | ||
if svcRule == nil || svcRule.GetValue() == nil { | ||
logger.Warnf("[TpsLimiter][Polaris] ns:%s svc:%s get RateLimit Rule is nil", req.GetNamespace(), req.GetService()) | ||
return nil, false | ||
} | ||
|
||
rules, ok := svcRule.GetValue().(*v1.RateLimit) | ||
if !ok { | ||
logger.Error("[TpsLimiter][Polaris] ns:%s svc:%s get RateLimit Rule invalid", req.GetNamespace(), req.GetService()) | ||
return nil, false | ||
} | ||
|
||
ret := make([]*v1.MatchArgument, 0, 4) | ||
for i := range rules.GetRules() { | ||
rule := rules.GetRules()[i] | ||
if len(rule.GetArguments()) == 0 { | ||
continue | ||
} | ||
|
||
ret = append(ret, rule.Arguments...) | ||
} | ||
|
||
return ret, true | ||
} |
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
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
Oops, something went wrong.