-
Notifications
You must be signed in to change notification settings - Fork 29
/
servers.go
71 lines (54 loc) · 1.37 KB
/
servers.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright (c) 2020-2021, R.I. Pienaar and the Choria Project contributors
//
// SPDX-License-Identifier: Apache-2.0
package srvcache
import (
"net/url"
)
type servers struct {
servers []Server
}
// Servers returns a copy of the server list, changes made to server instances
// will not be reflected in the source collection
func (s *servers) Servers() []Server {
servers := make([]Server, len(s.servers))
copy(servers, s.servers)
return servers
}
// Count is the amount of servers stored
func (s *servers) Count() int {
return len(s.servers)
}
// Each invokes f on each contained Server
func (s *servers) Each(f func(Server)) {
for _, srv := range s.servers {
f(srv)
}
}
// Strings returns a list of urls for each Server
func (s *servers) Strings() (urls []string) {
urls = make([]string, len(s.servers))
for i, s := range s.servers {
urls[i] = s.String()
}
return urls
}
// URLs returns a list of *url.URL for each Server
func (s *servers) URLs() (urls []*url.URL, err error) {
urls = make([]*url.URL, len(s.servers))
for i, s := range s.servers {
urls[i], err = s.URL()
if err != nil {
return nil, err
}
}
return urls, nil
}
// HostPorts returns a list of host:port strings for each server
func (s *servers) HostPorts() (hps []string) {
hps = make([]string, len(s.servers))
for i, s := range s.servers {
hps[i] = s.HostPort()
}
return hps
}