This repository has been archived by the owner on Dec 5, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 323
/
serviceinfo.go
164 lines (136 loc) · 3.35 KB
/
serviceinfo.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package skynet
import (
"fmt"
"github.com/skynetservices/skynet/config"
"github.com/skynetservices/skynet/log"
"net"
"strconv"
"strings"
"sync"
)
var portMutex sync.Mutex
// ServiceStatistics contains information about its service that can
// be used to estimate load.
type ServiceStatistics struct {
// Clients is the number of clients currently connected to this service.
Clients int32
// StartTime is the time when the service began running.
StartTime string
// LastRequest is the time when the last request was made.
LastRequest string
}
// ServiceInfo is the publicly reported information about a particular
// service instance.
type ServiceInfo struct {
UUID string
Name string
Version string
Region string
ServiceAddr BindAddr
// Registered indicates if the instance is currently accepting requests.
Registered bool
}
func (si ServiceInfo) AddrString() string {
return si.ServiceAddr.String()
}
func NewServiceInfo(name, version string) (si *ServiceInfo) {
// TODO: we need to grab Host/Region/ServiceAddr from config
si = &ServiceInfo{
Name: name,
Version: version,
UUID: config.UUID(),
}
var host string
var minPort, maxPort int
if r, err := config.String(name, version, "region"); err == nil {
si.Region = r
} else {
si.Region = config.DefaultRegion
}
if h, err := config.String(name, version, "host"); err == nil {
host = h
} else {
host = config.DefaultHost
}
if p, err := config.Int(name, version, "service.port.min"); err == nil {
minPort = p
} else {
minPort = config.DefaultMinPort
}
if p, err := config.Int(name, version, "service.port.max"); err == nil {
maxPort = p
} else {
maxPort = config.DefaultMaxPort
}
log.Println(log.TRACE, host, minPort, maxPort)
si.ServiceAddr = BindAddr{IPAddress: host, Port: minPort, MaxPort: maxPort}
return si
}
type BindAddr struct {
IPAddress string
Port int
MaxPort int
}
func BindAddrFromString(host string) (ba BindAddr, err error) {
if host == "" {
return
}
split := strings.Index(host, ":")
if split == -1 {
err = fmt.Errorf("Must specify a port for address (got %q)", host)
return
}
ba = BindAddr{}
ba.IPAddress = host[:split]
if ba.IPAddress == "" {
ba.IPAddress = "0.0.0.0"
}
portstr := host[split+1:]
if ba.Port, err = strconv.Atoi(portstr); err == nil {
return
}
var rindex int
if rindex = strings.Index(portstr, "-"); rindex == -1 {
err = fmt.Errorf("Couldn't process port for %q: %v", host, err)
return
}
maxPortStr := portstr[rindex+1:]
portstr = portstr[:rindex]
if ba.Port, err = strconv.Atoi(portstr); err != nil {
err = fmt.Errorf("Couldn't process port for %q: %v", host, err)
return
}
if ba.MaxPort, err = strconv.Atoi(maxPortStr); err != nil {
err = fmt.Errorf("Couldn't process port for %q: %v", host, err)
return
}
return
}
func (ba *BindAddr) String() string {
if ba == nil {
return ""
}
return fmt.Sprintf("%s:%d", ba.IPAddress, ba.Port)
}
func (ba *BindAddr) Listen() (listener *net.TCPListener, err error) {
// Ensure Admin, and RPC don't fight over the same port
portMutex.Lock()
defer portMutex.Unlock()
for {
var laddr *net.TCPAddr
laddr, err = net.ResolveTCPAddr("tcp", ba.String())
if err != nil {
panic(err)
}
listener, err = net.ListenTCP("tcp", laddr)
if err == nil {
return
}
if ba.Port < ba.MaxPort {
ba.Port++
} else {
return
}
}
return
}