Skip to content

Commit bf63754

Browse files
author
Antonio Cheong
committed
Fix concurrent access
1 parent 9387367 commit bf63754

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313

1414
# Dependency directories (remove the comment below to include it)
1515
# vendor/
16+
Data/auth.db

handlers/api.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,21 @@ func API_ask(c *gin.Context) {
6666
})
6767
return
6868
}
69+
connectionPool.Mu.RUnlock()
6970
if request.ConversationId == "" {
7071
// Retry 3 times before giving up
72+
var succeeded bool = false
7173
for i := 0; i < 3; i++ {
7274
// Find connection with the lowest load and where heartbeat is after last message time
75+
connectionPool.Mu.RLock()
7376
for _, conn := range connectionPool.Connections {
7477
if connection == nil || conn.LastMessageTime.Before(connection.LastMessageTime) {
7578
if conn.Heartbeat.After(conn.LastMessageTime) {
7679
connection = conn
7780
}
7881
}
7982
}
83+
connectionPool.Mu.RUnlock()
8084
// Check if connection was found
8185
if connection == nil {
8286
c.JSON(503, gin.H{
@@ -87,11 +91,21 @@ func API_ask(c *gin.Context) {
8791
// Ping before sending request
8892
if !ping(connection.Id) {
8993
// Ping failed. Try again
94+
println("Ping failed")
9095
continue
96+
} else {
97+
println("Ping succeeded")
9198
}
9299
// Ping succeeded. Break the loop
100+
succeeded = true
93101
break
94102
}
103+
if !succeeded {
104+
c.JSON(503, gin.H{
105+
"error": "Ping failed",
106+
})
107+
return
108+
}
95109
} else {
96110
// Check if conversation exists
97111
conversation, ok := conversationPool.Get(request.ConversationId)
@@ -122,7 +136,6 @@ func API_ask(c *gin.Context) {
122136
return
123137
}
124138
}
125-
connectionPool.Mu.RUnlock()
126139
message := types.Message{
127140
Id: utils.GenerateId(),
128141
Message: "ChatGptRequest",
@@ -205,6 +218,7 @@ func API_getConnections(c *gin.Context) {
205218
}
206219

207220
func ping(connection_id string) bool {
221+
println("Starting ping")
208222
// Get connection
209223
connection, ok := connectionPool.Get(connection_id)
210224
// Send "ping" to the connection
@@ -214,7 +228,9 @@ func ping(connection_id string) bool {
214228
Id: id,
215229
Message: "ping",
216230
}
231+
connection.Ws.SetReadDeadline(time.Now().Add(5 * time.Second))
217232
err := connection.Ws.WriteJSON(send)
233+
println("Sent ping")
218234
if err != nil {
219235
// Delete connection
220236
connectionPool.Delete(connection_id)
@@ -224,9 +240,10 @@ func ping(connection_id string) bool {
224240
for {
225241
// Read message
226242
var receive types.Message
227-
connection.Ws.SetReadDeadline(time.Now().Add(5 * time.Second))
228243
err = connection.Ws.ReadJSON(&receive)
244+
println("Received ping response")
229245
if err != nil {
246+
println("There was an error")
230247
// Delete connection
231248
connectionPool.Delete(connection_id)
232249
return false

types/types.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,11 @@ func (p *ConnectionPool) Set(conn *Connection) {
6060
p.Connections[conn.Id] = conn
6161
}
6262

63-
func (p *ConnectionPool) Delete(id string) {
63+
func (p *ConnectionPool) Delete(id string) error {
6464
p.Mu.Lock()
6565
defer p.Mu.Unlock()
6666
delete(p.Connections, id)
67+
return nil
6768
}
6869

6970
func NewConnectionPool() *ConnectionPool {

0 commit comments

Comments
 (0)