forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
210 lines (175 loc) · 4.3 KB
/
service.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package meta // import "github.com/influxdata/influxdb/services/meta"
import (
"crypto/tls"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"strings"
"time"
"github.com/influxdata/influxdb"
)
const (
MuxHeader = 8
)
type Service struct {
RaftListener net.Listener
Version string
config *Config
handler *handler
ln net.Listener
httpAddr string
raftAddr string
https bool
cert string
err chan error
Logger *log.Logger
store *store
Node *influxdb.Node
}
// NewService returns a new instance of Service.
func NewService(c *Config) *Service {
s := &Service{
config: c,
httpAddr: c.HTTPBindAddress,
raftAddr: c.BindAddress,
https: c.HTTPSEnabled,
cert: c.HTTPSCertificate,
err: make(chan error),
}
if c.LoggingEnabled {
s.Logger = log.New(os.Stderr, "[meta] ", log.LstdFlags)
} else {
s.Logger = log.New(ioutil.Discard, "", 0)
}
return s
}
// Open starts the service
func (s *Service) Open() error {
s.Logger.Println("Starting meta service")
if s.RaftListener == nil {
panic("no raft listener set")
}
// Open listener.
if s.https {
cert, err := tls.LoadX509KeyPair(s.cert, s.cert)
if err != nil {
return err
}
listener, err := tls.Listen("tcp", s.httpAddr, &tls.Config{
Certificates: []tls.Certificate{cert},
})
if err != nil {
return err
}
s.Logger.Println("Listening on HTTPS:", listener.Addr().String())
s.ln = listener
} else {
listener, err := net.Listen("tcp", s.httpAddr)
if err != nil {
return err
}
s.Logger.Println("Listening on HTTP:", listener.Addr().String())
s.ln = listener
}
// wait for the listeners to start
timeout := time.Now().Add(raftListenerStartupTimeout)
for {
if s.ln.Addr() != nil && s.RaftListener.Addr() != nil {
break
}
if time.Now().After(timeout) {
return fmt.Errorf("unable to open without http listener running")
}
time.Sleep(10 * time.Millisecond)
}
var err error
if autoAssignPort(s.httpAddr) {
s.httpAddr, err = combineHostAndAssignedPort(s.ln, s.httpAddr)
}
if autoAssignPort(s.raftAddr) {
s.raftAddr, err = combineHostAndAssignedPort(s.RaftListener, s.raftAddr)
}
if err != nil {
return err
}
// Open the store. The addresses passed in are remotely accessible.
s.store = newStore(s.config, s.remoteAddr(s.httpAddr), s.remoteAddr(s.raftAddr))
s.store.node = s.Node
handler := newHandler(s.config, s)
handler.logger = s.Logger
handler.store = s.store
s.handler = handler
// Begin listening for requests in a separate goroutine.
go s.serve()
if err := s.store.open(s.RaftListener); err != nil {
return err
}
return nil
}
func (s *Service) remoteAddr(addr string) string {
hostname := s.config.RemoteHostname
if hostname == "" {
hostname = DefaultHostname
}
remote, err := DefaultHost(hostname, addr)
if err != nil {
return addr
}
return remote
}
// serve serves the handler from the listener.
func (s *Service) serve() {
// The listener was closed so exit
// See https://github.com/golang/go/issues/4373
err := http.Serve(s.ln, s.handler)
if err != nil && !strings.Contains(err.Error(), "closed") {
s.err <- fmt.Errorf("listener failed: addr=%s, err=%s", s.ln.Addr(), err)
}
}
// Close closes the underlying listener.
func (s *Service) Close() error {
if err := s.handler.Close(); err != nil {
return err
}
if err := s.store.close(); err != nil {
return err
}
if s.ln != nil {
if err := s.ln.Close(); err != nil {
return err
}
}
return nil
}
// HTTPAddr returns the bind address for the HTTP API
func (s *Service) HTTPAddr() string {
return s.httpAddr
}
// RaftAddr returns the bind address for the Raft TCP listener
func (s *Service) RaftAddr() string {
return s.raftAddr
}
// Err returns a channel for fatal errors that occur on the listener.
func (s *Service) Err() <-chan error { return s.err }
// SetLogger sets the internal logger to the logger passed in.
func (s *Service) SetLogger(l *log.Logger) {
s.Logger = l
}
func autoAssignPort(addr string) bool {
_, p, _ := net.SplitHostPort(addr)
return p == "0"
}
func combineHostAndAssignedPort(ln net.Listener, autoAddr string) (string, error) {
host, _, err := net.SplitHostPort(autoAddr)
if err != nil {
return "", err
}
_, port, err := net.SplitHostPort(ln.Addr().String())
if err != nil {
return "", err
}
return net.JoinHostPort(host, port), nil
}