mirror of
https://github.com/octoleo/syncthing.git
synced 2024-12-22 02:48:59 +00:00
1bae4b7f50
* all: Use context in lib/dialer * a bit slimmer * https://github.com/syncthing/syncthing/pull/5753 * bot * missed adding debug.go * errors.Cause * simultaneous dialing * anti-leak
66 lines
1.2 KiB
Go
66 lines
1.2 KiB
Go
// Copyright (C) 2015 The Syncthing Authors.
|
|
//
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
package nat
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
type DiscoverFunc func(ctx context.Context, renewal, timeout time.Duration) []Device
|
|
|
|
var providers []DiscoverFunc
|
|
|
|
func Register(provider DiscoverFunc) {
|
|
providers = append(providers, provider)
|
|
}
|
|
|
|
func discoverAll(ctx context.Context, renewal, timeout time.Duration) map[string]Device {
|
|
wg := &sync.WaitGroup{}
|
|
wg.Add(len(providers))
|
|
|
|
c := make(chan Device)
|
|
done := make(chan struct{})
|
|
|
|
for _, discoverFunc := range providers {
|
|
go func(f DiscoverFunc) {
|
|
defer wg.Done()
|
|
for _, dev := range f(ctx, renewal, timeout) {
|
|
select {
|
|
case c <- dev:
|
|
case <-ctx.Done():
|
|
return
|
|
}
|
|
}
|
|
}(discoverFunc)
|
|
}
|
|
|
|
nats := make(map[string]Device)
|
|
|
|
go func() {
|
|
defer close(done)
|
|
for {
|
|
select {
|
|
case dev, ok := <-c:
|
|
if !ok {
|
|
return
|
|
}
|
|
nats[dev.ID()] = dev
|
|
case <-ctx.Done():
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
|
|
wg.Wait()
|
|
close(c)
|
|
<-done
|
|
|
|
return nats
|
|
}
|