Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/sync: Remove duplicated call to TrackBandwidth #2694

Merged
merged 2 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions x/sync/network_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,23 +274,24 @@ func (c *networkClient) request(

var (
response []byte
responded bool
startTime = time.Now()
)

select {
case <-ctx.Done():
c.peers.TrackBandwidth(nodeID, 0)
return nil, ctx.Err()
case response = <-handler.responseChan:
elapsedSeconds := time.Since(startTime).Seconds()
bandwidth := float64(len(response))/elapsedSeconds + epsilon
c.peers.TrackBandwidth(nodeID, bandwidth)
case response, responded = <-handler.responseChan:
}
if handler.failed {
if !responded {
c.peers.TrackBandwidth(nodeID, 0)
return nil, errRequestFailed
}

elapsedSeconds := time.Since(startTime).Seconds()
bandwidth := float64(len(response))/elapsedSeconds + epsilon
c.peers.TrackBandwidth(nodeID, bandwidth)

c.log.Debug("received response from peer",
zap.Stringer("nodeID", nodeID),
zap.Uint32("requestID", requestID),
Expand Down
11 changes: 5 additions & 6 deletions x/sync/response_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,22 @@ func newResponseHandler() *responseHandler {

// Implements [ResponseHandler].
// Used to wait for a response after making a synchronous request.
// responseChan may contain response bytes if the original request has not failed.
// responseChan contains response bytes if the request succeeded.
// responseChan is closed in either fail or success scenario.
type responseHandler struct {
// If [OnResponse] is called, the response bytes are sent on this channel.
// If [OnFailure] is called, the channel is closed without sending bytes.
responseChan chan []byte
// Set to true in [OnFailure].
failed bool
}

// OnResponse passes the response bytes to the responseChan and closes the channel
// OnResponse passes the response bytes to the responseChan and closes the
// channel.
func (h *responseHandler) OnResponse(response []byte) {
h.responseChan <- response
close(h.responseChan)
}

// OnFailure sets the failed flag to true and closes the channel
// OnFailure closes the channel.
func (h *responseHandler) OnFailure() {
h.failed = true
close(h.responseChan)
}
Loading