Skip to content

Commit 86b3498

Browse files
committed
2025.08.08
1 parent 5d30c2e commit 86b3498

File tree

2 files changed

+61
-32
lines changed

2 files changed

+61
-32
lines changed

api/handlers.go

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,12 @@ func handleBatchValidation(batchReq BatchRequest, conn *websocket.Conn) {
227227
closedConnMutex.Unlock()
228228
}()
229229

230+
log.Printf("Processing batch with %d validations", len(batchReq.Validations))
230231
results := make([]ExampleResponse, 0, len(batchReq.Validations))
231232

232233
// Process each validation silently and only send final results
233-
for _, msg := range batchReq.Validations {
234+
for i, msg := range batchReq.Validations {
235+
log.Printf("Processing validation %d/%d: Name=%s, CID=%s", i+1, len(batchReq.Validations), msg.Name, msg.CID)
234236
// Normalize the message to handle both uppercase and lowercase field names
235237
msg.Normalize()
236238

@@ -239,11 +241,15 @@ func handleBatchValidation(batchReq BatchRequest, conn *websocket.Conn) {
239241
if salt == "" {
240242
salt, err = createRandomHash(conn)
241243
if err != nil {
242-
// On error, report as Invalid with 0 elapsed time
243-
if sendErr := sendWsResponseWithContext("Invalid", "Invalid", "0", msg.Name, msg.CID, msg.Bn, conn); sendErr != nil {
244-
// Connection lost, stop processing batch
245-
return
246-
}
244+
// On error, add Invalid result and continue
245+
results = append(results, ExampleResponse{
246+
Status: "Invalid",
247+
Message: "Invalid",
248+
Elapsed: "0",
249+
Name: msg.Name,
250+
CID: msg.CID,
251+
Bn: msg.Bn,
252+
})
247253
continue
248254
}
249255
} else {
@@ -252,11 +258,15 @@ func handleBatchValidation(batchReq BatchRequest, conn *websocket.Conn) {
252258

253259
proofJson, err := createProofRequest(salt, msg.CID, conn, msg.Name)
254260
if err != nil {
255-
// On error, report as Invalid with 0 elapsed time
256-
if sendErr := sendWsResponseWithContext("Invalid", "Invalid", "0", msg.Name, msg.CID, msg.Bn, conn); sendErr != nil {
257-
// Connection lost, stop processing batch
258-
return
259-
}
261+
// On error, add Invalid result and continue
262+
results = append(results, ExampleResponse{
263+
Status: "Invalid",
264+
Message: "Invalid",
265+
Elapsed: "0",
266+
Name: msg.Name,
267+
CID: msg.CID,
268+
Bn: msg.Bn,
269+
})
260270
continue
261271
}
262272

@@ -282,11 +292,15 @@ func handleBatchValidation(batchReq BatchRequest, conn *websocket.Conn) {
282292
}
283293

284294
if !requestSent {
285-
// If we couldn't send the request, report as Invalid
286-
if sendErr := sendWsResponseWithContext("Invalid", "Invalid", "0", msg.Name, msg.CID, msg.Bn, conn); sendErr != nil {
287-
// Connection lost, stop processing batch
288-
return
289-
}
295+
// If we couldn't send the request, add Invalid result and continue
296+
results = append(results, ExampleResponse{
297+
Status: "Invalid",
298+
Message: "Invalid",
299+
Elapsed: "0",
300+
Name: msg.Name,
301+
CID: msg.CID,
302+
Bn: msg.Bn,
303+
})
290304
continue
291305
}
292306

@@ -304,11 +318,14 @@ func handleBatchValidation(batchReq BatchRequest, conn *websocket.Conn) {
304318
case <-ctx.Done():
305319
// Timeout - report as Invalid
306320
elapsed := time.Since(startTime)
307-
if sendErr := sendWsResponseWithContext("Invalid", "Invalid", formatElapsed(elapsed), msg.Name, msg.CID, msg.Bn, conn); sendErr != nil {
308-
// Connection lost, stop processing batch
309-
cancel()
310-
return
311-
}
321+
results = append(results, ExampleResponse{
322+
Status: "Invalid",
323+
Message: "Invalid",
324+
Elapsed: formatElapsed(elapsed),
325+
Name: msg.Name,
326+
CID: msg.CID,
327+
Bn: msg.Bn,
328+
})
312329
validationComplete = true
313330
default:
314331
status := localdata.GetStatus(salt).Status
@@ -333,15 +350,10 @@ func handleBatchValidation(batchReq BatchRequest, conn *websocket.Conn) {
333350
finalStatus = "Invalid"
334351
}
335352

336-
if sendErr := sendWsResponseWithContext(finalStatus, finalStatus, formatElapsed(elapsed), msg.Name, msg.CID, msg.Bn, conn); sendErr != nil {
337-
// Connection lost, stop processing batch
338-
cancel()
339-
return
340-
}
341-
353+
// Don't send individual responses - just collect for batch
342354
results = append(results, ExampleResponse{
343-
Status: status,
344-
Message: status,
355+
Status: finalStatus,
356+
Message: finalStatus,
345357
Elapsed: formatElapsed(elapsed),
346358
Name: msg.Name,
347359
CID: msg.CID,
@@ -358,6 +370,7 @@ func handleBatchValidation(batchReq BatchRequest, conn *websocket.Conn) {
358370
}
359371

360372
// Send final batch response with all results
373+
log.Printf("Sending batch response with %d results", len(results))
361374
localdata.Lock.Lock()
362375
err := conn.WriteJSON(BatchResponse{
363376
Type: "batch",
@@ -366,6 +379,8 @@ func handleBatchValidation(batchReq BatchRequest, conn *websocket.Conn) {
366379
localdata.Lock.Unlock()
367380
if err != nil {
368381
log.Println("Error writing batch response:", err)
382+
} else {
383+
log.Printf("Batch response sent successfully with %d results", len(results))
369384
}
370385
}
371386

connection/connection.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"log"
8+
"math"
89
"os"
910
"os/signal"
1011
"proofofaccess/localdata"
@@ -118,24 +119,37 @@ func StartWsClient(name string) {
118119
interrupt := make(chan os.Signal, 1)
119120
signal.Notify(interrupt, os.Interrupt)
120121

122+
// Initial delay to allow validator to start up
123+
log.Printf("Waiting 2 seconds before connecting to validator %s", name)
124+
time.Sleep(time.Second * 2)
125+
126+
retryCount := 0
121127
for {
122128
err := connectAndListen(name, interrupt)
123129
if err != nil {
124-
log.Printf("WebSocket error: %v", err)
125-
time.Sleep(time.Second * 5) // Wait before attempting to reconnect
130+
retryCount++
131+
// Use exponential backoff with max of 30 seconds
132+
backoff := time.Duration(math.Min(float64(5 * retryCount), 30)) * time.Second
133+
log.Printf("WebSocket connection to %s failed (attempt %d): %v. Retrying in %v", name, retryCount, err, backoff)
134+
time.Sleep(backoff)
135+
} else {
136+
// Reset retry count on successful connection
137+
retryCount = 0
126138
}
127139
}
128140
}
129141

130142
func connectAndListen(name string, interrupt <-chan os.Signal) error {
131143
u := localdata.ValidatorAddress[name] + "/messaging"
132-
log.Printf("Connecting to %s", u)
144+
log.Printf("Attempting WebSocket connection to validator %s at %s", name, u)
133145

134146
c, _, err := websocket.DefaultDialer.Dial(u, nil)
135147
if err != nil {
136148
return fmt.Errorf("dial: %v", err)
137149
}
138150
defer c.Close()
151+
152+
log.Printf("Successfully connected to validator %s at %s", name, u)
139153

140154
done := make(chan struct{})
141155
go func() {

0 commit comments

Comments
 (0)