-
Notifications
You must be signed in to change notification settings - Fork 152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rate limit filter #169
Merged
Merged
rate limit filter #169
Changes from 42 commits
Commits
Show all changes
45 commits
Select commit
Hold shift + click to select a range
848409d
update dubbo-go to v1.5.6 & fmt sth.
mark4z 8133665
fix fmt
mark4z 980669a
Merge branch 'develop' into update_dubbo
mark4z 67d98da
Update go.mod
mark4z f3f2437
update dubbo-go to v1.5.6 & fmt sth.
mark4z 44543c4
update dubbo-go to v1.5.6 & fmt sth.
mark4z bca62b8
fix fmt
mark4z 0547c78
Merge branch 'develop' into update_dubbo
mark4z af4a56f
rateLimit filter
mark4z 9b99a24
add licence header
mark4z c865a5d
fix imports
mark4z 2e35f60
add license header
mark4z 564a2d3
add license header
mark4z be02472
Merge branch 'develop' into ratelimit
kezhenxu94 2563b4d
fix ci
mark4z 93363b7
Merge remote-tracking branch 'origin/ratelimit' into ratelimit
mark4z 90836a4
Merge branch 'develop' into ratelimit
mark4z da69b51
fix matcher var inline
mark4z 49015e4
Merge remote-tracking branch 'origin/ratelimit' into ratelimit
mark4z 2dd2b8e
Merge branch 'develop' into update_dubbo
mark4z 172ad82
fix ci
mark4z c73a7d0
fix sync.RWMutex
mark4z 153e99c
errors.warp
mark4z 02cb291
test table
mark4z 15a66ed
remove unreachable statement
mark4z b9a7c17
Pattern pattern,omitempty
mark4z b435753
Merge branch 'develop' of https://github.com/apache/dubbo-go-pixiu in…
mark4z 8a6dae2
update develop
mark4z 79d0c3b
update the docs for ratelimit
mark4z 602f8b2
update develop
mark4z c3a7da5
ratelimit filter
mark4z b93e6b1
ratelimit filter
mark4z 5ef9f3a
ratelimit filter
mark4z f0e7b70
Merge branch 'develop' into update_dubbo
mark4z ee45a7b
ratelimit filter
mark4z bb18bb0
ratelimit filter
mark4z fd1abef
ratelimit filter
mark4z 5f9e8ca
ratelimit filter
mark4z 10d44e8
ratelimit filter
mark4z 5d24779
Merge branch 'update_dubbo' into ratelimit
mark4z a4597ab
ratelimit filter
mark4z a482ff4
ratelimit filter
mark4z 5429895
ratelimit filter
mark4z 8af536e
ratelimit filter
mark4z 33e3246
Merge branch 'develop' of https://github.com/apache/dubbo-go-pixiu in…
mark4z File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,74 @@ | ||
/* | ||
* 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() | ||
|
||
loadApiResources(c.Resources) | ||
loadRules(c.Rules) | ||
return nil | ||
} | ||
|
||
// OnUpdate update api & rule | ||
func OnUpdate(c ratelimit.Config) { | ||
loadApiResources(c.Resources) | ||
loadRules(c.Rules) | ||
} | ||
|
||
// loadRules | ||
func loadRules(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) | ||
} | ||
} | ||
|
||
// loadApiResources | ||
func loadApiResources(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) | ||
} |
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 @@ | ||
/* | ||
* 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 "github.com/apache/dubbo-go-pixiu/pkg/logger" | ||
|
||
// loggerWrapper | ||
type loggerWrapper struct { | ||
} | ||
|
||
// getWrappedLogger | ||
func getWrappedLogger() loggerWrapper { | ||
return loggerWrapper{} | ||
} | ||
|
||
func (l loggerWrapper) Debug(msg string, keysAndValues ...interface{}) { | ||
logger.Debugf(msg, keysAndValues) | ||
} | ||
|
||
func (l loggerWrapper) Info(msg string, keysAndValues ...interface{}) { | ||
logger.Infof(msg, keysAndValues) | ||
} | ||
|
||
func (l loggerWrapper) Warn(msg string, keysAndValues ...interface{}) { | ||
logger.Warnf(msg, keysAndValues) | ||
} | ||
|
||
func (l loggerWrapper) Error(err error, msg string, keysAndValues ...interface{}) { | ||
logger.Warnf(msg, err, keysAndValues) | ||
} | ||
|
||
// InfoEnabled todo logger should implements this method | ||
func (l loggerWrapper) InfoEnabled() bool { | ||
return true | ||
} | ||
|
||
func (l loggerWrapper) ErrorEnabled() bool { | ||
return true | ||
} | ||
|
||
func (l loggerWrapper) DebugEnabled() bool { | ||
return true | ||
} | ||
|
||
func (l loggerWrapper) WarnEnabled() bool { | ||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* 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 matcher | ||
|
||
import ( | ||
"github.com/dubbogo/dubbo-go-pixiu-filter/pkg/api/config/ratelimit" | ||
) | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
type Exact struct { | ||
apiNames map[string]string | ||
|
||
mu sync.RWMutex | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI,I think pointer is better There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mu *sync.RWMutex, this way? |
||
} | ||
|
||
func (p *Exact) load(apis []ratelimit.Resource) { | ||
m := map[string]string{} | ||
|
||
for _, api := range apis { | ||
apiName := api.Name | ||
for _, item := range api.Items { | ||
if item.MatchStrategy == ratelimit.EXACT { | ||
m[item.Pattern] = apiName | ||
} | ||
} | ||
} | ||
|
||
p.mu.Lock() | ||
defer p.mu.Unlock() | ||
p.apiNames = m | ||
} | ||
|
||
func (p *Exact) match(path string) (string, bool) { | ||
p.mu.RLock() | ||
defer p.mu.RUnlock() | ||
|
||
resourceName, ok := p.apiNames[path] | ||
return resourceName, ok | ||
} |
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,66 @@ | ||
/* | ||
* 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 matcher | ||
|
||
import ( | ||
"github.com/dubbogo/dubbo-go-pixiu-filter/pkg/api/config/ratelimit" | ||
) | ||
|
||
var _matcher *Matcher | ||
|
||
// Init matcher | ||
func Init() { | ||
_matcher = NewMatcher() | ||
} | ||
|
||
//PathMatcher according the url path find APIResource name | ||
type PathMatcher interface { | ||
load(apis []ratelimit.Resource) | ||
|
||
match(path string) (string, bool) | ||
} | ||
|
||
type Matcher struct { | ||
matchers []PathMatcher | ||
} | ||
|
||
func NewMatcher() *Matcher { | ||
return &Matcher{ | ||
matchers: []PathMatcher{ | ||
&Exact{}, | ||
&Regex{}, | ||
}, | ||
} | ||
} | ||
|
||
// Load load api resource for matchers | ||
func Load(apis []ratelimit.Resource) { | ||
for _, v := range _matcher.matchers { | ||
v.load(apis) | ||
} | ||
} | ||
|
||
// Match match resource via url path | ||
func Match(path string) (string, bool) { | ||
for _, m := range _matcher.matchers { | ||
if res, ok := m.match(path); ok { | ||
return res, ok | ||
} | ||
} | ||
return "", false | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what does this code make sense?