-
Notifications
You must be signed in to change notification settings - Fork 55
/
value.go
163 lines (142 loc) · 5.27 KB
/
value.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
* 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 local
import (
"sync"
"sync/atomic"
"github.com/polarismesh/polaris-go/pkg/metric"
"github.com/polarismesh/polaris-go/pkg/model"
"github.com/polarismesh/polaris-go/pkg/plugin/common"
)
// InstanceLocalValue 本地实例数据,包括熔断,动态权重等信息
type InstanceLocalValue interface {
// GetSliceWindows 获取统计滑窗
GetSliceWindows(int32) []*metric.SliceWindow
// GetCircuitBreakerStatus 实例的熔断状态
GetCircuitBreakerStatus() model.CircuitBreakerStatus
// GetActiveDetectStatus 实例的健康检查状态
GetActiveDetectStatus() model.ActiveDetectStatus
GetExtendedData(pluginIndex int32) interface{}
SetExtendedData(pluginIndex int32, data interface{})
}
// NewInstanceLocalValue 创建默认的实例本地信息
func NewInstanceLocalValue() InstanceLocalValue {
return &DefaultInstanceLocalValue{
sliceWindows: make(map[int32][]*metric.SliceWindow, 0),
extendedData: &sync.Map{},
}
}
// DefaultInstanceLocalValue 用于存储实例的本地信息
type DefaultInstanceLocalValue struct {
// 各插件统计窗口, 每个插件给于通过插件名进行唯一标识
sliceWindows map[int32][]*metric.SliceWindow
// 各个插件的特定数据
extendedData *sync.Map
cbStatus atomic.Value
odStatus atomic.Value
}
// GetSliceWindows 获取滑窗
func (lv *DefaultInstanceLocalValue) GetSliceWindows(pluginIndex int32) []*metric.SliceWindow {
return lv.sliceWindows[pluginIndex]
}
// GetExtendedData 获取插件数据
func (lv *DefaultInstanceLocalValue) GetExtendedData(pluginIndex int32) interface{} {
res, load := lv.extendedData.Load(pluginIndex)
if load {
return res
}
return nil
}
// SetExtendedData 设置插件数据
func (lv *DefaultInstanceLocalValue) SetExtendedData(pluginIndex int32, data interface{}) {
lv.extendedData.Store(pluginIndex, data)
}
// SetSliceWindows 设置滑窗
func (lv *DefaultInstanceLocalValue) SetSliceWindows(pluginIndex int32, windows []*metric.SliceWindow) {
lv.sliceWindows[pluginIndex] = windows
}
// SetCircuitBreakerStatus 设置熔断信息
func (lv *DefaultInstanceLocalValue) SetCircuitBreakerStatus(st model.CircuitBreakerStatus) {
lv.cbStatus.Store(st)
}
// SetActiveDetectStatus 设置健康检测信息
func (lv *DefaultInstanceLocalValue) SetActiveDetectStatus(st model.ActiveDetectStatus) {
lv.odStatus.Store(st)
}
// GetCircuitBreakerStatus 返回熔断信息
func (lv *DefaultInstanceLocalValue) GetCircuitBreakerStatus() model.CircuitBreakerStatus {
res := lv.cbStatus.Load()
if nil == res {
return nil
}
return res.(model.CircuitBreakerStatus)
}
// GetActiveDetectStatus 返回健康检测信息
func (lv *DefaultInstanceLocalValue) GetActiveDetectStatus() model.ActiveDetectStatus {
res := lv.odStatus.Load()
if nil == res {
return nil
}
return res.(model.ActiveDetectStatus)
}
// ServiceLocalValue 服务localvalue接口
type ServiceLocalValue interface {
// 通过插件ID获取服务级缓存数据
GetServiceDataByPluginId(pluginIdx int32) interface{}
// 设置通过插件ID索引的服务级缓存数据
SetServiceDataByPluginId(pluginIdx int32, data interface{})
// 通过插件类型来获取服务级缓存数据
GetServiceDataByPluginType(pluginType common.Type) interface{}
// 设置通过插件类型来索引的服务级缓存数据
SetServiceDataByPluginType(pluginType common.Type, data interface{})
}
// DefaultServiceLocalValue 服务localvalue实现
type DefaultServiceLocalValue struct {
svcDataByPluginId *sync.Map
svcDataByPluginType *sync.Map
}
// NewServiceLocalValue 创建服务localvalue
func NewServiceLocalValue() *DefaultServiceLocalValue {
return &DefaultServiceLocalValue{
svcDataByPluginId: &sync.Map{},
svcDataByPluginType: &sync.Map{},
}
}
// GetServiceDataByPluginId 获取服务localvalue数据
func (sv *DefaultServiceLocalValue) GetServiceDataByPluginId(pluginIdx int32) interface{} {
res, ok := sv.svcDataByPluginId.Load(pluginIdx)
if ok {
return res
}
return nil
}
// SetServiceDataByPluginId 设置服务localvalue数据
func (sv *DefaultServiceLocalValue) SetServiceDataByPluginId(pluginIdx int32, data interface{}) {
sv.svcDataByPluginId.Store(pluginIdx, data)
}
// GetServiceDataByPluginType 获取服务localvalue数据
func (sv *DefaultServiceLocalValue) GetServiceDataByPluginType(pluginType common.Type) interface{} {
res, ok := sv.svcDataByPluginType.Load(pluginType)
if ok {
return res
}
return nil
}
// SetServiceDataByPluginType 设置服务localvalue数据
func (sv *DefaultServiceLocalValue) SetServiceDataByPluginType(pluginType common.Type, data interface{}) {
sv.svcDataByPluginType.Store(pluginType, data)
}