diff --git a/server/service.go b/server/service.go index 9b3e2e4b..103321ea 100644 --- a/server/service.go +++ b/server/service.go @@ -545,13 +545,17 @@ func (m *BoostService) processCapellaPayload(w http.ResponseWriter, req *http.Re // Prepare the request context, which will be cancelled after the first successful response from a relay requestCtx, requestCtxCancel := context.WithCancel(context.Background()) defer requestCtxCancel() + resultCh := make(chan *builderApi.VersionedSubmitBlindedBlockResponse, len(m.relays)) + var received atomic.Bool + go func() { + // Make sure we receive a response within the timeout + time.Sleep(m.httpClientGetPayload.Timeout * time.Duration(m.requestMaxRetries+1)) + resultCh <- nil + }() - var wg sync.WaitGroup for _, relay := range m.relays { - wg.Add(1) go func(relay types.RelayEntry) { - defer wg.Done() url := relay.GetURI(params.PathGetPayload) log := log.WithField("url", url) log.Debug("calling getPayload") @@ -596,14 +600,16 @@ func (m *BoostService) processCapellaPayload(w http.ResponseWriter, req *http.Re // Received successful response. Try to cancel other requests and return immediately requestCtxCancel() - resultCh <- responsePayload - log.Info("received payload from relay") + if received.CompareAndSwap(false, true) { + resultCh <- responsePayload + log.Info("received payload from relay") + } else { + log.Trace("Discarding response, already received a correct response") + } }(relay) } - // Wait for all requests to complete... - wg.Wait() - close(resultCh) + // Wait for the first request to complete result := <-resultCh // If no payload has been received from relay, log loudly about withholding! @@ -731,11 +737,11 @@ func (m *BoostService) processDenebPayload(w http.ResponseWriter, req *http.Requ } requestCtxCancel() - if received.CompareAndSwap(true, true) { - log.Trace("Discarding response, already received a correct response") - } else { + if received.CompareAndSwap(false, true) { resultCh <- responsePayload log.Info("received payload from relay") + } else { + log.Trace("Discarding response, already received a correct response") } }(relay) }