-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
3,820 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright The HTNN 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 sentinel | ||
|
||
import ( | ||
"mosn.io/htnn/api/pkg/filtermanager/api" | ||
"mosn.io/htnn/api/pkg/plugins" | ||
"mosn.io/htnn/plugins/plugins/sentinel/rules" | ||
"mosn.io/htnn/types/plugins/sentinel" | ||
|
||
sentinelApi "github.com/alibaba/sentinel-golang/api" | ||
sentinelConf "github.com/alibaba/sentinel-golang/core/config" | ||
) | ||
|
||
func init() { | ||
plugins.RegisterPlugin(sentinel.Name, &plugin{}) | ||
} | ||
|
||
type plugin struct { | ||
sentinel.Plugin | ||
} | ||
|
||
func (p *plugin) Factory() api.FilterFactory { | ||
return factory | ||
} | ||
|
||
func (p *plugin) Config() api.PluginConfig { | ||
return &config{} | ||
} | ||
|
||
type config struct { | ||
sentinel.Config | ||
} | ||
|
||
func (conf *config) Init(cb api.ConfigCallbackHandler) error { | ||
sconf := sentinelConf.NewDefaultConfig() | ||
err := sentinelApi.InitWithConfig(sconf) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = rules.Load(conf.Type, conf.Rule) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
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,24 @@ | ||
// Copyright The HTNN 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 sentinel | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
// TODO(WeixinX) | ||
func TestConfig(t *testing.T) { | ||
|
||
} |
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,71 @@ | ||
// Copyright The HTNN 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 sentinel | ||
|
||
import ( | ||
"mosn.io/htnn/api/pkg/filtermanager/api" | ||
types "mosn.io/htnn/types/plugins/sentinel" | ||
|
||
sentinel "github.com/alibaba/sentinel-golang/api" | ||
"github.com/alibaba/sentinel-golang/core/base" | ||
) | ||
|
||
func factory(c interface{}, callbacks api.FilterCallbackHandler) api.Filter { | ||
return &filter{ | ||
callbacks: callbacks, | ||
config: c.(*config), | ||
} | ||
} | ||
|
||
type filter struct { | ||
api.PassThroughFilter | ||
|
||
callbacks api.FilterCallbackHandler | ||
config *config | ||
} | ||
|
||
// Sentinel traffic control | ||
func (f *filter) verify(resName string) bool { | ||
e, b := sentinel.Entry(resName, sentinel.WithTrafficType(base.Inbound), sentinel.WithArgs()) | ||
if b != nil { | ||
// blocked | ||
return false | ||
} | ||
|
||
// passed | ||
e.Exit() | ||
return true | ||
} | ||
|
||
// TODO(WeixinX): 完善限流逻辑 | ||
func (f *filter) DecodeHeaders(headers api.RequestHeaderMap, endStream bool) api.ResultAction { | ||
config := f.config | ||
var vals []string | ||
var resName string | ||
if config.Key.Source == types.Key_HEADER { | ||
vals = headers.Values(config.Key.Name) | ||
} else { | ||
vals = headers.URL().Query()[config.Key.Name] | ||
} | ||
|
||
if len(vals) >= 1 { | ||
resName = vals[0] | ||
} | ||
|
||
if ok := f.verify(resName); !ok { | ||
return &api.LocalResponse{Code: 429} | ||
} | ||
return api.Continue | ||
} |
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,24 @@ | ||
// Copyright The HTNN 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 sentinel | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
// TODO(WeixinX) | ||
func TestFilter(t *testing.T) { | ||
|
||
} |
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,46 @@ | ||
// Copyright The HTNN 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 rules | ||
|
||
import ( | ||
types "mosn.io/htnn/types/plugins/sentinel" | ||
|
||
"github.com/alibaba/sentinel-golang/core/circuitbreaker" | ||
) | ||
|
||
func LoadCircuitBreakerRule(rule *types.CircuitBreakerRule) (bool, error) { | ||
oldRules := circuitbreaker.GetRules() | ||
newRules := make([]*circuitbreaker.Rule, 0, len(oldRules)+1) | ||
i := 0 | ||
for _, r := range oldRules { | ||
tmp := r | ||
newRules[i] = &tmp | ||
i++ | ||
} | ||
|
||
newRules[i] = &circuitbreaker.Rule{ | ||
Id: rule.Id, | ||
Resource: rule.Resource, | ||
Strategy: circuitbreaker.Strategy(rule.Strategy), | ||
RetryTimeoutMs: rule.RetryTimeoutMs, | ||
MinRequestAmount: rule.MinRequestAmount, | ||
StatIntervalMs: rule.StatIntervalMs, | ||
StatSlidingWindowBucketCount: rule.StatSlidingWindowBucketCount, | ||
MaxAllowedRtMs: rule.MaxAllowedRtMs, | ||
Threshold: rule.Threshold, | ||
ProbeNum: rule.ProbeNum, | ||
} | ||
return circuitbreaker.LoadRules(newRules) | ||
} |
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,51 @@ | ||
// Copyright The HTNN 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 rules | ||
|
||
import ( | ||
types "mosn.io/htnn/types/plugins/sentinel" | ||
|
||
"github.com/alibaba/sentinel-golang/core/flow" | ||
) | ||
|
||
func LoadFlowRule(rule *types.FlowRule) (bool, error) { | ||
oldRules := flow.GetRules() | ||
newRules := make([]*flow.Rule, 0, len(oldRules)+1) | ||
i := 0 | ||
for _, r := range oldRules { | ||
tmp := r | ||
newRules[i] = &tmp | ||
i++ | ||
} | ||
|
||
newRules[i] = &flow.Rule{ | ||
ID: rule.Id, | ||
Resource: rule.Resource, | ||
TokenCalculateStrategy: flow.TokenCalculateStrategy(rule.TokenCalculateStrategy), | ||
ControlBehavior: flow.ControlBehavior(rule.ControlBehavior), | ||
Threshold: rule.Threshold, | ||
RelationStrategy: flow.RelationStrategy(rule.RelationStrategy), | ||
RefResource: rule.RefResource, | ||
MaxQueueingTimeMs: rule.MaxQueueingTimeMs, | ||
WarmUpPeriodSec: rule.WarmUpPeriodSec, | ||
WarmUpColdFactor: rule.WarmUpColdFactor, | ||
StatIntervalInMs: rule.StatIntervalInMs, | ||
LowMemUsageThreshold: rule.LowMemUsageThreshold, | ||
HighMemUsageThreshold: rule.HighMemUsageThreshold, | ||
MemLowWaterMarkBytes: rule.MemLowWaterMarkBytes, | ||
MemHighWaterMarkBytes: rule.MemHighWaterMarkBytes, | ||
} | ||
return flow.LoadRules(newRules) | ||
} |
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,52 @@ | ||
// Copyright The HTNN 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 rules | ||
|
||
import ( | ||
types "mosn.io/htnn/types/plugins/sentinel" | ||
|
||
"github.com/alibaba/sentinel-golang/core/hotspot" | ||
) | ||
|
||
func LoadHotSpotRule(rule *types.HotSpotRule) (bool, error) { | ||
oldRules := hotspot.GetRules() | ||
newRules := make([]*hotspot.Rule, 0, len(oldRules)+1) | ||
i := 0 | ||
for _, r := range oldRules { | ||
tmp := r | ||
newRules[i] = &tmp | ||
i++ | ||
} | ||
|
||
var specificItems map[interface{}]int64 | ||
for k, v := range rule.SpecificItems { | ||
specificItems[k] = v | ||
} | ||
newRules[i] = &hotspot.Rule{ | ||
ID: rule.Id, | ||
Resource: rule.Resource, | ||
MetricType: hotspot.MetricType(rule.MetricType), | ||
ControlBehavior: hotspot.ControlBehavior(rule.ControlBehavior), | ||
ParamIndex: int(rule.ParamIndex), | ||
ParamKey: rule.ParamKey, | ||
Threshold: rule.Threshold, | ||
MaxQueueingTimeMs: rule.MaxQueueingTimeMs, | ||
BurstCount: rule.BurstCount, | ||
DurationInSec: rule.DurationInSec, | ||
ParamsMaxCapacity: rule.ParamsMaxCapacity, | ||
SpecificItems: specificItems, | ||
} | ||
return hotspot.LoadRules(newRules) | ||
} |
Oops, something went wrong.