From 22c6ad6b8c231cfb95e224509d9e5e750703c9f1 Mon Sep 17 00:00:00 2001 From: Juliusz Chroboczek Date: Sun, 13 Sep 2020 12:24:06 +0200 Subject: [PATCH] Implement ranging over groups. --- group.go | 26 ++++++++++++++++++-------- webserver.go | 7 +++---- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/group.go b/group.go index 5310947..8a79fdc 100644 --- a/group.go +++ b/group.go @@ -123,14 +123,25 @@ func addGroup(name string, desc *groupDescription) (*group, error) { return g, nil } -func getGroupNames() []string { +func rangeGroups(f func(g *group) bool) { groups.mu.Lock() defer groups.mu.Unlock() - names := make([]string, 0, len(groups.groups)) - for name := range groups.groups { - names = append(names, name) + for _, g := range groups.groups { + ok := f(g) + if !ok { + break + } } +} + +func getGroupNames() []string { + names := make([]string, 0) + + rangeGroups(func(g *group) bool { + names = append(names, g.name) + return true + }) return names } @@ -408,16 +419,15 @@ type publicGroup struct { func getPublicGroups() []publicGroup { gs := make([]publicGroup, 0) - groups.mu.Lock() - defer groups.mu.Unlock() - for _, g := range groups.groups { + rangeGroups(func (g *group) bool { if g.description.Public { gs = append(gs, publicGroup{ Name: g.name, ClientCount: len(g.clients), }) } - } + return true + }) sort.Slice(gs, func(i, j int) bool { return gs[i].Name < gs[j].Name }) diff --git a/webserver.go b/webserver.go index 2d312e0..6ca8050 100644 --- a/webserver.go +++ b/webserver.go @@ -47,11 +47,10 @@ func webserver() { IdleTimeout: 120 * time.Second, } server.RegisterOnShutdown(func() { - groups.mu.Lock() - defer groups.mu.Unlock() - for _, g := range groups.groups { + rangeGroups(func (g *group) bool { go g.shutdown("server is shutting down") - } + return true + }) }) go func() { var err error