-
Notifications
You must be signed in to change notification settings - Fork 1
/
entry_groups.go
53 lines (43 loc) · 1.1 KB
/
entry_groups.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
package main
import (
"fmt"
"sync"
"github.com/holoplot/go-avahi"
"ldddns.arnested.dk/internal/log"
)
type entryGroups struct {
avahiServer *avahi.Server
groups map[string]*avahi.EntryGroup
mutex sync.Mutex
}
func newEntryGroups(avahiServer *avahi.Server) *entryGroups {
return &entryGroups{
avahiServer: avahiServer,
groups: make(map[string]*avahi.EntryGroup),
mutex: sync.Mutex{},
}
}
func (e *entryGroups) get(containerID string) (*avahi.EntryGroup, func(), error) {
commit := func() {
empty, err := e.groups[containerID].IsEmpty()
if err != nil {
log.Logf(log.PriErr, "checking whether Avahi entry group is empty: %v", err)
}
if !empty {
err := e.groups[containerID].Commit()
if err != nil {
log.Logf(log.PriErr, "error committing: %v", err)
}
}
e.mutex.Unlock()
}
e.mutex.Lock()
if _, ok := e.groups[containerID]; !ok {
eg, err := e.avahiServer.EntryGroupNew()
if err != nil {
return nil, commit, fmt.Errorf("error creating new entry group: %w", err)
}
e.groups[containerID] = eg
}
return e.groups[containerID], commit, nil
}