/
githubmirror
/
syncthing
Обзор
Документация
Войти
/
githubmirror
/
syncthing
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
lib/nat/registry.go
65 строк
1 KB
Simon Frei
all: Use context in lib/dialer (#6177)
26 ноя 2019, 10:39
26 ноя 2019, 10:39
1bae4b7
Код
Авторство
О чём код?
// 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 }