forked from anycable/anycable-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
193 lines (151 loc) · 4.32 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
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"time"
"github.com/gorilla/websocket"
"github.com/namsral/flag"
"github.com/op/go-logging"
)
const (
// Time allowed to write a message to the peer.
writeWait = 10 * time.Second
// Send pings to peer with this period. Must be less than pongWait.
pingPeriod = 3 * time.Second
// Maximum message size allowed from peer.
maxMessageSize = 512
)
// Conn is an middleman between the websocket connection and the hub.
type Conn struct {
// The websocket connection.
ws *websocket.Conn
// Connection identifiers as received from RPC server
identifiers string
// Connection subscriptions
subscriptions map[string]bool
// Buffered channel of outbound messages.
send chan []byte
}
var version string
var log = logging.MustGetLogger("main")
var rpchost = flag.String("rpc", "0.0.0.0:50051", "rpc service address")
var redishost = flag.String("redis", "redis://localhost:6379/5", "redis address")
var redischannel = flag.String("redis_channel", "anycable", "redis channel")
var addr = flag.String("addr", "localhost:8080", "http service address")
var wspath = flag.String("wspath", "/cable", "WS endpoint path")
var disconnectRate = flag.Int("disconnect_rate", 100, "the number of Disconnect calls per second")
var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool { return true },
Subprotocols: []string{"actioncable-v1-json"},
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
// readPump pumps messages from the websocket connection to the hub.
func (c *Conn) readPump() {
defer func() {
log.Debugf("Disconnect on read error")
app.Disconnected(c)
c.ws.Close()
}()
for {
_, message, err := c.ws.ReadMessage()
if err != nil {
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
log.Debugf("read error: %v", err)
}
break
}
msg := &Message{}
if err := json.Unmarshal(message, &msg); err != nil {
log.Debugf("Unknown message: %s", message)
} else {
log.Debugf("Client message: %s", msg)
switch msg.Command {
case "subscribe":
app.Subscribe(c, msg)
case "unsubscribe":
app.Unsubscribe(c, msg)
case "message":
app.Perform(c, msg)
default:
log.Debugf("Unknown command: %s", msg.Command)
}
}
}
}
// write writes a message with the given message type and payload.
func (c *Conn) write(mt int, payload []byte) error {
c.ws.SetWriteDeadline(time.Now().Add(writeWait))
return c.ws.WriteMessage(mt, payload)
}
func (c *Conn) writePump() {
defer c.ws.Close()
for {
select {
case message, ok := <-c.send:
if !ok {
// The hub closed the channel.
c.write(websocket.CloseMessage, []byte{})
return
}
c.ws.SetWriteDeadline(time.Now().Add(writeWait))
w, err := c.ws.NextWriter(websocket.TextMessage)
if err != nil {
return
}
w.Write(message)
if err := w.Close(); err != nil {
return
}
}
}
}
// serveWs handles websocket requests from the peer.
func serveWs(w http.ResponseWriter, r *http.Request) {
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Critical(err)
return
}
response := rpc.VerifyConnection(r)
log.Debugf("Auth %s", response)
if response.Status != 1 {
log.Warningf("Auth Failed")
ws.Close()
return
}
conn := &Conn{send: make(chan []byte, 256), ws: ws, identifiers: response.Identifiers, subscriptions: make(map[string]bool)}
app.Connected(conn, response.Transmissions)
go conn.writePump()
conn.readPump()
}
func main() {
logflag := flag.Bool("log", false, "enable verbose logging")
showVersion := flag.Bool("version", false, "show version")
flag.Parse()
if *showVersion {
fmt.Println(version)
return
}
backend := logging.AddModuleLevel(logging.NewLogBackend(os.Stderr, "", 0))
if *logflag {
backend.SetLevel(logging.DEBUG, "")
} else {
backend.SetLevel(logging.INFO, "")
}
logging.SetBackend(backend)
go hub.run()
app.Pinger = NewPinger(pingPeriod)
go app.Pinger.run()
rpc.Init(*rpchost)
defer rpc.Close()
app.Subscriber = &Subscriber{host: *redishost, channel: *redischannel}
go app.Subscriber.run()
app.Disconnector = &DisconnectNotifier{rate: *disconnectRate, disconnect: make(chan *Conn)}
go app.Disconnector.run()
log.Infof("Running AnyCable websocket server v%s on %s at %s", version, *addr, *wspath)
http.HandleFunc(*wspath, serveWs)
http.ListenAndServe(*addr, nil)
}