@@ -14,6 +14,7 @@ import (
1414 "net/url"
1515 "os"
1616 "strings"
17+ "time"
1718
1819 "github.com/cosmos/cosmos-sdk/client"
1920 cosmosflags "github.com/cosmos/cosmos-sdk/client/flags"
@@ -347,43 +348,49 @@ func runRelay(cmd *cobra.Command, args []string) error {
347348 }
348349 logger .Info ().Msgf ("✅ Endpoint URL parsed: %v" , reqUrl )
349350
351+ // Create http client
352+ backendClient := & http.Client {
353+ Timeout : 600 * time .Second ,
354+ }
355+
356+ ctxWithTimeout , cancelFn := context .WithTimeout (context .Background (), 600 * time .Second )
357+ defer cancelFn ()
358+
350359 // Send multiple requests sequentially as specified by the count flag
351360 for i := 1 ; i <= flagRelayRequestCount ; i ++ {
361+
352362 // Create the HTTP request with the relay request body
353- httpReq := & http.Request {
354- Method : http .MethodPost ,
355- URL : reqUrl ,
356- Body : io .NopCloser (bytes .NewReader (relayReqBz )),
363+ httpReq , err := http .NewRequestWithContext (
364+ ctxWithTimeout ,
365+ http .MethodPost , // This is the method to the Relay Miner node
366+ reqUrl .String (),
367+ io .NopCloser (bytes .NewReader (relayReqBz )),
368+ )
369+ if err != nil {
370+ logger .Error ().Err (err ).Msg ("❌ Error creating relay request" )
371+ continue
357372 }
358373
359374 // Send the request HTTP request containing the signed relay request
360- httpResp , err := http . DefaultClient .Do (httpReq )
375+ httpResp , err := backendClient .Do (httpReq )
361376 if err != nil {
362377 logger .Error ().Err (err ).Msg ("❌ Error sending relay request" )
363- proxy .CloseRequestBody (logger , httpResp .Body )
364- continue
365- }
366-
367- bodyCloseErr := httpResp .Body .Close ()
368- if bodyCloseErr != nil {
369- logger .Error ().Err (bodyCloseErr ).Msg ("❌ Error closing response body" )
370- proxy .CloseRequestBody (logger , httpResp .Body )
371378 continue
372379 }
373380
374381 // Ensure the supplier operator signature is present
375382 supplierSignerAddress := signedRelayReq .Meta .SupplierOperatorAddress
376383 if supplierSignerAddress == "" {
377384 logger .Error ().Msg ("❌ Supplier operator signature is missing" )
378- proxy .CloseRequestBody (logger , httpResp .Body )
385+ proxy .CloseBody (logger , httpResp .Body )
379386 continue
380387 }
381388 // Ensure the supplier operator address matches the expected address
382389 if flagRelaySupplier == "" {
383390 logger .Warn ().Msg ("⚠️ Supplier operator address not specified, skipping signature check" )
384391 } else if supplierSignerAddress != flagRelaySupplier {
385392 logger .Error ().Msgf ("❌ Supplier operator address %s does not match the expected address %s" , supplierSignerAddress , flagRelaySupplier )
386- proxy .CloseRequestBody (logger , httpResp .Body )
393+ proxy .CloseBody (logger , httpResp .Body )
387394 continue
388395 }
389396
@@ -392,18 +399,22 @@ func runRelay(cmd *cobra.Command, args []string) error {
392399 // Handle response according to type
393400 if proxy .IsStreamingResponse (httpResp ) {
394401 streamErr := processStreamRequest (ctx , httpResp , supplierSignerAddress , accountClient , logger )
395- proxy .CloseRequestBody (logger , httpResp .Body )
402+ proxy .CloseBody (logger , httpResp .Body )
396403 if streamErr != nil {
397404 logger .Error ().Err (streamErr ).Msg ("❌ Stream errored" )
398405 }
399406 } else {
400407 // Normal, non-streaming request
401408 reqErr := processNormalRequest (ctx , httpResp , supplierSignerAddress , accountClient , logger )
402- proxy .CloseRequestBody (logger , httpResp .Body )
409+ proxy .CloseBody (logger , httpResp .Body )
403410 if reqErr != nil {
404411 logger .Error ().Err (reqErr ).Msg ("❌ Request errored" )
405412 }
406413 }
414+
415+ // This is intentionally not a defer because the loop could introduce memory leaks,
416+ // performance issues and bad connection management for high flagRelayRequestCount values
417+ proxy .CloseBody (logger , httpResp .Body )
407418 }
408419
409420 return nil
0 commit comments