Skip to content

Commit 9833534

Browse files
committed
Do not cache ports in the DiscoveryClient
there is already a cache in the DiscoveryManager there is no need to duplicate it.
1 parent 5330736 commit 9833534

File tree

2 files changed

+16
-24
lines changed

2 files changed

+16
-24
lines changed

arduino/discovery/discovery.go

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ type PluggableDiscovery struct {
5757
incomingMessagesError error
5858
state int
5959
eventChan chan<- *Event
60-
cachedPorts map[string]*Port
6160
}
6261

6362
type discoveryMessage struct {
@@ -132,7 +131,6 @@ func New(id string, args ...string) *PluggableDiscovery {
132131
id: id,
133132
processArgs: args,
134133
state: Dead,
135-
cachedPorts: map[string]*Port{},
136134
}
137135
}
138136

@@ -177,7 +175,6 @@ func (disc *PluggableDiscovery) jsonDecodeLoop(in io.Reader, outChan chan<- *dis
177175
return
178176
}
179177
disc.statusMutex.Lock()
180-
disc.cachedPorts[msg.Port.Address+"|"+msg.Port.Protocol] = msg.Port
181178
if disc.eventChan != nil {
182179
disc.eventChan <- &Event{"add", msg.Port, disc.GetID()}
183180
}
@@ -188,7 +185,6 @@ func (disc *PluggableDiscovery) jsonDecodeLoop(in io.Reader, outChan chan<- *dis
188185
return
189186
}
190187
disc.statusMutex.Lock()
191-
delete(disc.cachedPorts, msg.Port.Address+"|"+msg.Port.Protocol)
192188
if disc.eventChan != nil {
193189
disc.eventChan <- &Event{"remove", msg.Port, disc.GetID()}
194190
}
@@ -371,12 +367,6 @@ func (disc *PluggableDiscovery) Stop() error {
371367

372368
func (disc *PluggableDiscovery) stopSync() {
373369
if disc.eventChan != nil {
374-
// When stopping sync send a batch of "remove" events for
375-
// all the active ports.
376-
for _, port := range disc.cachedPorts {
377-
disc.eventChan <- &Event{"remove", port, disc.GetID()}
378-
}
379-
disc.cachedPorts = map[string]*Port{}
380370
disc.eventChan <- &Event{"stop", nil, disc.GetID()}
381371
close(disc.eventChan)
382372
disc.eventChan = nil
@@ -440,16 +430,3 @@ func (disc *PluggableDiscovery) StartSync(size int) (<-chan *Event, error) {
440430
disc.eventChan = c
441431
return c, nil
442432
}
443-
444-
// ListCachedPorts returns a list of the available ports. The list is a cache of all the
445-
// add/remove events happened from the StartSync call and it will not consume any
446-
// resource from the underliying discovery.
447-
func (disc *PluggableDiscovery) ListCachedPorts() []*Port {
448-
disc.statusMutex.Lock()
449-
defer disc.statusMutex.Unlock()
450-
res := []*Port{}
451-
for _, port := range disc.cachedPorts {
452-
res = append(res, port)
453-
}
454-
return res
455-
}

arduino/discovery/discoverymanager/discoverymanager.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,22 @@ func (dm *DiscoveryManager) feedEvent(ev *discovery.Event) {
214214
}
215215

216216
if ev.Type == "stop" {
217-
// Remove all the cached events for the terminating discovery
217+
// Send remove events for all the cached ports of the terminating discovery
218+
cache := dm.watchersCache[ev.DiscoveryID]
219+
for _, addEv := range cache {
220+
removeEv := &discovery.Event{
221+
Type: "remove",
222+
Port: &discovery.Port{
223+
Address: addEv.Port.Address,
224+
AddressLabel: addEv.Port.AddressLabel,
225+
Protocol: addEv.Port.Protocol,
226+
ProtocolLabel: addEv.Port.ProtocolLabel},
227+
DiscoveryID: addEv.DiscoveryID,
228+
}
229+
sendToAllWatchers(removeEv)
230+
}
231+
232+
// Remove the cache for the terminating discovery
218233
delete(dm.watchersCache, ev.DiscoveryID)
219234
return
220235
}

0 commit comments

Comments
 (0)