forked from skycoin/dmsg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
225 lines (189 loc) · 5.38 KB
/
server.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package dmsg
import (
"context"
"net"
"sync"
"time"
"github.com/sirupsen/logrus"
"github.com/skycoin/skycoin/src/util/logging"
"github.com/skycoin/dmsg/cipher"
"github.com/skycoin/dmsg/disc"
"github.com/skycoin/dmsg/netutil"
"github.com/skycoin/dmsg/servermetrics"
)
// ServerConfig configues the Server
type ServerConfig struct {
MaxSessions int
UpdateInterval time.Duration
}
// DefaultServerConfig returns the default server config.
func DefaultServerConfig() *ServerConfig {
return &ServerConfig{
MaxSessions: DefaultMaxSessions,
UpdateInterval: DefaultUpdateInterval,
}
}
// Server represents a dsmg server entity.
type Server struct {
EntityCommon
m servermetrics.Metrics
ready chan struct{} // Closed once dmsg.Server is serving.
readyOnce sync.Once
done chan struct{}
once sync.Once
wg sync.WaitGroup
// Public TCP address which the dmsg server advertises itself as.
// This should only be set once. Once set, addrDone closes.
addr string
addrDone chan struct{}
maxSessions int
}
// NewServer creates a new dmsg server entity.
func NewServer(pk cipher.PubKey, sk cipher.SecKey, dc disc.APIClient, conf *ServerConfig, m servermetrics.Metrics) *Server {
if conf == nil {
conf = DefaultServerConfig()
}
if m == nil {
m = servermetrics.NewEmpty()
}
log := logging.MustGetLogger("dmsg_server")
s := new(Server)
s.EntityCommon.init(pk, sk, dc, log, conf.UpdateInterval)
s.m = m
s.ready = make(chan struct{})
s.done = make(chan struct{})
s.addrDone = make(chan struct{})
s.maxSessions = conf.MaxSessions
s.setSessionCallback = func(ctx context.Context, sessionCount int) error {
return s.updateServerEntry(ctx, s.AdvertisedAddr(), s.maxSessions)
}
s.delSessionCallback = func(ctx context.Context, sessionCount int) error {
return s.updateServerEntry(ctx, s.AdvertisedAddr(), s.maxSessions)
}
return s
}
// GetSessions returns underlying sessions map.
func (s *Server) GetSessions() map[cipher.PubKey]*SessionCommon {
s.sessionsMx.Lock()
defer s.sessionsMx.Unlock()
sessions := make(map[cipher.PubKey]*SessionCommon, len(s.sessions))
for pk, session := range s.sessions {
sessions[pk] = session
}
return sessions
}
// Close implements io.Closer
func (s *Server) Close() error {
if s == nil {
return nil
}
s.once.Do(func() {
close(s.done)
s.wg.Wait()
})
return nil
}
// Serve serves the server.
func (s *Server) Serve(lis net.Listener, addr string) error {
s.SetAdvertisedAddr(lis, &addr)
log := s.log.
WithField("advertised_addr", addr).
WithField("local_pk", s.pk)
log.Info("Serving server.")
s.wg.Add(1)
defer func() {
log.Info("Stopped server.")
s.wg.Done()
}()
ctx, cancel := context.WithCancel(context.Background())
go func() {
<-s.done
cancel()
log.WithError(lis.Close()).Info("Stopping server...")
}()
if err := s.startUpdateEntryLoop(ctx); err != nil {
return err
}
log.Info("Accepting sessions...")
s.readyOnce.Do(func() { close(s.ready) })
for {
conn, err := lis.Accept()
if err != nil {
// If server is closed, there is no error to report.
if isClosed(s.done) {
return nil
}
return err
}
// TODO(evanlinjin): Implement proper load-balancing.
if s.SessionCount() >= s.maxSessions {
s.log.
WithField("max_sessions", s.maxSessions).
WithField("remote_tcp", conn.RemoteAddr()).
Debug("Max sessions is reached, but still accepting so clients who delegated us can still listen.")
}
s.wg.Add(1)
go func(conn net.Conn) {
defer func() {
err := recover()
if err != nil {
log.Warnf("panic in handleSession: %+v", err)
}
}()
s.handleSession(conn)
s.wg.Done()
}(conn)
}
}
func (s *Server) startUpdateEntryLoop(ctx context.Context) error {
err := netutil.NewDefaultRetrier(s.log).Do(ctx, func() error {
return s.updateServerEntry(ctx, s.AdvertisedAddr(), s.maxSessions)
})
if err != nil {
return err
}
go s.updateServerEntryLoop(ctx, s.AdvertisedAddr(), s.maxSessions)
return nil
}
// AdvertisedAddr returns the TCP address in which the dmsg server is advertised by.
// This is the TCP address that should be contained within the dmsg discovery entry of this server.
func (s *Server) AdvertisedAddr() string {
<-s.addrDone
return s.addr
}
// SetAdvertisedAddr sets the advertised TCP address in which the dmsg server is advertised by.
// This should only be called once.
func (s *Server) SetAdvertisedAddr(lis net.Listener, addr *string) {
if *addr == "" {
s.log.Warn("We are using a local addr as the advertised addr. This should only be done in a local test env.")
*addr = lis.Addr().String()
}
s.addr = *addr
close(s.addrDone)
}
// Ready returns a chan which blocks until the server begins serving.
func (s *Server) Ready() <-chan struct{} {
return s.ready
}
func (s *Server) handleSession(conn net.Conn) {
log := logrus.FieldLogger(s.log.WithField("remote_tcp", conn.RemoteAddr()))
dSes, err := makeServerSession(s.m, &s.EntityCommon, conn)
if err != nil {
if err := conn.Close(); err != nil {
log.WithError(err).Debug("On handleSession() failure, close connection resulted in error.")
}
return
}
log = log.WithField("remote_pk", dSes.RemotePK())
log.Info("Started session.")
ctx, cancel := context.WithCancel(context.Background())
go func() {
awaitDone(ctx, s.done)
log.WithError(dSes.Close()).Info("Stopped session.")
}()
if s.setSession(ctx, dSes.SessionCommon) {
dSes.Serve()
}
s.delSession(ctx, dSes.RemotePK())
cancel()
}