Skip to content

Commit 7de4c2f

Browse files
author
Antonio Cheong
committed
Fixed reassignment bug
1 parent d3a67be commit 7de4c2f

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

handlers/api.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package handlers
22

33
import (
44
"encoding/json"
5-
"fmt"
65
"time"
76

87
"github.com/ChatGPT-Hackers/ChatGPT-API-server/types"
@@ -75,6 +74,7 @@ func API_ask(c *gin.Context) {
7574
// Find connection with the lowest load and where heartbeat is after last message time
7675
connectionPool.Mu.RLock()
7776
for _, conn := range connectionPool.Connections {
77+
println(conn.Id)
7878
if connection == nil || conn.LastMessageTime.Before(connection.LastMessageTime) {
7979
if conn.Heartbeat.After(conn.LastMessageTime) {
8080
connection = conn
@@ -90,23 +90,20 @@ func API_ask(c *gin.Context) {
9090
return
9191
}
9292
// Ping before sending request
93-
if !ping(connection.Id) {
93+
println(connection.Id)
94+
var pingSucceeded bool = ping(connection.Id)
95+
if !pingSucceeded {
9496
// Ping failed. Try again
9597
connectionPool.Delete(connection.Id)
96-
connectionPool.Mu.RLock()
97-
for _, conn := range connectionPool.Connections {
98-
fmt.Println("Connection ID:", conn.Id)
99-
}
100-
connectionPool.Mu.RUnlock()
101-
10298
println("Ping failed")
99+
succeeded = false
100+
connection = nil
103101
continue
104102
} else {
105103
println("Ping succeeded")
104+
succeeded = true
105+
break
106106
}
107-
// Ping succeeded. Break the loop
108-
succeeded = true
109-
break
110107
}
111108
if !succeeded {
112109
// Delete connection

0 commit comments

Comments
 (0)