forked from coaidev/coai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.go
194 lines (157 loc) · 3.34 KB
/
connection.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
package manager
import (
"chat/globals"
"chat/manager/conversation"
"chat/utils"
"database/sql"
"fmt"
"github.com/gin-gonic/gin"
"github.com/go-redis/redis/v8"
)
const (
ChatType = "chat"
StopType = "stop"
RestartType = "restart"
ShareType = "share"
MaskType = "mask"
EditType = "edit"
RemoveType = "remove"
)
type Stack chan *conversation.FormMessage
type Connection struct {
conn *utils.WebSocket
stack Stack
auth bool
hash string
}
func NewConnection(conn *utils.WebSocket, auth bool, hash string, bufferSize int) *Connection {
return &Connection{
conn: conn,
auth: auth,
hash: hash,
stack: make(Stack, bufferSize),
}
}
func (c *Connection) GetConn() *utils.WebSocket {
return c.conn
}
func (c *Connection) GetCtx() *gin.Context {
return c.conn.GetCtx()
}
func (c *Connection) GetStack() Stack {
return c.stack
}
func (c *Connection) ReadWorker() {
for {
if c.IsClosed() {
break
}
form, err := utils.ReadForm[conversation.FormMessage](c.conn)
if err != nil {
break
}
if form.Type == "" {
form.Type = ChatType
}
c.Write(form)
}
c.Stop()
}
func (c *Connection) Write(data *conversation.FormMessage) {
if len(c.stack) == cap(c.stack) {
c.Skip()
}
c.stack <- data
}
func (c *Connection) IsClosed() bool {
return c.conn.IsClosed()
}
func (c *Connection) Stop() {
c.Write(nil)
}
func (c *Connection) Read() *conversation.FormMessage {
form := <-c.stack
return form
}
func (c *Connection) Peek() *conversation.FormMessage {
select {
case form := <-c.stack:
utils.InsertChannel(c.stack, form, 0)
return form
default:
return nil
}
}
func (c *Connection) PeekWithType(t string) *conversation.FormMessage {
// skip if type is matched
if form := c.Peek(); form != nil {
if form.Type == t {
c.Skip()
return form
}
}
return nil
}
func (c *Connection) PeekWithTypes(types ...string) *conversation.FormMessage {
// skip if type is matched
if form := c.Peek(); form != nil {
for _, t := range types {
if form.Type == t {
c.Skip()
return form
}
}
}
return nil
}
func (c *Connection) PeekStop() *conversation.FormMessage {
return c.PeekWithTypes(StopType, RemoveType)
}
func (c *Connection) Skip() {
<-c.stack
}
func (c *Connection) GetDB() *sql.DB {
return c.conn.GetDB()
}
func (c *Connection) GetCache() *redis.Client {
return c.conn.GetCache()
}
func (c *Connection) Send(message globals.ChatSegmentResponse) {
c.conn.Send(message)
}
func (c *Connection) SendClient(message globals.ChatSegmentResponse) error {
return c.conn.SendJSON(message)
}
func (c *Connection) Process(handler func(*conversation.FormMessage) error) {
for {
if form := c.Read(); form != nil {
if err := handler(form); err != nil {
return
}
} else {
return
}
}
}
func (c *Connection) Handle(handler func(*conversation.FormMessage) error) {
go c.Process(handler)
c.ReadWorker()
}
func (c *Connection) Lock() bool {
state := c.conn.IncrRateWithLimit(
c.hash,
utils.Multi[int64](c.auth, globals.ChatMaxThread, globals.AnonymousMaxThread),
60,
)
if !state {
c.conn.Send(globals.ChatSegmentResponse{
Message: fmt.Sprintf("You have reached the maximum number of threads (%d) the same time. Please wait for a while.", globals.ChatMaxThread),
End: true,
})
return false
}
return true
}
func (c *Connection) Release() {
c.conn.DecrRate(c.hash)
}