lib/discover: Add instance ID to local discovery (fixes #3278)

A random "instance ID" is generated on each start of the local discovery
service. The instance ID is included in the announcement. When we see a
new instance ID we treat is a new device and respond with an
announcement of our own. Hence devices get to know each other quickly on
restart.

GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/3385
This commit is contained in:
Jakob Borg
2016-07-04 11:16:48 +00:00
committed by Audrius Butkevicius
parent 8d0019595f
commit 9a0e5a7c18
5 changed files with 128 additions and 16 deletions

View File

@@ -22,6 +22,7 @@ import (
"github.com/syncthing/syncthing/lib/beacon"
"github.com/syncthing/syncthing/lib/events"
"github.com/syncthing/syncthing/lib/protocol"
"github.com/syncthing/syncthing/lib/rand"
"github.com/thejerf/suture"
)
@@ -114,8 +115,9 @@ func (c *localClient) Error() error {
func (c *localClient) announcementPkt() Announce {
return Announce{
ID: c.myID[:],
Addresses: c.addrList.AllAddresses(),
ID: c.myID[:],
Addresses: c.addrList.AllAddresses(),
InstanceID: rand.Int63(),
}
}
@@ -194,9 +196,11 @@ func (c *localClient) registerDevice(src net.Addr, device Announce) bool {
copy(id[:], device.ID)
// Remember whether we already had a valid cache entry for this device.
// If the instance ID has changed the remote device has restarted since
// we last heard from it, so we should treat it as a new device.
ce, existsAlready := c.Get(id)
isNewDevice := !existsAlready || time.Since(ce.when) > CacheLifeTime
isNewDevice := !existsAlready || time.Since(ce.when) > CacheLifeTime || ce.instanceID != device.InstanceID
// Any empty or unspecified addresses should be set to the source address
// of the announcement. We also skip any addresses we can't parse.
@@ -245,9 +249,10 @@ func (c *localClient) registerDevice(src net.Addr, device Announce) bool {
}
c.Set(id, CacheEntry{
Addresses: validAddresses,
when: time.Now(),
found: true,
Addresses: validAddresses,
when: time.Now(),
found: true,
instanceID: device.InstanceID,
})
if isNewDevice {