This repository has been archived by the owner on May 1, 2020. It is now read-only.
/
server.go
114 lines (99 loc) · 2.65 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
package webwire
import (
"context"
"fmt"
"log"
"net"
"net/http"
"sync"
)
const protocolVersion = "1.4"
// server represents a headless WebWire server instance,
// where headless means there's no HTTP server that's hosting it
type server struct {
impl ServerImplementation
httpServer *http.Server
listener net.Listener
sessionManager SessionManager
sessionKeyGen SessionKeyGenerator
sessionInfoParser SessionInfoParser
// State
addr net.Addr
shutdown bool
shutdownRdy chan bool
currentOps uint32
opsLock sync.Mutex
clientsLock *sync.Mutex
clients []*Client
sessionsEnabled bool
sessionRegistry *sessionRegistry
// Internals
connUpgrader ConnUpgrader
warnLog *log.Logger
errorLog *log.Logger
}
func (srv *server) shutdownHTTPServer() error {
if srv.httpServer == nil {
return nil
}
if err := srv.httpServer.Shutdown(context.Background()); err != nil {
return fmt.Errorf("Couldn't properly shutdown HTTP server: %s", err)
}
return nil
}
// Run implements the Server interface
func (srv *server) Run() error {
// Launch HTTP server
if err := srv.httpServer.Serve(
tcpKeepAliveListener{srv.listener.(*net.TCPListener)},
); err != http.ErrServerClosed {
return fmt.Errorf("HTTP Server failure: %s", err)
}
return nil
}
// Addr implements the Server interface
func (srv *server) Addr() net.Addr {
return srv.addr
}
// Shutdown implements the Server interface
func (srv *server) Shutdown() error {
srv.opsLock.Lock()
srv.shutdown = true
// Don't block if there's no currently processed operations
if srv.currentOps < 1 {
srv.opsLock.Unlock()
return srv.shutdownHTTPServer()
}
srv.opsLock.Unlock()
<-srv.shutdownRdy
return srv.shutdownHTTPServer()
}
// ActiveSessionsNum implements the Server interface
func (srv *server) ActiveSessionsNum() int {
return srv.sessionRegistry.activeSessionsNum()
}
// SessionConnectionsNum implements the Server interface
func (srv *server) SessionConnectionsNum(sessionKey string) int {
return srv.sessionRegistry.sessionConnectionsNum(sessionKey)
}
// SessionConnections implements the Server interface
func (srv *server) SessionConnections(sessionKey string) []*Client {
agents := srv.sessionRegistry.sessionConnections(sessionKey)
if agents == nil {
return nil
}
list := make([]*Client, len(agents))
copy(list, agents)
return list
}
// CloseSession implements the Server interface
func (srv *server) CloseSession(sessionKey string) int {
agents := srv.sessionRegistry.sessionConnections(sessionKey)
if agents == nil {
return -1
}
for _, agent := range agents {
agent.Close()
}
return len(agents)
}