|
| 1 | +/** |
| 2 | + * Tencent is pleased to support the open source community by making polaris-go available. |
| 3 | + * |
| 4 | + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. |
| 5 | + * |
| 6 | + * Licensed under the BSD 3-Clause License (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * https://opensource.org/licenses/BSD-3-Clause |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software distributed |
| 13 | + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 14 | + * CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 15 | + * specific language governing permissions and limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package polaris |
| 19 | + |
| 20 | +import ( |
| 21 | + "github.com/polarismesh/polaris-go/api" |
| 22 | + "github.com/polarismesh/polaris-go/pkg/config" |
| 23 | + "github.com/polarismesh/polaris-go/pkg/model" |
| 24 | +) |
| 25 | + |
| 26 | +// NewCircuitBreakerAPI 获取 CircuitBreakerAPI |
| 27 | +func NewCircuitBreakerAPI() (CircuitBreakerAPI, error) { |
| 28 | + rawAPI, err := api.NewCircuitBreakerAPI() |
| 29 | + if err != nil { |
| 30 | + return nil, err |
| 31 | + } |
| 32 | + return &circuitBreakerAPI{ |
| 33 | + rawAPI: rawAPI, |
| 34 | + }, nil |
| 35 | +} |
| 36 | + |
| 37 | +// NewCircuitBreakerAPIByConfig 通过配置对象获取 CircuitBreakerAPI |
| 38 | +func NewCircuitBreakerAPIByConfig(cfg config.Configuration) (CircuitBreakerAPI, error) { |
| 39 | + rawAPI, err := api.NewCircuitBreakerByConfig(cfg) |
| 40 | + if err != nil { |
| 41 | + return nil, err |
| 42 | + } |
| 43 | + return &circuitBreakerAPI{ |
| 44 | + rawAPI: rawAPI, |
| 45 | + }, nil |
| 46 | +} |
| 47 | + |
| 48 | +// NewCircuitBreakerAPIByFile 通过配置文件获取 CircuitBreakerAPI |
| 49 | +func NewCircuitBreakerAPIByFile(path string) (CircuitBreakerAPI, error) { |
| 50 | + rawAPI, err := api.NewCircuitBreakerByFile(path) |
| 51 | + if err != nil { |
| 52 | + return nil, err |
| 53 | + } |
| 54 | + return &circuitBreakerAPI{ |
| 55 | + rawAPI: rawAPI, |
| 56 | + }, nil |
| 57 | +} |
| 58 | + |
| 59 | +// NewCircuitBreakerAPIByContext 通过上下文对象获取 CircuitBreakerAPI |
| 60 | +func NewCircuitBreakerAPIByContext(context api.SDKContext) CircuitBreakerAPI { |
| 61 | + rawAPI := api.NewCircuitBreakerByContext(context) |
| 62 | + return &circuitBreakerAPI{ |
| 63 | + rawAPI: rawAPI, |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +type circuitBreakerAPI struct { |
| 68 | + rawAPI api.CircuitBreakerAPI |
| 69 | +} |
| 70 | + |
| 71 | +// Check |
| 72 | +func (c *circuitBreakerAPI) Check(res model.Resource) (*model.CheckResult, error) { |
| 73 | + return c.rawAPI.Check(res) |
| 74 | +} |
| 75 | + |
| 76 | +// Report |
| 77 | +func (c *circuitBreakerAPI) Report(stat *model.ResourceStat) error { |
| 78 | + return c.rawAPI.Report(stat) |
| 79 | +} |
| 80 | + |
| 81 | +// MakeFunctionDecorator |
| 82 | +func (c *circuitBreakerAPI) MakeFunctionDecorator(f model.CustomerFunction, reqCtx *api.RequestContext) model.DecoratorFunction { |
| 83 | + return c.rawAPI.MakeFunctionDecorator(f, reqCtx) |
| 84 | +} |
| 85 | + |
| 86 | +// MakeInvokeHandler |
| 87 | +func (c *circuitBreakerAPI) MakeInvokeHandler(reqCtx *api.RequestContext) model.InvokeHandler { |
| 88 | + return c.rawAPI.MakeInvokeHandler(reqCtx) |
| 89 | +} |
| 90 | + |
| 91 | +func (c *circuitBreakerAPI) SDKContext() api.SDKContext { |
| 92 | + return c.rawAPI.SDKContext() |
| 93 | +} |
| 94 | + |
| 95 | +// Destroy the api is destroyed and cannot be called again |
| 96 | +func (c *circuitBreakerAPI) Destroy() { |
| 97 | + c.rawAPI.SDKContext().Destroy() |
| 98 | +} |
0 commit comments