github.com/MetalBlockchain/metalgo@v1.11.9/snow/consensus/snowman/bootstrapper/requests.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package bootstrapper
     5  
     6  import (
     7  	"context"
     8  
     9  	"github.com/MetalBlockchain/metalgo/ids"
    10  	"github.com/MetalBlockchain/metalgo/utils/set"
    11  )
    12  
    13  type requests struct {
    14  	maxOutstanding int
    15  
    16  	pendingSend set.Set[ids.NodeID]
    17  	outstanding set.Set[ids.NodeID]
    18  }
    19  
    20  func (r *requests) GetPeers(context.Context) set.Set[ids.NodeID] {
    21  	numPending := r.outstanding.Len()
    22  	if numPending >= r.maxOutstanding {
    23  		return nil
    24  	}
    25  
    26  	numToSend := min(
    27  		r.maxOutstanding-numPending,
    28  		r.pendingSend.Len(),
    29  	)
    30  	nodeIDs := set.NewSet[ids.NodeID](numToSend)
    31  	for i := 0; i < numToSend; i++ {
    32  		nodeID, _ := r.pendingSend.Pop()
    33  		nodeIDs.Add(nodeID)
    34  	}
    35  	r.outstanding.Union(nodeIDs)
    36  	return nodeIDs
    37  }
    38  
    39  func (r *requests) recordResponse(nodeID ids.NodeID) bool {
    40  	wasOutstanding := r.outstanding.Contains(nodeID)
    41  	r.outstanding.Remove(nodeID)
    42  	return wasOutstanding
    43  }
    44  
    45  func (r *requests) finished() bool {
    46  	return r.pendingSend.Len() == 0 && r.outstanding.Len() == 0
    47  }