@@ -67,20 +67,30 @@ func API_ask(c *gin.Context) {
67
67
return
68
68
}
69
69
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
+ }
75
78
}
76
79
}
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
84
94
}
85
95
} else {
86
96
// Check if conversation exists
@@ -104,15 +114,15 @@ func API_ask(c *gin.Context) {
104
114
return
105
115
}
106
116
}
117
+ // Ping before sending request
118
+ if ! ping (connection .Id ) {
119
+ c .JSON (503 , gin.H {
120
+ "error" : "Ping failed" ,
121
+ })
122
+ return
123
+ }
107
124
}
108
125
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
- }
116
126
message := types.Message {
117
127
Id : utils .GenerateId (),
118
128
Message : "ChatGptRequest" ,
0 commit comments