Skip to content

Commit d891753

Browse files
committed
fixed cmd_delay | updated default timout temprorary
1 parent b871086 commit d891753

File tree

3 files changed

+33
-35
lines changed

3 files changed

+33
-35
lines changed

pkg/relayer/cmd/cmd_relay.go

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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

pkg/relayer/config/relayminer_configs_reader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
// DefaultRequestTimeoutSeconds is the default timeout for requests in seconds.
1111
// If not specified in the config, it will be used as a fallback.
12-
const DefaultRequestTimeoutSeconds = 10
12+
const DefaultRequestTimeoutSeconds = 600
1313

1414
// DefaultMaxBodySize defines the default maximum HTTP body size as a string, used as a fallback if unspecified.
1515
const DefaultMaxBodySize = "20MB"

pkg/relayer/proxy/sync.go

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -300,26 +300,13 @@ func (server *relayMinerHTTPServer) serveSyncRequest(
300300
Msg("backend service returned a non-2XX status code. Passing it through to the client.")
301301
}
302302

303-
// Check if the response is a stream
304-
isStream := IsStreamingResponse(httpResponse)
305-
// Create empty relay response
306-
var relayResponse *types.RelayResponse
307-
var responseSize float64
308-
if isStream {
309-
logger.Debug().Msg("Handling streaming request.")
310-
311-
// Process and assign the relay response
312-
relayResponse, responseSize, err = server.HandleHttpStream(httpResponse, writer, meta, logger)
313-
if err != nil {
314-
return relayRequest, err
315-
}
316-
} else {
317-
logger.Debug().Msg("Handling normal request.")
318-
319303
// Check if the response is a stream
320304
streamThis := IsStreamingResponse(httpResponse)
321305
// Create empty relay response
322-
var relayResponse *types.RelayResponse
306+
relayResponse := &types.RelayResponse{
307+
Meta: types.RelayResponseMetadata{SessionHeader: meta.SessionHeader},
308+
Payload: nil,
309+
}
323310
var responseSize float64
324311
if streamThis {
325312
logger.Debug().Msg("Handling streaming request.")

0 commit comments

Comments
 (0)