delete empty targets sets to avoid memory leaks

This commit is contained in:
Krasi Georgiev 2018-01-12 13:10:59 +00:00
parent abfd9f1920
commit cabce21b70
2 changed files with 60 additions and 1 deletions

View File

@ -179,8 +179,16 @@ func (m *Manager) allGroups() map[string][]*targetgroup.Group {
m.actionCh <- func(ctx context.Context) {
tSetsAll := map[string][]*targetgroup.Group{}
for pkey, tsets := range m.targets {
del := true
for _, tg := range tsets {
tSetsAll[pkey.setName] = append(tSetsAll[pkey.setName], tg)
if len(tg.Targets) != 0 {
tSetsAll[pkey.setName] = append(tSetsAll[pkey.setName], tg)
del = false
}
}
// Delete the empty map for this target set to avoid memory leaks.
if del {
delete(m.targets, pkey)
}
}
tSets <- tSetsAll

View File

@ -649,6 +649,57 @@ func TestDiscoveryManagerSyncCalls(t *testing.T) {
},
},
},
{
title: "Single TP update with an empty group to check for memory leaks",
updates: map[string][]update{
"tp1": {
{
targetGroups: []targetgroup.Group{
{
Source: "tp1_group1",
Targets: []model.LabelSet{{"__instance__": "1"}},
},
{
Source: "tp1_group2",
Targets: []model.LabelSet{{"__instance__": "2"}},
},
},
interval: 30,
},
{
targetGroups: []targetgroup.Group{
{
Source: "tp1_group1",
Targets: []model.LabelSet{{"__instance__": "3"}},
},
{
Source: "tp1_group2",
Targets: nil,
},
},
interval: 10,
},
},
},
expectedTargets: [][]*targetgroup.Group{
{
{
Source: "tp1_group1",
Targets: []model.LabelSet{{"__instance__": "1"}},
},
{
Source: "tp1_group2",
Targets: []model.LabelSet{{"__instance__": "2"}},
},
},
{
{
Source: "tp1_group1",
Targets: []model.LabelSet{{"__instance__": "3"}},
},
},
},
},
}
for testIndex, testCase := range testCases {