@@ -19,15 +19,15 @@ package main
19
19
20
20
import (
21
21
"context"
22
+ "fmt"
22
23
"sync"
23
24
"time"
24
25
25
26
discovery "github.com/arduino/pluggable-discovery-protocol-handler"
26
27
)
27
28
28
- // cacheItem stores the Port discovered and its timer to handle TTL.
29
+ // cacheItem stores TTL of discovered ports and its timer to handle TTL.
29
30
type cacheItem struct {
30
- Port * discovery.Port
31
31
timerTTL * time.Timer
32
32
ctx context.Context
33
33
cancelFunc context.CancelFunc
@@ -47,18 +47,20 @@ type portsCache struct {
47
47
// newCache creates a new portsCache and returns it.
48
48
// itemsTTL is the TTL of a single item, when it's reached
49
49
// the stored item is deleted.
50
- func newCache (itemsTTL time.Duration ) * portsCache {
50
+ func newCache (itemsTTL time.Duration , deletionCallback func ( port * discovery. Port ) ) * portsCache {
51
51
return & portsCache {
52
- itemsTTL : itemsTTL ,
53
- data : make (map [string ]* cacheItem ),
52
+ itemsTTL : itemsTTL ,
53
+ data : make (map [string ]* cacheItem ),
54
+ deletionCallback : deletionCallback ,
54
55
}
55
56
}
56
57
57
- // set stores a new port and sets its TTL.
58
- // If the specified key is already found the item's TTL is
59
- // renewed.
60
- // set is thread safe.
61
- func (c * portsCache ) set (key string , port * discovery.Port ) {
58
+ // storeOrUpdate stores a new port and sets its TTL or
59
+ // updates the TTL if already stored.
60
+ // Return true if the port TTL has been updated, false otherwise
61
+ // storeOrUpdate is thread safe.
62
+ func (c * portsCache ) storeOrUpdate (port * discovery.Port ) bool {
63
+ key := fmt .Sprintf ("%s:%s %s" , port .Address , port .Properties .Get ("port" ), port .Properties .Get ("board" ))
62
64
c .dataMutex .Lock ()
63
65
defer c .dataMutex .Unlock ()
64
66
// We need a cancellable context to avoid leaving
@@ -77,20 +79,19 @@ func (c *portsCache) set(key string, port *discovery.Port) {
77
79
item .cancelFunc = cancelFunc
78
80
} else {
79
81
item = & cacheItem {
80
- Port : port ,
81
82
timerTTL : timerTTL ,
82
83
ctx : ctx ,
83
84
cancelFunc : cancelFunc ,
84
85
}
85
86
c .data [key ] = item
86
87
}
87
88
88
- go func (key string , item * cacheItem ) {
89
+ go func (key string , item * cacheItem , port * discovery. Port ) {
89
90
select {
90
91
case <- item .timerTTL .C :
91
92
c .dataMutex .Lock ()
92
93
defer c .dataMutex .Unlock ()
93
- c .deletionCallback (item . Port )
94
+ c .deletionCallback (port )
94
95
delete (c .data , key )
95
96
return
96
97
case <- item .ctx .Done ():
@@ -102,19 +103,9 @@ func (c *portsCache) set(key string, port *discovery.Port) {
102
103
// Using a context we handle this gracefully.
103
104
return
104
105
}
105
- }(key , item )
106
- }
106
+ }(key , item , port )
107
107
108
- // get returns the item stored with the specified key and true.
109
- // If the item is not found returns a nil port and false.
110
- // get is thread safe.
111
- func (c * portsCache ) get (key string ) (* discovery.Port , bool ) {
112
- c .dataMutex .Lock ()
113
- defer c .dataMutex .Unlock ()
114
- if item , ok := c .data [key ]; ok {
115
- return item .Port , ok
116
- }
117
- return nil , false
108
+ return ok
118
109
}
119
110
120
111
// clear removes all the stored items and stops their TTL timers.
0 commit comments