Skip to content

Commit 9387367

Browse files
author
Antonio Cheong
committed
Retry 3 times
1 parent 1f3ae19 commit 9387367

File tree

1 file changed

+29
-19
lines changed

1 file changed

+29
-19
lines changed

handlers/api.go

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,30 @@ func API_ask(c *gin.Context) {
6767
return
6868
}
6969
if request.ConversationId == "" {
70-
// Find connection with the lowest load and where heartbeat is after last message time
71-
for _, conn := range connectionPool.Connections {
72-
if connection == nil || conn.LastMessageTime.Before(connection.LastMessageTime) {
73-
if conn.Heartbeat.After(conn.LastMessageTime) {
74-
connection = conn
70+
// Retry 3 times before giving up
71+
for i := 0; i < 3; i++ {
72+
// Find connection with the lowest load and where heartbeat is after last message time
73+
for _, conn := range connectionPool.Connections {
74+
if connection == nil || conn.LastMessageTime.Before(connection.LastMessageTime) {
75+
if conn.Heartbeat.After(conn.LastMessageTime) {
76+
connection = conn
77+
}
7578
}
7679
}
77-
}
78-
// Check if connection was found
79-
if connection == nil {
80-
c.JSON(503, gin.H{
81-
"error": "No available clients",
82-
})
83-
return
80+
// Check if connection was found
81+
if connection == nil {
82+
c.JSON(503, gin.H{
83+
"error": "No available clients",
84+
})
85+
return
86+
}
87+
// Ping before sending request
88+
if !ping(connection.Id) {
89+
// Ping failed. Try again
90+
continue
91+
}
92+
// Ping succeeded. Break the loop
93+
break
8494
}
8595
} else {
8696
// Check if conversation exists
@@ -104,15 +114,15 @@ func API_ask(c *gin.Context) {
104114
return
105115
}
106116
}
117+
// Ping before sending request
118+
if !ping(connection.Id) {
119+
c.JSON(503, gin.H{
120+
"error": "Ping failed",
121+
})
122+
return
123+
}
107124
}
108125
connectionPool.Mu.RUnlock()
109-
// Ping before sending request
110-
if !ping(connection.Id) {
111-
c.JSON(503, gin.H{
112-
"error": "Ping failed",
113-
})
114-
return
115-
}
116126
message := types.Message{
117127
Id: utils.GenerateId(),
118128
Message: "ChatGptRequest",

0 commit comments

Comments
 (0)