Skip to content

Commit b5c9afa

Browse files
ivanmatmatioktalz
authored andcommitted
BUG: fix rate limiting backend management
This commit introduces the function BackendUsed to determine if a backend is present and used.
1 parent 9dd6df6 commit b5c9afa

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

pkg/haproxy/api/api.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ type HAProxyClient interface { //nolint:interfacebloat
3737
ACLEdit(id int64, parentType string, parentName string, data *models.ACL) error
3838
BackendsGet() models.Backends
3939
BackendGet(backendName string) (*models.Backend, error)
40+
// This function tests if a backend is existing :
41+
// Check if you're not rather looking for BackendUsed function.
4042
BackendExists(backendName string) bool
43+
// This function tests if a backend is existing AND IT'S USED.
44+
BackendUsed(backendName string) bool
4145
BackendCreatePermanently(backend models.Backend)
4246
BackendCreateIfNotExist(backend models.Backend)
4347
BackendCreateOrUpdate(backend models.Backend) (map[string][]interface{}, bool)

pkg/haproxy/api/backend.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (c *clientNative) BackendCreateIfNotExist(backend models.Backend) {
4141
existingBackend := c.backends[backend.Name]
4242
existingBackend.Used = true
4343
c.backends[backend.Name] = existingBackend
44-
if c.BackendExists(backend.Name) {
44+
if c.BackendUsed(backend.Name) {
4545
return
4646
}
4747
c.BackendCreateOrUpdate(backend)
@@ -240,6 +240,8 @@ func (c *clientNative) BackendServersGet(backendName string) (models.Servers, er
240240
return servers, nil
241241
}
242242

243+
// This function tests if a backend is existing
244+
// Check if you're not rather looking for BackendUsed function.
243245
func (c *clientNative) BackendExists(backendName string) (exists bool) {
244246
_, exists = c.backends[backendName]
245247
return
@@ -266,3 +268,12 @@ func (c *clientNative) BackendDeleteAllUnnecessary() ([]string, error) {
266268
}
267269
return backendDeleted, errs.Result()
268270
}
271+
272+
// This function tests if a backend is existing AND IT'S USED.
273+
func (c *clientNative) BackendUsed(backendName string) bool {
274+
backend, exists := c.backends[backendName]
275+
if !exists {
276+
return false
277+
}
278+
return backend.Used
279+
}

pkg/haproxy/rules/reqTrack.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,17 @@ func (r ReqTrack) Create(client api.HAProxyClient, frontend *models.Frontend, in
2727
}
2828

2929
// Create tracking table.
30-
client.BackendCreateOrUpdate(models.Backend{
31-
Name: r.TableName,
32-
StickTable: &models.ConfigStickTable{
33-
Peers: "localinstance",
34-
Type: "ip",
35-
Size: r.TableSize,
36-
Store: fmt.Sprintf("http_req_rate(%d)", *r.TablePeriod),
37-
},
38-
})
30+
if !client.BackendUsed(r.TableName) {
31+
client.BackendCreateOrUpdate(models.Backend{
32+
Name: r.TableName,
33+
StickTable: &models.ConfigStickTable{
34+
Peers: "localinstance",
35+
Type: "ip",
36+
Size: r.TableSize,
37+
Store: fmt.Sprintf("http_req_rate(%d)", *r.TablePeriod),
38+
},
39+
})
40+
}
3941
// Create rule
4042
httpRule := models.HTTPRequestRule{
4143
Index: utils.PtrInt64(0),

0 commit comments

Comments
 (0)