-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* update dubbo-go to v1.5.6 & fmt sth. * fix fmt * Update go.mod * update dubbo-go to v1.5.6 & fmt sth. * update dubbo-go to v1.5.6 & fmt sth. * fix fmt * rateLimit filter * add licence header * fix imports * add license header * add license header * fix ci * fix matcher var inline * fix ci * fix sync.RWMutex * errors.warp * test table * remove unreachable statement * Pattern pattern,omitempty * update develop * update the docs for ratelimit * update develop * ratelimit filter * ratelimit filter * ratelimit filter * ratelimit filter * ratelimit filter * ratelimit filter * ratelimit filter * ratelimit filter * ratelimit filter * ratelimit filter * ratelimit filter * ratelimit filter Co-authored-by: Zhenxu Ke <[email protected]>
- Loading branch information
1 parent
4bae729
commit fbaf7ed
Showing
40 changed files
with
966 additions
and
337 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* 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 ratelimit | ||
|
||
import ( | ||
sentinel "github.com/alibaba/sentinel-golang/api" | ||
sc "github.com/alibaba/sentinel-golang/core/config" | ||
"github.com/alibaba/sentinel-golang/core/flow" | ||
"github.com/alibaba/sentinel-golang/logging" | ||
"github.com/dubbogo/dubbo-go-pixiu-filter/pkg/api/config/ratelimit" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
import ( | ||
"github.com/apache/dubbo-go-pixiu/pkg/filter/ratelimit/matcher" | ||
"github.com/apache/dubbo-go-pixiu/pkg/logger" | ||
) | ||
|
||
func rateLimitInit(c *ratelimit.Config) error { | ||
sentinelConf := sc.NewDefaultConfig() | ||
if len(c.LogPath) > 0 { | ||
sentinelConf.Sentinel.Log.Dir = c.LogPath | ||
} | ||
_ = logging.ResetGlobalLogger(getWrappedLogger()) | ||
|
||
if err := sentinel.InitWithConfig(sentinelConf); err != nil { | ||
return errors.Wrap(err, "rate limit init fail") | ||
} | ||
matcher.Init() | ||
|
||
OnUpdate(c) | ||
return nil | ||
} | ||
|
||
// OnUpdate update api & rule | ||
func OnUpdate(c *ratelimit.Config) { | ||
OnResourcesUpdate(c.Resources) | ||
OnRulesUpdate(c.Rules) | ||
} | ||
|
||
// OnRulesUpdate update rule | ||
func OnRulesUpdate(rules []ratelimit.Rule) { | ||
var enableRules []*flow.Rule | ||
for _, v := range rules { | ||
if v.Enable { | ||
enableRules = append(enableRules, &v.FlowRule) | ||
} | ||
} | ||
|
||
if _, err := flow.LoadRules(enableRules); err != nil { | ||
logger.Warnf("rate limit load rules err: %v", err) | ||
} | ||
} | ||
|
||
// OnResourcesUpdate update matcher for resources | ||
func OnResourcesUpdate(apis []ratelimit.Resource) { | ||
matcher.Load(apis) | ||
} |
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,33 @@ | ||
/* | ||
* 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 ratelimit | ||
|
||
import "testing" | ||
|
||
func TestInit(t *testing.T) { | ||
c := GetMockedRateLimitConfig() | ||
err := rateLimitInit(c) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func TestOnUpdate(t *testing.T) { | ||
config := GetMockedRateLimitConfig() | ||
OnUpdate(config) | ||
} |
Oops, something went wrong.