-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.go
More file actions
191 lines (174 loc) · 4.99 KB
/
server.go
File metadata and controls
191 lines (174 loc) · 4.99 KB
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
package main
import (
"flag"
log "github.com/sirupsen/logrus"
"net"
"strconv"
"strings"
"sync"
"time"
)
const (
ACCEPT_MIN_SLEEP = 10 * time.Millisecond
// ACCEPT_MAX_SLEEP is the maximum acceptable sleep times on temporary errors
ACCEPT_MAX_SLEEP = 1 * time.Second
)
type Options struct {
Host string
TcpPort int
HttpPort int
AutoVerification bool
AutoHeartbeatResponse bool
AutoBillingModelVerify bool
AutoTransactionRecordConfirm bool
MessagingServerType string
Servers []string
Username string
Password string
MessageForwarder MessageForwarder
PublishSubjectPrefix string
}
type Server struct {
Opt *Options
Forwarder *MessageForwarder
Running bool
Mu sync.RWMutex
QuitCh chan struct{}
GrMu sync.Mutex
GrRunning bool
GrWG sync.WaitGroup
Done chan bool
Shutdown bool
}
func NewServer(opts *Options) (*Server, error) {
s := &Server{
Opt: opts,
}
return s, nil
}
func (s *Server) Start() {
o := s.Opt
var hl net.Listener
var err error
port := o.TcpPort
if port == -1 {
port = 0
}
hp := net.JoinHostPort(o.Host, strconv.Itoa(port))
s.Mu.Lock()
if s.Shutdown {
s.Mu.Unlock()
return
}
hl, err = net.Listen("tcp", hp)
if err != nil {
s.Mu.Unlock()
log.Fatalf("Unable to listen for tcp connections: %v", err)
return
}
if port == 0 {
o.TcpPort = hl.Addr().(*net.TCPAddr).Port
}
//go s.acceptConnections(hl, "YKC", func(conn net.Conn) { s.createClient(conn, nil) }, nil)
s.Mu.Unlock()
}
// Protected check on running state
func (s *Server) isRunning() bool {
s.Mu.RLock()
running := s.Running
s.Mu.RUnlock()
return running
}
// The following code is modified from nats https://github.com/nats-io/nats-server.git
func (s *Server) acceptConnections(l net.Listener, acceptName string, createFunc func(conn net.Conn), errFunc func(err error) bool) {
tmpDelay := ACCEPT_MIN_SLEEP
for {
conn, err := l.Accept()
if err != nil {
if errFunc != nil && errFunc(err) {
return
}
if tmpDelay = s.acceptError(acceptName, err, tmpDelay); tmpDelay < 0 {
break
}
continue
}
tmpDelay = ACCEPT_MIN_SLEEP
if !s.startGoRoutine(func() {
createFunc(conn)
s.GrWG.Done()
}) {
conn.Close()
}
}
log.Debugf(acceptName + " accept loop exiting..")
s.Done <- true
}
// If given error is a net.Error and is temporary, sleeps for the given
// delay and double it, but cap it to ACCEPT_MAX_SLEEP. The sleep is
// interrupted if the server is shutdown.
// An error message is displayed depending on the type of error.
// Returns the new (or unchanged) delay, or a negative value if the
// server has been or is being shutdown.
func (s *Server) acceptError(acceptName string, err error, tmpDelay time.Duration) time.Duration {
if !s.isRunning() {
return -1
}
//lint:ignore SA1019 We want to retry on a bunch of errors here.
if ne, ok := err.(net.Error); ok && ne.Temporary() { // nolint:staticcheck
log.Errorf("Temporary %s Accept Error(%v), sleeping %dms", acceptName, ne, tmpDelay/time.Millisecond)
select {
case <-time.After(tmpDelay):
case <-s.QuitCh:
return -1
}
tmpDelay *= 2
if tmpDelay > ACCEPT_MAX_SLEEP {
tmpDelay = ACCEPT_MAX_SLEEP
}
} else {
log.Errorf("%s Accept error: %v", acceptName, err)
}
return tmpDelay
}
func (s *Server) startGoRoutine(f func()) bool {
var started bool
s.GrMu.Lock()
if s.GrRunning {
s.GrWG.Add(1)
go f()
started = true
}
s.GrMu.Unlock()
return started
}
func parseOptions() *Options {
host := flag.String("host", "0.0.0.0", "host")
tcpPort := flag.Int("tcpPort", 27600, "tcpPort")
httpPort := flag.Int("httpPort", 9556, "httpPort")
autoVerification := flag.Bool("autoVerification", false, "autoVerification")
autoHeartbeatResponse := flag.Bool("autoHeartbeatResponse", true, "autoHeartbeatResponse")
autoBillingModelVerify := flag.Bool("autoBillingModelVerify", false, "autoBillingModelVerify")
autoTransactionRecordConfirm := flag.Bool("autoTransactionRecordConfirm", false, "autoTransactionRecordConfirm")
messagingServerType := flag.String("messagingServerType", "", "messagingServerType")
servers := flag.String("servers", "", "servers")
username := flag.String("username", "", "username")
password := flag.String("password", "", "password")
flag.Parse()
//splitting servers with comma
serversArr := strings.Split(*servers, ",")
opt := &Options{
Host: *host,
TcpPort: *tcpPort,
HttpPort: *httpPort,
AutoVerification: *autoVerification,
AutoHeartbeatResponse: *autoHeartbeatResponse,
AutoBillingModelVerify: *autoBillingModelVerify,
AutoTransactionRecordConfirm: *autoTransactionRecordConfirm,
MessagingServerType: *messagingServerType,
Servers: serversArr,
Username: *username,
Password: *password,
}
return opt
}