forked from nsqio/nsq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nsqlookupd.go
55 lines (48 loc) · 1.32 KB
/
nsqlookupd.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
package main
import (
"../util"
"log"
"net"
"time"
)
type NSQLookupd struct {
tcpAddr *net.TCPAddr
httpAddr *net.TCPAddr
tcpListener net.Listener
httpListener net.Listener
broadcastAddress string
waitGroup util.WaitGroupWrapper
inactiveProducerTimeout time.Duration
tombstoneLifetime time.Duration
DB *RegistrationDB
}
func NewNSQLookupd() *NSQLookupd {
return &NSQLookupd{
inactiveProducerTimeout: 300 * time.Second,
tombstoneLifetime: 45 * time.Second,
DB: NewRegistrationDB(),
}
}
func (l *NSQLookupd) Main() {
tcpListener, err := net.Listen("tcp", l.tcpAddr.String())
if err != nil {
log.Fatalf("FATAL: listen (%s) failed - %s", l.tcpAddr, err.Error())
}
l.tcpListener = tcpListener
l.waitGroup.Wrap(func() { util.TcpServer(tcpListener, &TcpProtocol{protocols: protocols}) })
httpListener, err := net.Listen("tcp", l.httpAddr.String())
if err != nil {
log.Fatalf("FATAL: listen (%s) failed - %s", l.httpAddr, err.Error())
}
l.httpListener = httpListener
l.waitGroup.Wrap(func() { httpServer(httpListener) })
}
func (l *NSQLookupd) Exit() {
if l.tcpListener != nil {
l.tcpListener.Close()
}
if l.httpListener != nil {
l.httpListener.Close()
}
l.waitGroup.Wait()
}