forked from kedacore/http-add-on
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhandlers.go
220 lines (195 loc) · 5.11 KB
/
handlers.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Handlers contains the gRPC implementation for an external scaler as defined
// by the KEDA documentation at https://keda.sh/docs/2.0/concepts/external-scalers/#built-in-scalers-interface
// This is the interface KEDA will poll in order to get the request queue size
// and scale user apps properly
package main
import (
context "context"
"fmt"
"math/rand"
"time"
"github.com/go-logr/logr"
"google.golang.org/protobuf/types/known/emptypb"
"github.com/kedacore/http-add-on/pkg/routing"
externalscaler "github.com/kedacore/http-add-on/proto"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
const (
interceptor = "interceptor"
httpRequests = "http-requests"
)
type impl struct {
lggr logr.Logger
pinger *queuePinger
routingTable routing.TableReader
targetMetric int64
targetMetricInterceptor int64
externalscaler.UnimplementedExternalScalerServer
}
func newImpl(
lggr logr.Logger,
pinger *queuePinger,
routingTable routing.TableReader,
defaultTargetMetric int64,
defaultTargetMetricInterceptor int64,
) *impl {
return &impl{
lggr: lggr,
pinger: pinger,
routingTable: routingTable,
targetMetric: defaultTargetMetric,
targetMetricInterceptor: defaultTargetMetricInterceptor,
}
}
func (e *impl) Ping(context.Context, *emptypb.Empty) (*emptypb.Empty, error) {
return &emptypb.Empty{}, nil
}
func (e *impl) IsActive(
ctx context.Context,
scaledObject *externalscaler.ScaledObjectRef,
) (*externalscaler.IsActiveResponse, error) {
lggr := e.lggr.WithName("IsActive")
hosts, err := getHostsFromScaledObjectRef(lggr, scaledObject)
if err != nil {
return nil, err
}
totalHostCount := 0
for _, host := range hosts {
if host == interceptor {
return &externalscaler.IsActiveResponse{
Result: true,
}, nil
}
hostCount, ok := getHostCount(
host,
e.pinger.counts(),
e.routingTable,
)
if !ok {
err := fmt.Errorf("host '%s' not found in counts", host)
allCounts := e.pinger.mergeCountsWithRoutingTable(
e.routingTable,
)
lggr.Error(err, "Given host was not found in queue count map", "host", host, "allCounts", allCounts)
return nil, err
}
totalHostCount += hostCount
}
active := totalHostCount > 0
return &externalscaler.IsActiveResponse{
Result: active,
}, nil
}
func (e *impl) StreamIsActive(
scaledObject *externalscaler.ScaledObjectRef,
server externalscaler.ExternalScaler_StreamIsActiveServer,
) error {
// this function communicates with KEDA via the 'server' parameter.
// we call server.Send (below) every 200ms, which tells it to immediately
// ping our IsActive RPC
ticker := time.NewTicker(2 * time.Second)
defer ticker.Stop()
for {
select {
case <-server.Context().Done():
return nil
case <-ticker.C:
active, err := e.IsActive(server.Context(), scaledObject)
if err != nil {
e.lggr.Error(
err,
"error getting active status in stream",
)
return err
}
err = server.Send(&externalscaler.IsActiveResponse{
Result: active.Result,
})
if err != nil {
e.lggr.Error(
err,
"error sending the active result in stream",
)
return err
}
}
}
}
func (e *impl) GetMetricSpec(
_ context.Context,
sor *externalscaler.ScaledObjectRef,
) (*externalscaler.GetMetricSpecResponse, error) {
lggr := e.lggr.WithName("GetMetricSpec")
hosts, err := getHostsFromScaledObjectRef(lggr, sor)
if err != nil {
return nil, err
}
var targetPendingRequests int64
var host = hosts[0] // We are only interested in the first host to get the targetPendingRequests
if host == interceptor {
targetPendingRequests = e.targetMetricInterceptor
} else {
target, err := e.routingTable.Lookup(host)
if err != nil {
lggr.Error(
err,
"error getting target for host",
"host",
host,
)
return nil, err
}
host = httpRequests
targetPendingRequests = int64(target.TargetPendingRequests)
}
metricSpecs := []*externalscaler.MetricSpec{
{
MetricName: host,
TargetSize: targetPendingRequests,
},
}
return &externalscaler.GetMetricSpecResponse{
MetricSpecs: metricSpecs,
}, nil
}
func (e *impl) GetMetrics(
_ context.Context,
metricRequest *externalscaler.GetMetricsRequest,
) (*externalscaler.GetMetricsResponse, error) {
lggr := e.lggr.WithName("GetMetrics")
hosts, err := getHostsFromScaledObjectRef(lggr, metricRequest.ScaledObjectRef)
if err != nil {
return nil, err
}
var totalCount int64
var metricName = httpRequests
for _, host := range hosts {
hostCount, ok := getHostCount(
host,
e.pinger.counts(),
e.routingTable,
)
if !ok {
if host != interceptor {
err := fmt.Errorf("host '%s' not found in counts", host)
allCounts := e.pinger.mergeCountsWithRoutingTable(e.routingTable)
lggr.Error(err, "allCounts", allCounts)
return nil, err
}
hostCount = e.pinger.aggregate()
metricName = interceptor
}
totalCount += int64(hostCount)
}
metricValues := []*externalscaler.MetricValue{
{
MetricName: metricName,
MetricValue: totalCount,
},
}
return &externalscaler.GetMetricsResponse{
MetricValues: metricValues,
}, nil
}