-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathconfig.go
More file actions
145 lines (116 loc) · 3.98 KB
/
config.go
File metadata and controls
145 lines (116 loc) · 3.98 KB
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
/*
* Copyright (c) New Cloud Technologies, Ltd. 2013-2026.
* Author: Vitaly Isaev <vitaly.isaev@myoffice.team>
* License: https://github.com/newcloudtechnologies/memlimiter/blob/master/LICENSE
*/
package nextgc
import (
"errors"
"github.com/newcloudtechnologies/memlimiter/backpressure"
"github.com/newcloudtechnologies/memlimiter/utils/config/bytes"
"github.com/newcloudtechnologies/memlimiter/utils/config/duration"
)
// defaultMinGOGC is the default minimum GOGC value used in the "red zone".
const defaultMinGOGC = 10
// ControllerConfig - controller configuration.
type ControllerConfig struct {
// RSSLimit - physical memory (RSS) consumption hard limit for a process.
RSSLimit bytes.Bytes `json:"rss_limit"`
// DangerZoneGOGC - RSS utilization threshold that triggers controller to
// set more conservative parameters for GC.
// Possible values are in range (0; 100].
DangerZoneGOGC uint32 `json:"danger_zone_gogc"`
// DangerZoneThrottling - RSS utilization threshold that triggers controller to
// throttle incoming requests.
// Possible values are in range (0; 100].
// It's recommended to keep it greater than or equal to DangerZoneGOGC so that
// the service first intensifies GC and starts throttling only later.
DangerZoneThrottling uint32 `json:"danger_zone_throttling"`
// Period - the periodicity of control parameters computation.
Period duration.Duration `json:"period"`
// MinGOGC - minimal allowed GOGC value used in the "red zone".
// Zero means default safe value.
MinGOGC int `json:"min_gogc"`
// ComponentProportional - controller's proportional component configuration
ComponentProportional *ComponentProportionalConfig `json:"component_proportional"`
// TODO:
// if some other components will appear in future, put their configs here.
}
// Prepare - config validator.
func (c *ControllerConfig) Prepare() error {
if err := c.validateRSSLimit(); err != nil {
return err
}
if err := c.validateDangerZoneGOGC(); err != nil {
return err
}
if err := c.validateDangerZoneThrottling(); err != nil {
return err
}
if err := c.validatePeriod(); err != nil {
return err
}
c.applyDefaults()
if err := c.validateMinGOGC(); err != nil {
return err
}
if err := c.validateComponentProportional(); err != nil {
return err
}
return nil
}
func (c *ControllerConfig) validateRSSLimit() error {
if c.RSSLimit.Value == 0 {
return errors.New("empty RSSLimit")
}
return nil
}
func (c *ControllerConfig) validateDangerZoneGOGC() error {
if c.DangerZoneGOGC == 0 || c.DangerZoneGOGC > 100 {
return errors.New("invalid DangerZoneGOGC value (must belong to (0; 100])")
}
return nil
}
func (c *ControllerConfig) validateDangerZoneThrottling() error {
if c.DangerZoneThrottling == 0 || c.DangerZoneThrottling > 100 {
return errors.New("invalid DangerZoneThrottling value (must belong to (0; 100])")
}
return nil
}
func (c *ControllerConfig) validatePeriod() error {
if c.Period.Duration == 0 {
return errors.New("empty Period")
}
return nil
}
func (c *ControllerConfig) applyDefaults() {
if c.MinGOGC == 0 {
c.MinGOGC = defaultMinGOGC
}
}
func (c *ControllerConfig) validateMinGOGC() error {
if c.MinGOGC < 1 || c.MinGOGC > backpressure.DefaultGOGC {
return errors.New("invalid MinGOGC value")
}
return nil
}
func (c *ControllerConfig) validateComponentProportional() error {
if c.ComponentProportional == nil {
return errors.New("empty ComponentProportional")
}
return nil
}
// ComponentProportionalConfig - controller's proportional component configuration.
type ComponentProportionalConfig struct {
// Coefficient - coefficient used to computed weighted sum of in the controller equation
Coefficient float64 `json:"coefficient"`
// WindowSize - averaging window size for the EMA. Averaging is disabled if WindowSize is zero.
WindowSize uint `json:"window_size"`
}
// Prepare - config validator.
func (c *ComponentProportionalConfig) Prepare() error {
if c.Coefficient == 0 {
return errors.New("empty Coefficient makes no sense")
}
return nil
}