-
Notifications
You must be signed in to change notification settings - Fork 149
/
client_reader.go
42 lines (37 loc) · 1017 Bytes
/
client_reader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package clientreader
import (
"doppler/dopplerservice"
"fmt"
)
//go:generate hel --type ClientPool --output mock_client_pool_test.go
type ClientPool interface {
SetAddresses(addresses []string) int
}
func Read(clientPool map[string]ClientPool, protocols []string, event dopplerservice.Event) string {
protocol, servers := chooseProtocol(protocols, event)
if protocol == "" {
panic(fmt.Sprintf("No dopplers listening on %v", protocols))
}
clients := clientPool[protocol].SetAddresses(servers)
if clients == 0 {
panic(fmt.Sprintf("Unable to connect to dopplers running on %s", protocol))
}
return protocol
}
func chooseProtocol(protocols []string, event dopplerservice.Event) (string, []string) {
for _, protocol := range protocols {
var dopplers []string
switch protocol {
case "udp":
dopplers = event.UDPDopplers
case "tcp":
dopplers = event.TCPDopplers
case "tls":
dopplers = event.TLSDopplers
}
if len(dopplers) > 0 {
return protocol, dopplers
}
}
return "", nil
}