UPnP/SSDP client: avoid duplicate URLs
This commit is contained in:
parent
ae892ff712
commit
eec4895c73
@ -38,7 +38,8 @@ type Client struct {
|
|||||||
wrappedLog *ssdpLogger
|
wrappedLog *ssdpLogger
|
||||||
|
|
||||||
mutex *sync.Mutex
|
mutex *sync.Mutex
|
||||||
urls []string
|
urls []string // Preserves order
|
||||||
|
seenURLs map[string]bool // Removes duplicates
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(logger zerolog.Logger) (*Client, error) {
|
func NewClient(logger zerolog.Logger) (*Client, error) {
|
||||||
@ -49,6 +50,7 @@ func NewClient(logger zerolog.Logger) (*Client, error) {
|
|||||||
|
|
||||||
mutex: new(sync.Mutex),
|
mutex: new(sync.Mutex),
|
||||||
urls: make([]string, 0),
|
urls: make([]string, 0),
|
||||||
|
seenURLs: make(map[string]bool),
|
||||||
}
|
}
|
||||||
|
|
||||||
ssdp, err := gossdp.NewSsdpClientWithLogger(&client, wrap)
|
ssdp, err := gossdp.NewSsdpClientWithLogger(&client, wrap)
|
||||||
@ -111,7 +113,15 @@ func (c *Client) appendURLs(location string) {
|
|||||||
c.mutex.Lock()
|
c.mutex.Lock()
|
||||||
defer c.mutex.Unlock()
|
defer c.mutex.Unlock()
|
||||||
|
|
||||||
c.urls = append(c.urls, urls...)
|
// Only append URLs that we haven't seen yet.
|
||||||
|
for _, url := range urls {
|
||||||
|
if c.seenURLs[url] {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
c.urls = append(c.urls, url)
|
||||||
|
c.seenURLs[url] = true
|
||||||
|
}
|
||||||
|
|
||||||
c.log.Debug().
|
c.log.Debug().
|
||||||
Int("new", len(urls)).
|
Int("new", len(urls)).
|
||||||
Int("total", len(c.urls)).
|
Int("total", len(c.urls)).
|
||||||
|
Loading…
x
Reference in New Issue
Block a user