• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nitroshare / gomdns / 16461252732

23 Jul 2025 04:04AM UTC coverage: 14.935% (-70.3%) from 85.185%
16461252732

push

github

nathan-osman
Add initial implementation of an mDNS cache.

0 of 127 new or added lines in 3 files covered. (0.0%)

23 of 154 relevant lines covered (14.94%)

0.18 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/cache/cache.go
1
package cache
2

3
import (
4
        "log/slog"
5
        "sync"
6
        "time"
7

8
        "github.com/nitroshare/gomdns/util/list"
9
)
10

11
var (
12
        fnNow   = time.Now
13
        fnAfter = time.After
14
)
15

16
type recordEntry struct {
17
        record   *Record
18
        triggers *list.List[time.Time]
19
}
20

21
// Cache stores records received from DNS queries and sends on the
22
// shouldQuery channel when records are about to expire.
23
type Cache struct {
24
        mutex       sync.Mutex
25
        logger      *slog.Logger
26
        entries     *list.List[*recordEntry]
27
        chanQuery   chan<- *Record
28
        chanExpired chan<- *Record
29
        chanAdd     chan *Record
30
        chanClosed  chan any
31
}
32

NEW
33
func (c *Cache) run() {
×
NEW
34
        defer close(c.chanClosed)
×
NEW
35
        for {
×
NEW
36
                select {
×
NEW
37
                case <-c.nextTrigger():
×
NEW
38
                case r, ok := <-c.chanAdd:
×
NEW
39
                        if !ok {
×
NEW
40
                                return
×
NEW
41
                        }
×
NEW
42
                        c.add(r)
×
43
                }
44
        }
45
}
46

47
// New returns a new Cache instance.
NEW
48
func New(cfg *Config) *Cache {
×
NEW
49
        c := &Cache{
×
NEW
50
                logger:      cfg.Logger,
×
NEW
51
                entries:     &list.List[*recordEntry]{},
×
NEW
52
                chanQuery:   cfg.ChanQuery,
×
NEW
53
                chanExpired: cfg.ChanExpired,
×
NEW
54
                chanAdd:     make(chan *Record),
×
NEW
55
                chanClosed:  make(chan any),
×
NEW
56
        }
×
NEW
57
        if c.logger == nil {
×
NEW
58
                c.logger = slog.Default()
×
NEW
59
        }
×
NEW
60
        go c.run()
×
NEW
61
        return c
×
62
}
63

64
// Add adds a record to the cache.
NEW
65
func (c *Cache) Add(record *Record) {
×
NEW
66
        c.chanAdd <- record
×
NEW
67
}
×
68

69
// LookupByName returns all records for the provided name.
NEW
70
func (c *Cache) LookupByName(name string) []*Record {
×
NEW
71
        c.mutex.Lock()
×
NEW
72
        defer c.mutex.Unlock()
×
NEW
73
        records := []*Record{}
×
NEW
74
        for e := c.entries.Front; e != nil; e = e.Next {
×
NEW
75
                if e.Value.record.Name == name {
×
NEW
76
                        records = append(records, e.Value.record)
×
NEW
77
                }
×
78
        }
NEW
79
        return records
×
80
}
81

82
// LookupByNameAndType returns all records of the specified type for the
83
// provided name.
NEW
84
func (c *Cache) LookupByNameAndType(name string, _type uint16) []*Record {
×
NEW
85
        c.mutex.Lock()
×
NEW
86
        defer c.mutex.Unlock()
×
NEW
87
        records := []*Record{}
×
NEW
88
        for e := c.entries.Front; e != nil; e = e.Next {
×
NEW
89
                if e.Value.record.Name == name &&
×
NEW
90
                        e.Value.record.Type == _type {
×
NEW
91
                        records = append(records, e.Value.record)
×
NEW
92
                }
×
93
        }
NEW
94
        return records
×
95
}
96

97
// Close shuts down the cache.
NEW
98
func (c *Cache) Close() {
×
NEW
99
        close(c.chanAdd)
×
NEW
100
        <-c.chanClosed
×
NEW
101
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc