This repository has been archived by the owner on May 12, 2021. It is now read-only.
/
token_bucket.go
112 lines (85 loc) · 2.95 KB
/
token_bucket.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
// Code generated by go-swagger; DO NOT EDIT.
package models
// This file was generated by the swagger tool.
// Editing this file might prove futile when you re-run the swagger generate command
import (
strfmt "github.com/go-openapi/strfmt"
"github.com/go-openapi/errors"
"github.com/go-openapi/swag"
"github.com/go-openapi/validate"
)
// TokenBucket Defines a token bucket with a maximum capacity (size), an initial burst size (one_time_burst) and an interval for refilling purposes (refill_time). The refill-rate is derived from size and refill_time, and it is the constant rate at which the tokens replenish. The refill process only starts happening after the initial burst budget is consumed. Consumption from the token bucket is unbounded in speed which allows for bursts bound in size by the amount of tokens available. Once the token bucket is empty, consumption speed is bound by the refill_rate.
// swagger:model TokenBucket
type TokenBucket struct {
// The initial size of a token bucket.
// Minimum: 0
OneTimeBurst *int64 `json:"one_time_burst,omitempty"`
// The amount of milliseconds it takes for the bucket to refill.
// Required: true
// Minimum: 0
RefillTime *int64 `json:"refill_time"`
// The total number of tokens this bucket can hold.
// Required: true
// Minimum: 0
Size *int64 `json:"size"`
}
// Validate validates this token bucket
func (m *TokenBucket) Validate(formats strfmt.Registry) error {
var res []error
if err := m.validateOneTimeBurst(formats); err != nil {
res = append(res, err)
}
if err := m.validateRefillTime(formats); err != nil {
res = append(res, err)
}
if err := m.validateSize(formats); err != nil {
res = append(res, err)
}
if len(res) > 0 {
return errors.CompositeValidationError(res...)
}
return nil
}
func (m *TokenBucket) validateOneTimeBurst(formats strfmt.Registry) error {
if swag.IsZero(m.OneTimeBurst) { // not required
return nil
}
if err := validate.MinimumInt("one_time_burst", "body", int64(*m.OneTimeBurst), 0, false); err != nil {
return err
}
return nil
}
func (m *TokenBucket) validateRefillTime(formats strfmt.Registry) error {
if err := validate.Required("refill_time", "body", m.RefillTime); err != nil {
return err
}
if err := validate.MinimumInt("refill_time", "body", int64(*m.RefillTime), 0, false); err != nil {
return err
}
return nil
}
func (m *TokenBucket) validateSize(formats strfmt.Registry) error {
if err := validate.Required("size", "body", m.Size); err != nil {
return err
}
if err := validate.MinimumInt("size", "body", int64(*m.Size), 0, false); err != nil {
return err
}
return nil
}
// MarshalBinary interface implementation
func (m *TokenBucket) MarshalBinary() ([]byte, error) {
if m == nil {
return nil, nil
}
return swag.WriteJSON(m)
}
// UnmarshalBinary interface implementation
func (m *TokenBucket) UnmarshalBinary(b []byte) error {
var res TokenBucket
if err := swag.ReadJSON(b, &res); err != nil {
return err
}
*m = res
return nil
}