Skip to content

Commit

Permalink
feat:支持新版本的熔断规则设置 (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
chuntaojun authored Nov 19, 2023
1 parent dd56f3c commit ca98b72
Show file tree
Hide file tree
Showing 119 changed files with 6,450 additions and 5,291 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ jobs:
- uses: actions/setup-go@v3
- uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@v3.3.0
uses: golangci/golangci-lint-action@v3.6.0
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
version: latest
args: --timeout=30m

14 changes: 14 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,20 @@ type ConfigGroupAPI interface {
GetConfigGroup(namesapce, group string) (model.ConfigFileGroup, error)
}

type CircuitBreakerAPI interface {
api.SDKOwner
// Check
Check(model.Resource) (*model.CheckResult, error)
// Report
Report(*model.ResourceStat) error
// MakeFunctionDecorator
MakeFunctionDecorator(model.CustomerFunction, *api.RequestContext) model.DecoratorFunction
// MakeInvokeHandler
MakeInvokeHandler(*api.RequestContext) model.InvokeHandler
// Destroy the api is destroyed and cannot be called again
Destroy()
}

// RouterAPI routing api methods
type RouterAPI interface {
api.SDKOwner
Expand Down
87 changes: 87 additions & 0 deletions api/circuitbreaker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* Tencent is pleased to support the open source community by making polaris-go available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* 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 api

import (
"github.com/polarismesh/polaris-go/pkg/config"
"github.com/polarismesh/polaris-go/pkg/model"
)

// CircuitBreakerAPI .
type CircuitBreakerAPI interface {
SDKOwner
// Check
Check(model.Resource) (*model.CheckResult, error)
// Report
Report(*model.ResourceStat) error
// MakeFunctionDecorator
MakeFunctionDecorator(model.CustomerFunction, *RequestContext) model.DecoratorFunction
// MakeInvokeHandler
MakeInvokeHandler(*RequestContext) model.InvokeHandler
}

type ResultToErrorCode interface {
model.ResultToErrorCode
}

type RequestContext struct {
model.RequestContext
}

type ResponseContext struct {
model.ResponseContext
}

var (
// NewConsumerAPI 通过以默认域名为埋点server的默认配置创建 CircuitBreakerAPI
NewCircuitBreakerAPI = newCircuitBreakerAPI
// NewCircuitBreakerByFile 通过配置文件创建SDK CircuitBreakerAPI 对象
NewCircuitBreakerByFile = newCircuitBreakerAPIByFile
// NewCircuitBreakerByConfig 通过配置对象创建SDK CircuitBreakerAPI 对象
NewCircuitBreakerByConfig = newCircuitBreakerAPIByConfig
// NewCircuitBreakerByContext 通过上下文创建SDK CircuitBreakerAPI 对象
NewCircuitBreakerByContext = newCircuitBreakerAPIByContext
)

// 通过以默认域名为埋点server的默认配置创建 CircuitBreakerAPI
func newCircuitBreakerAPI() (CircuitBreakerAPI, error) {
return newCircuitBreakerAPIByConfig(config.NewDefaultConfigurationWithDomain())
}

// newCircuitBreakerAPIByFile 通过配置文件创建SDK CircuitBreakerAPI 对象
func newCircuitBreakerAPIByFile(path string) (CircuitBreakerAPI, error) {
context, err := InitContextByFile(path)
if err != nil {
return nil, err
}
return &circuitBreakerAPI{context}, nil
}

// newCircuitBreakerAPIByConfig 通过配置对象创建SDK CircuitBreakerAPI 对象
func newCircuitBreakerAPIByConfig(cfg config.Configuration) (CircuitBreakerAPI, error) {
context, err := InitContextByConfig(cfg)
if err != nil {
return nil, err
}
return &circuitBreakerAPI{context}, nil
}

// newCircuitBreakerAPIByContext 通过上下文创建SDK CircuitBreakerAPI 对象
func newCircuitBreakerAPIByContext(context SDKContext) CircuitBreakerAPI {
return &circuitBreakerAPI{context}
}
48 changes: 48 additions & 0 deletions api/circuitbreaker_impl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Tencent is pleased to support the open source community by making polaris-go available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* 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 api

import (
"github.com/polarismesh/polaris-go/pkg/model"
_ "github.com/polarismesh/polaris-go/pkg/plugin/register"
)

type circuitBreakerAPI struct {
context SDKContext
}

// SDKContext 获取SDK上下文
func (c *circuitBreakerAPI) SDKContext() SDKContext {
return c.context
}

func (c *circuitBreakerAPI) Check(resource model.Resource) (*model.CheckResult, error) {
return c.context.GetEngine().Check(resource)
}

func (c *circuitBreakerAPI) Report(reportStat *model.ResourceStat) error {
return c.context.GetEngine().Report(reportStat)
}

func (c *circuitBreakerAPI) MakeFunctionDecorator(f model.CustomerFunction, reqCtx *RequestContext) model.DecoratorFunction {
return c.context.GetEngine().MakeFunctionDecorator(f, &reqCtx.RequestContext)
}

func (c *circuitBreakerAPI) MakeInvokeHandler(reqCtx *RequestContext) model.InvokeHandler {
return c.context.GetEngine().MakeInvokeHandler(&reqCtx.RequestContext)
}
2 changes: 1 addition & 1 deletion api/provider_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (c *providerAPI) RegisterInstance(instance *InstanceRegisterRequest) (*mode
if err := instance.Validate(); err != nil {
return nil, err
}
return c.context.GetEngine().SyncRegisterV2(&instance.InstanceRegisterRequest)
return c.context.GetEngine().SyncRegister(&instance.InstanceRegisterRequest)
}

// Register 同步注册服务,服务注册成功后会填充instance中的InstanceId字段
Expand Down
98 changes: 98 additions & 0 deletions api_circuitbreaker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/**
* Tencent is pleased to support the open source community by making polaris-go available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* 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 polaris

import (
"github.com/polarismesh/polaris-go/api"
"github.com/polarismesh/polaris-go/pkg/config"
"github.com/polarismesh/polaris-go/pkg/model"
)

// NewCircuitBreakerAPI 获取 CircuitBreakerAPI
func NewCircuitBreakerAPI() (CircuitBreakerAPI, error) {
rawAPI, err := api.NewCircuitBreakerAPI()
if err != nil {
return nil, err
}
return &circuitBreakerAPI{
rawAPI: rawAPI,
}, nil
}

// NewCircuitBreakerAPIByConfig 通过配置对象获取 CircuitBreakerAPI
func NewCircuitBreakerAPIByConfig(cfg config.Configuration) (CircuitBreakerAPI, error) {
rawAPI, err := api.NewCircuitBreakerByConfig(cfg)
if err != nil {
return nil, err
}
return &circuitBreakerAPI{
rawAPI: rawAPI,
}, nil
}

// NewCircuitBreakerAPIByFile 通过配置文件获取 CircuitBreakerAPI
func NewCircuitBreakerAPIByFile(path string) (CircuitBreakerAPI, error) {
rawAPI, err := api.NewCircuitBreakerByFile(path)
if err != nil {
return nil, err
}
return &circuitBreakerAPI{
rawAPI: rawAPI,
}, nil
}

// NewCircuitBreakerAPIByContext 通过上下文对象获取 CircuitBreakerAPI
func NewCircuitBreakerAPIByContext(context api.SDKContext) CircuitBreakerAPI {
rawAPI := api.NewCircuitBreakerByContext(context)
return &circuitBreakerAPI{
rawAPI: rawAPI,
}
}

type circuitBreakerAPI struct {
rawAPI api.CircuitBreakerAPI
}

// Check
func (c *circuitBreakerAPI) Check(res model.Resource) (*model.CheckResult, error) {
return c.rawAPI.Check(res)
}

// Report
func (c *circuitBreakerAPI) Report(stat *model.ResourceStat) error {
return c.rawAPI.Report(stat)
}

// MakeFunctionDecorator
func (c *circuitBreakerAPI) MakeFunctionDecorator(f model.CustomerFunction, reqCtx *api.RequestContext) model.DecoratorFunction {
return c.rawAPI.MakeFunctionDecorator(f, reqCtx)
}

// MakeInvokeHandler
func (c *circuitBreakerAPI) MakeInvokeHandler(reqCtx *api.RequestContext) model.InvokeHandler {
return c.rawAPI.MakeInvokeHandler(reqCtx)
}

func (c *circuitBreakerAPI) SDKContext() api.SDKContext {
return c.rawAPI.SDKContext()
}

// Destroy the api is destroyed and cannot be called again
func (c *circuitBreakerAPI) Destroy() {
c.rawAPI.SDKContext().Destroy()
}
3 changes: 2 additions & 1 deletion api_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ func (p *providerAPI) SDKContext() api.SDKContext {
// RegisterInstance
// minimum supported version of polaris-server is v1.10.0
func (p *providerAPI) RegisterInstance(instance *InstanceRegisterRequest) (*model.InstanceRegisterResponse, error) {
return p.rawAPI.RegisterInstance((*api.InstanceRegisterRequest)(instance))
instance.AutoHeartbeat = true
return p.rawAPI.Register((*api.InstanceRegisterRequest)(instance))
}

// Register
Expand Down
Loading

0 comments on commit ca98b72

Please sign in to comment.