Skip to content

Commit

Permalink
server: removed lock, added second check to prevent multiple log outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
MariusVanDerWijden committed Jul 13, 2024
1 parent 882e9c4 commit ebb5790
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions server/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,11 @@ func (m *BoostService) handleRegisterValidator(w http.ResponseWriter, req *http.
log := log.WithField("url", url)

_, err := SendHTTPRequest(context.Background(), m.httpClientRegVal, http.MethodPost, url, ua, headers, payload, nil)
relayRespCh <- err
if err != nil {
log.WithError(err).Warn("error calling registerValidator on relay")
return
}
relayRespCh <- err

}(relay)

Check failure on line 278 in server/service.go

View workflow job for this annotation

GitHub Actions / Lint

unnecessary trailing newline (whitespace)
}

Expand Down Expand Up @@ -547,13 +547,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")
Expand Down Expand Up @@ -598,14 +602,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!
Expand Down Expand Up @@ -667,17 +673,20 @@ func (m *BoostService) processDenebPayload(w http.ResponseWriter, req *http.Requ
}

// Prepare for requests
var wg sync.WaitGroup
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)
resultCh <- nil
}()

// Prepare the request context, which will be cancelled after the first successful response from a relay
requestCtx, requestCtxCancel := context.WithCancel(context.Background())
defer requestCtxCancel()

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")
Expand Down Expand Up @@ -733,14 +742,16 @@ func (m *BoostService) processDenebPayload(w http.ResponseWriter, req *http.Requ
}

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!
Expand Down

0 comments on commit ebb5790

Please sign in to comment.