forked from mmkhitaryan/drawio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
241 lines (212 loc) · 4.53 KB
/
main.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package main
import (
"container/ring"
"context"
"encoding/json"
"fmt"
"github.com/gorilla/websocket"
"io"
"io/ioutil"
"log"
"net/http"
"sync"
"sync/atomic"
"time"
)
const quota = 1800
var clients = &sync.Map{}
var online int64
var broadcast = make(chan *data, 100000)
var circularBuf = &safeCircularBuffer{mu: &sync.RWMutex{}, rbuf: ring.New(1000)}
type safeCircularBuffer struct {
mu *sync.RWMutex
rbuf *ring.Ring
}
//data
type data struct {
mType int
force bool
message []byte
from *websocket.Conn
}
//socket
type socket struct {
conn *websocket.Conn
message chan data
quotum int64
quotumLock uint32
}
//TODO временый костыйль с origin
var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
}}
//handleMessages
func handleMessages() {
defer func() {
if err := recover(); err != nil {
log.Println(err)
}
go handleMessages()
}()
for msg := range broadcast {
clients.Range(func(key, value interface{}) bool {
key.(chan data) <- *msg
return true
})
}
}
//writer
func (s *socket) writer(ctx context.Context) {
timer := time.NewTicker(time.Second * 3)
for {
select {
case <-ctx.Done():
return
case <-timer.C:
if s.quotumIsLock() {
i := atomic.AddInt64(&s.quotum, -600)
s.sendQuotum(i, 1)
if i <= 0 {
s.unlockQuotum()
}
}
case msg := <-s.message:
if msg.from != s.conn || msg.force {
writer, err := s.conn.NextWriter(msg.mType)
if err != nil {
s.log(err)
return
}
if _, err := writer.Write(msg.message); err != nil {
s.log(err)
return
}
if err = writer.Close(); err != nil {
s.log(err)
return
}
}
}
}
}
//socketPayload
type socketPayload struct {
ID int `json:"id"`
Data struct {
Points [][]int `json:"points,omitempty"`
} `json:"data,omitempty"`
Count int64 `json:"count,omitempty"`
}
//reader
func (s *socket) reader() {
ctx, cancel := context.WithCancel(context.Background())
atomic.AddInt64(&online, 1)
defer func() {
cancel()
atomic.AddInt64(&online, -1)
clients.Delete(s.message)
close(s.message)
s.conn.Close()
}()
s.sendHistory()
clients.Store(s.message, struct{}{})
go s.writer(ctx)
for {
mt, reader, err := s.conn.NextReader()
if err != nil {
s.log(err)
return
}
buf, err := ioutil.ReadAll(reader)
if err != nil && err != io.ErrUnexpectedEOF {
s.log(err)
return
}
var sp socketPayload
if err := json.Unmarshal(buf, &sp); err != nil {
s.log(err)
}
if countQuota, ok := s.quotumAllow(len(sp.Data.Points)); !ok {
s.sendQuotum(countQuota, mt)
continue
}
broadcast <- &data{from: s.conn, mType: mt, force: false, message: buf}
circularBuf.mu.Lock()
circularBuf.rbuf.Value = buf
circularBuf.rbuf = circularBuf.rbuf.Next()
circularBuf.mu.Unlock()
}
}
func (s *socket) sendHistory() {
circularBuf.mu.RLock()
defer circularBuf.mu.RUnlock()
circularBuf.rbuf.Do(func(val interface{}) {
if msg, ok := val.([]byte); ok {
s.message <- data{from: s.conn, mType: 1, force: true, message: msg}
}
})
}
//quotumAllow
func (s *socket) quotumAllow(count int) (int64, bool) {
if s.quotumIsLock() {
return atomic.LoadInt64(&s.quotum), false
}
i := atomic.LoadInt64(&s.quotum)
if i >= quota {
s.lockQuotum()
return i, false
}
i = atomic.AddInt64(&s.quotum, int64(count))
return i, true
}
func (s *socket) lockQuotum() {
if s.quotumIsLock() {
return
}
atomic.StoreUint32(&s.quotumLock, 1)
}
func (s *socket) unlockQuotum() {
if s.quotumIsLock() {
atomic.StoreUint32(&s.quotumLock, 0)
}
}
func (s *socket) quotumIsLock() bool {
return atomic.LoadUint32(&s.quotumLock) == 1
}
//sendQuotum
func (s *socket) sendQuotum(quotum int64, mt int) {
sp := &socketPayload{
ID: 2,
Count: quotum,
}
if msg, err := json.Marshal(sp); err == nil {
s.message <- data{from: s.conn, mType: mt, force: true, message: msg}
}
}
func (s *socket) log(err error) {
log.Println(s.conn.RemoteAddr().String(), err)
}
//upgrade
func upgrade(w http.ResponseWriter, r *http.Request) {
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Print("upgrade:", err)
return
}
s := &socket{conn: ws, message: make(chan data, 100000)}
go s.reader()
}
//online
func onlineHandle(w http.ResponseWriter, r *http.Request) {
total := atomic.LoadInt64(&online)
fmt.Fprint(w, total)
}
func main() {
fs := http.FileServer(http.Dir("./web"))
http.Handle("/", fs)
go handleMessages()
http.HandleFunc("/online", onlineHandle)
http.HandleFunc("/ws", upgrade)
log.Fatal(http.ListenAndServe(":80", nil))
}