github.com/bcskill/bcschain/v3@v3.4.9-beta2/eth/downloader/peer.go (about)

     1  // Copyright 2015 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  // Contains the active peer-set of the downloader, maintaining both failures
    18  // as well as reputation metrics to prioritize the block retrievals.
    19  
    20  package downloader
    21  
    22  import (
    23  	"errors"
    24  	"fmt"
    25  	"math"
    26  	"math/big"
    27  	"sort"
    28  	"sync"
    29  	"sync/atomic"
    30  	"time"
    31  
    32  	"github.com/bcskill/bcschain/v3/common"
    33  	"github.com/bcskill/bcschain/v3/log"
    34  )
    35  
    36  const (
    37  	maxLackingHashes  = 4096 // Maximum number of entries allowed on the list or lacking items
    38  	measurementImpact = 0.1  // The impact a single measurement has on a peer's final throughput value.
    39  )
    40  
    41  var (
    42  	errAlreadyFetching   = errors.New("already fetching blocks from peer")
    43  	errAlreadyRegistered = errors.New("peer is already registered")
    44  	errNotRegistered     = errors.New("peer is not registered")
    45  )
    46  
    47  // peerConnection represents an active peer from which hashes and blocks are retrieved.
    48  type peerConnection struct {
    49  	id string // Unique identifier of the peer
    50  
    51  	headerIdle  int32 // Current header activity state of the peer (idle = 0, active = 1)
    52  	blockIdle   int32 // Current block activity state of the peer (idle = 0, active = 1)
    53  	receiptIdle int32 // Current receipt activity state of the peer (idle = 0, active = 1)
    54  	stateIdle   int32 // Current node data activity state of the peer (idle = 0, active = 1)
    55  
    56  	headerThroughput  float64 // Number of headers measured to be retrievable per second
    57  	blockThroughput   float64 // Number of blocks (bodies) measured to be retrievable per second
    58  	receiptThroughput float64 // Number of receipts measured to be retrievable per second
    59  	stateThroughput   float64 // Number of node data pieces measured to be retrievable per second
    60  
    61  	rtt time.Duration // Request round trip time to track responsiveness (QoS)
    62  
    63  	headerStarted  time.Time // Time instance when the last header fetch was started
    64  	blockStarted   time.Time // Time instance when the last block (body) fetch was started
    65  	receiptStarted time.Time // Time instance when the last receipt fetch was started
    66  	stateStarted   time.Time // Time instance when the last node data fetch was started
    67  
    68  	lacking map[common.Hash]struct{} // Set of hashes not to request (didn't have previously)
    69  
    70  	peer Peer
    71  
    72  	version int        // Eth protocol version number to switch strategies
    73  	log     log.Logger // Contextual logger to add extra infos to peer logs
    74  	lock    sync.RWMutex
    75  }
    76  
    77  // LightPeer encapsulates the methods required to synchronise with a remote light peer.
    78  type LightPeer interface {
    79  	Head() (common.Hash, *big.Int)
    80  	RequestHeadersByHash(common.Hash, int, int, bool) error
    81  	RequestHeadersByNumber(uint64, int, int, bool) error
    82  }
    83  
    84  // Peer encapsulates the methods required to synchronise with a remote full peer.
    85  type Peer interface {
    86  	LightPeer
    87  	RequestBodies([]common.Hash) error
    88  	RequestReceipts([]common.Hash) error
    89  	RequestNodeData([]common.Hash) error
    90  }
    91  
    92  // lightPeerWrapper wraps a LightPeer struct, stubbing out the Peer-only methods.
    93  type lightPeerWrapper struct {
    94  	peer LightPeer
    95  }
    96  
    97  func (w *lightPeerWrapper) Head() (common.Hash, *big.Int) { return w.peer.Head() }
    98  func (w *lightPeerWrapper) RequestHeadersByHash(h common.Hash, amount int, skip int, reverse bool) error {
    99  	return w.peer.RequestHeadersByHash(h, amount, skip, reverse)
   100  }
   101  func (w *lightPeerWrapper) RequestHeadersByNumber(i uint64, amount int, skip int, reverse bool) error {
   102  	return w.peer.RequestHeadersByNumber(i, amount, skip, reverse)
   103  }
   104  func (w *lightPeerWrapper) RequestBodies([]common.Hash) error {
   105  	panic("RequestBodies not supported in light client mode sync")
   106  }
   107  func (w *lightPeerWrapper) RequestReceipts([]common.Hash) error {
   108  	panic("RequestReceipts not supported in light client mode sync")
   109  }
   110  func (w *lightPeerWrapper) RequestNodeData([]common.Hash) error {
   111  	panic("RequestNodeData not supported in light client mode sync")
   112  }
   113  
   114  // newPeerConnection creates a new downloader peer.
   115  func newPeerConnection(id string, version int, peer Peer, logger log.Logger) *peerConnection {
   116  	return &peerConnection{
   117  		id:      id,
   118  		lacking: make(map[common.Hash]struct{}),
   119  
   120  		peer: peer,
   121  
   122  		version: version,
   123  		log:     logger,
   124  	}
   125  }
   126  
   127  // Reset clears the internal state of a peer entity.
   128  func (p *peerConnection) Reset() {
   129  	p.lock.Lock()
   130  	defer p.lock.Unlock()
   131  
   132  	atomic.StoreInt32(&p.headerIdle, 0)
   133  	atomic.StoreInt32(&p.blockIdle, 0)
   134  	atomic.StoreInt32(&p.receiptIdle, 0)
   135  	atomic.StoreInt32(&p.stateIdle, 0)
   136  
   137  	p.headerThroughput = 0
   138  	p.blockThroughput = 0
   139  	p.receiptThroughput = 0
   140  	p.stateThroughput = 0
   141  
   142  	p.lacking = make(map[common.Hash]struct{})
   143  }
   144  
   145  // FetchHeaders sends a header retrieval request to the remote peer.
   146  func (p *peerConnection) FetchHeaders(from uint64, count int) error {
   147  	// Sanity check the protocol version
   148  	if p.version < 62 {
   149  		panic(fmt.Sprintf("header fetch [eth/62+] requested on eth/%d", p.version))
   150  	}
   151  	// Short circuit if the peer is already fetching
   152  	if !atomic.CompareAndSwapInt32(&p.headerIdle, 0, 1) {
   153  		return errAlreadyFetching
   154  	}
   155  	p.headerStarted = time.Now()
   156  
   157  	// Issue the header retrieval request (absolut upwards without gaps)
   158  	go p.peer.RequestHeadersByNumber(from, count, 0, false)
   159  
   160  	return nil
   161  }
   162  
   163  // FetchBodies sends a block body retrieval request to the remote peer.
   164  func (p *peerConnection) FetchBodies(request *fetchRequest) error {
   165  	// Sanity check the protocol version
   166  	if p.version < 62 {
   167  		panic(fmt.Sprintf("body fetch [eth/62+] requested on eth/%d", p.version))
   168  	}
   169  	// Short circuit if the peer is already fetching
   170  	if !atomic.CompareAndSwapInt32(&p.blockIdle, 0, 1) {
   171  		return errAlreadyFetching
   172  	}
   173  	p.blockStarted = time.Now()
   174  
   175  	// Convert the header set to a retrievable slice
   176  	hashes := make([]common.Hash, 0, len(request.Headers))
   177  	for _, header := range request.Headers {
   178  		hashes = append(hashes, header.Hash())
   179  	}
   180  	go p.peer.RequestBodies(hashes)
   181  
   182  	return nil
   183  }
   184  
   185  // FetchReceipts sends a receipt retrieval request to the remote peer.
   186  func (p *peerConnection) FetchReceipts(request *fetchRequest) error {
   187  	// Sanity check the protocol version
   188  	if p.version < 63 {
   189  		panic(fmt.Sprintf("body fetch [eth/63+] requested on eth/%d", p.version))
   190  	}
   191  	// Short circuit if the peer is already fetching
   192  	if !atomic.CompareAndSwapInt32(&p.receiptIdle, 0, 1) {
   193  		return errAlreadyFetching
   194  	}
   195  	p.receiptStarted = time.Now()
   196  
   197  	// Convert the header set to a retrievable slice
   198  	hashes := make([]common.Hash, 0, len(request.Headers))
   199  	for _, header := range request.Headers {
   200  		hashes = append(hashes, header.Hash())
   201  	}
   202  	go p.peer.RequestReceipts(hashes)
   203  
   204  	return nil
   205  }
   206  
   207  // FetchNodeData sends a node state data retrieval request to the remote peer.
   208  func (p *peerConnection) FetchNodeData(hashes []common.Hash) error {
   209  	// Sanity check the protocol version
   210  	if p.version < 63 {
   211  		panic(fmt.Sprintf("node data fetch [eth/63+] requested on eth/%d", p.version))
   212  	}
   213  	// Short circuit if the peer is already fetching
   214  	if !atomic.CompareAndSwapInt32(&p.stateIdle, 0, 1) {
   215  		return errAlreadyFetching
   216  	}
   217  	p.stateStarted = time.Now()
   218  
   219  	go p.peer.RequestNodeData(hashes)
   220  
   221  	return nil
   222  }
   223  
   224  // SetHeadersIdle sets the peer to idle, allowing it to execute new header retrieval
   225  // requests. Its estimated header retrieval throughput is updated with that measured
   226  // just now.
   227  func (p *peerConnection) SetHeadersIdle(delivered int) {
   228  	p.setIdle(p.headerStarted, delivered, &p.headerThroughput, &p.headerIdle)
   229  }
   230  
   231  // SetBlocksIdle sets the peer to idle, allowing it to execute new block retrieval
   232  // requests. Its estimated block retrieval throughput is updated with that measured
   233  // just now.
   234  func (p *peerConnection) SetBlocksIdle(delivered int) {
   235  	p.setIdle(p.blockStarted, delivered, &p.blockThroughput, &p.blockIdle)
   236  }
   237  
   238  // SetBodiesIdle sets the peer to idle, allowing it to execute block body retrieval
   239  // requests. Its estimated body retrieval throughput is updated with that measured
   240  // just now.
   241  func (p *peerConnection) SetBodiesIdle(delivered int) {
   242  	p.setIdle(p.blockStarted, delivered, &p.blockThroughput, &p.blockIdle)
   243  }
   244  
   245  // SetReceiptsIdle sets the peer to idle, allowing it to execute new receipt
   246  // retrieval requests. Its estimated receipt retrieval throughput is updated
   247  // with that measured just now.
   248  func (p *peerConnection) SetReceiptsIdle(delivered int) {
   249  	p.setIdle(p.receiptStarted, delivered, &p.receiptThroughput, &p.receiptIdle)
   250  }
   251  
   252  // SetNodeDataIdle sets the peer to idle, allowing it to execute new state trie
   253  // data retrieval requests. Its estimated state retrieval throughput is updated
   254  // with that measured just now.
   255  func (p *peerConnection) SetNodeDataIdle(delivered int) {
   256  	p.setIdle(p.stateStarted, delivered, &p.stateThroughput, &p.stateIdle)
   257  }
   258  
   259  // setIdle sets the peer to idle, allowing it to execute new retrieval requests.
   260  // Its estimated retrieval throughput is updated with that measured just now.
   261  func (p *peerConnection) setIdle(started time.Time, delivered int, throughput *float64, idle *int32) {
   262  	// Irrelevant of the scaling, make sure the peer ends up idle
   263  	defer atomic.StoreInt32(idle, 0)
   264  
   265  	p.lock.Lock()
   266  	defer p.lock.Unlock()
   267  
   268  	// If nothing was delivered (hard timeout / unavailable data), reduce throughput to minimum
   269  	if delivered == 0 {
   270  		*throughput = 0
   271  		return
   272  	}
   273  	// Otherwise update the throughput with a new measurement
   274  	elapsed := time.Since(started) + 1 // +1 (ns) to ensure non-zero divisor
   275  	measured := float64(delivered) / (float64(elapsed) / float64(time.Second))
   276  
   277  	*throughput = (1-measurementImpact)*(*throughput) + measurementImpact*measured
   278  	p.rtt = time.Duration((1-measurementImpact)*float64(p.rtt) + measurementImpact*float64(elapsed))
   279  
   280  	p.log.Trace("Peer throughput measurements updated",
   281  		"hps", p.headerThroughput, "bps", p.blockThroughput,
   282  		"rps", p.receiptThroughput, "sps", p.stateThroughput,
   283  		"miss", len(p.lacking), "rtt", p.rtt)
   284  }
   285  
   286  // HeaderCapacity retrieves the peers header download allowance based on its
   287  // previously discovered throughput.
   288  func (p *peerConnection) HeaderCapacity(targetRTT time.Duration) int {
   289  	p.lock.RLock()
   290  	defer p.lock.RUnlock()
   291  
   292  	return int(math.Min(1+math.Max(1, p.headerThroughput*float64(targetRTT)/float64(time.Second)), float64(MaxHeaderFetch)))
   293  }
   294  
   295  // BlockCapacity retrieves the peers block download allowance based on its
   296  // previously discovered throughput.
   297  func (p *peerConnection) BlockCapacity(targetRTT time.Duration) int {
   298  	p.lock.RLock()
   299  	defer p.lock.RUnlock()
   300  
   301  	return int(math.Min(1+math.Max(1, p.blockThroughput*float64(targetRTT)/float64(time.Second)), float64(MaxBlockFetch)))
   302  }
   303  
   304  // ReceiptCapacity retrieves the peers receipt download allowance based on its
   305  // previously discovered throughput.
   306  func (p *peerConnection) ReceiptCapacity(targetRTT time.Duration) int {
   307  	p.lock.RLock()
   308  	defer p.lock.RUnlock()
   309  
   310  	return int(math.Min(1+math.Max(1, p.receiptThroughput*float64(targetRTT)/float64(time.Second)), float64(MaxReceiptFetch)))
   311  }
   312  
   313  // NodeDataCapacity retrieves the peers state download allowance based on its
   314  // previously discovered throughput.
   315  func (p *peerConnection) NodeDataCapacity(targetRTT time.Duration) int {
   316  	p.lock.RLock()
   317  	defer p.lock.RUnlock()
   318  
   319  	return int(math.Min(1+math.Max(1, p.stateThroughput*float64(targetRTT)/float64(time.Second)), float64(MaxStateFetch)))
   320  }
   321  
   322  // MarkLacking appends a new entity to the set of items (blocks, receipts, states)
   323  // that a peer is known not to have (i.e. have been requested before). If the
   324  // set reaches its maximum allowed capacity, items are randomly dropped off.
   325  func (p *peerConnection) MarkLacking(hash common.Hash) {
   326  	p.lock.Lock()
   327  	defer p.lock.Unlock()
   328  
   329  	for len(p.lacking) >= maxLackingHashes {
   330  		for drop := range p.lacking {
   331  			delete(p.lacking, drop)
   332  			break
   333  		}
   334  	}
   335  	p.lacking[hash] = struct{}{}
   336  }
   337  
   338  // Lacks retrieves whether the hash of a blockchain item is on the peers lacking
   339  // list (i.e. whether we know that the peer does not have it).
   340  func (p *peerConnection) Lacks(hash common.Hash) bool {
   341  	p.lock.RLock()
   342  	defer p.lock.RUnlock()
   343  
   344  	_, ok := p.lacking[hash]
   345  	return ok
   346  }
   347  
   348  // peerSet represents the collection of active peer participating in the chain
   349  // download procedure.
   350  type peerSet struct {
   351  	peers        map[string]*peerConnection
   352  	newPeerFeed  peerConnectionFeed
   353  	peerDropFeed peerConnectionFeed
   354  	lock         sync.RWMutex
   355  }
   356  
   357  // newPeerSet creates a new peer set top track the active download sources.
   358  func newPeerSet() *peerSet {
   359  	return &peerSet{
   360  		peers: make(map[string]*peerConnection),
   361  	}
   362  }
   363  
   364  // SubscribeNewPeers subscribes to peer arrival events.
   365  func (ps *peerSet) SubscribeNewPeers(ch chan<- *peerConnection, name string) {
   366  	ps.newPeerFeed.Subscribe(ch, name)
   367  }
   368  
   369  func (ps *peerSet) UnsubscribeNewPeers(ch chan<- *peerConnection) {
   370  	ps.newPeerFeed.Unsubscribe(ch)
   371  }
   372  
   373  // SubscribePeerDrops subscribes to peer departure events.
   374  func (ps *peerSet) SubscribePeerDrops(ch chan<- *peerConnection, name string) {
   375  	ps.peerDropFeed.Subscribe(ch, name)
   376  }
   377  
   378  func (ps *peerSet) UnsubscribePeerDrops(ch chan<- *peerConnection) {
   379  	ps.peerDropFeed.Unsubscribe(ch)
   380  }
   381  
   382  // Reset iterates over the current peer set, and resets each of the known peers
   383  // to prepare for a next batch of block retrieval.
   384  func (ps *peerSet) Reset() {
   385  	ps.lock.RLock()
   386  	defer ps.lock.RUnlock()
   387  
   388  	for _, peer := range ps.peers {
   389  		peer.Reset()
   390  	}
   391  }
   392  
   393  // Register injects a new peer into the working set, or returns an error if the
   394  // peer is already known.
   395  //
   396  // The method also sets the starting throughput values of the new peer to the
   397  // average of all existing peers, to give it a realistic chance of being used
   398  // for data retrievals.
   399  func (ps *peerSet) Register(p *peerConnection) error {
   400  	// Retrieve the current median RTT as a sane default
   401  	p.rtt = ps.medianRTT()
   402  
   403  	// Register the new peer with some meaningful defaults
   404  	ps.lock.Lock()
   405  	if _, ok := ps.peers[p.id]; ok {
   406  		ps.lock.Unlock()
   407  		return errAlreadyRegistered
   408  	}
   409  	if len(ps.peers) > 0 {
   410  		p.headerThroughput, p.blockThroughput, p.receiptThroughput, p.stateThroughput = 0, 0, 0, 0
   411  
   412  		for _, peer := range ps.peers {
   413  			peer.lock.RLock()
   414  			p.headerThroughput += peer.headerThroughput
   415  			p.blockThroughput += peer.blockThroughput
   416  			p.receiptThroughput += peer.receiptThroughput
   417  			p.stateThroughput += peer.stateThroughput
   418  			peer.lock.RUnlock()
   419  		}
   420  		p.headerThroughput /= float64(len(ps.peers))
   421  		p.blockThroughput /= float64(len(ps.peers))
   422  		p.receiptThroughput /= float64(len(ps.peers))
   423  		p.stateThroughput /= float64(len(ps.peers))
   424  	}
   425  	ps.peers[p.id] = p
   426  	ps.lock.Unlock()
   427  
   428  	ps.newPeerFeed.Send(p)
   429  	return nil
   430  }
   431  
   432  // Unregister removes a remote peer from the active set, disabling any further
   433  // actions to/from that particular entity.
   434  func (ps *peerSet) Unregister(id string) error {
   435  	ps.lock.Lock()
   436  	p, ok := ps.peers[id]
   437  	if !ok {
   438  		defer ps.lock.Unlock()
   439  		return errNotRegistered
   440  	}
   441  	delete(ps.peers, id)
   442  	ps.lock.Unlock()
   443  
   444  	ps.peerDropFeed.Send(p)
   445  	return nil
   446  }
   447  
   448  // Peer retrieves the registered peer with the given id.
   449  func (ps *peerSet) Peer(id string) *peerConnection {
   450  	ps.lock.RLock()
   451  	defer ps.lock.RUnlock()
   452  
   453  	return ps.peers[id]
   454  }
   455  
   456  // Len returns if the current number of peers in the set.
   457  func (ps *peerSet) Len() int {
   458  	ps.lock.RLock()
   459  	defer ps.lock.RUnlock()
   460  
   461  	return len(ps.peers)
   462  }
   463  
   464  // AllPeers retrieves a flat list of all the peers within the set.
   465  func (ps *peerSet) AllPeers() []*peerConnection {
   466  	ps.lock.RLock()
   467  	defer ps.lock.RUnlock()
   468  
   469  	list := make([]*peerConnection, 0, len(ps.peers))
   470  	for _, p := range ps.peers {
   471  		list = append(list, p)
   472  	}
   473  	return list
   474  }
   475  
   476  // HeaderIdlePeers retrieves a flat list of all the currently header-idle peers
   477  // within the active peer set, ordered by their reputation.
   478  func (ps *peerSet) HeaderIdlePeers() ([]*peerConnection, int) {
   479  	idle := func(p *peerConnection) bool {
   480  		return atomic.LoadInt32(&p.headerIdle) == 0
   481  	}
   482  	throughput := func(p *peerConnection) float64 {
   483  		p.lock.RLock()
   484  		defer p.lock.RUnlock()
   485  		return p.headerThroughput
   486  	}
   487  	return ps.idlePeers(62, 64, idle, throughput)
   488  }
   489  
   490  // BodyIdlePeers retrieves a flat list of all the currently body-idle peers within
   491  // the active peer set, ordered by their reputation.
   492  func (ps *peerSet) BodyIdlePeers() ([]*peerConnection, int) {
   493  	idle := func(p *peerConnection) bool {
   494  		return atomic.LoadInt32(&p.blockIdle) == 0
   495  	}
   496  	throughput := func(p *peerConnection) float64 {
   497  		p.lock.RLock()
   498  		defer p.lock.RUnlock()
   499  		return p.blockThroughput
   500  	}
   501  	return ps.idlePeers(62, 64, idle, throughput)
   502  }
   503  
   504  // ReceiptIdlePeers retrieves a flat list of all the currently receipt-idle peers
   505  // within the active peer set, ordered by their reputation.
   506  func (ps *peerSet) ReceiptIdlePeers() ([]*peerConnection, int) {
   507  	idle := func(p *peerConnection) bool {
   508  		return atomic.LoadInt32(&p.receiptIdle) == 0
   509  	}
   510  	throughput := func(p *peerConnection) float64 {
   511  		p.lock.RLock()
   512  		defer p.lock.RUnlock()
   513  		return p.receiptThroughput
   514  	}
   515  	return ps.idlePeers(63, 64, idle, throughput)
   516  }
   517  
   518  // NodeDataIdlePeers retrieves a flat list of all the currently node-data-idle
   519  // peers within the active peer set, ordered by their reputation.
   520  func (ps *peerSet) NodeDataIdlePeers() ([]*peerConnection, int) {
   521  	idle := func(p *peerConnection) bool {
   522  		return atomic.LoadInt32(&p.stateIdle) == 0
   523  	}
   524  	throughput := func(p *peerConnection) float64 {
   525  		p.lock.RLock()
   526  		defer p.lock.RUnlock()
   527  		return p.stateThroughput
   528  	}
   529  	return ps.idlePeers(63, 64, idle, throughput)
   530  }
   531  
   532  // idlePeers retrieves a flat list of all currently idle peers satisfying the
   533  // protocol version constraints, using the provided function to check idleness.
   534  // The resulting set of peers are sorted by their measure throughput.
   535  func (ps *peerSet) idlePeers(minProtocol, maxProtocol int, idleCheck func(*peerConnection) bool, throughput func(*peerConnection) float64) ([]*peerConnection, int) {
   536  	ps.lock.RLock()
   537  	defer ps.lock.RUnlock()
   538  
   539  	idle, total := make([]*peerConnection, 0, len(ps.peers)), 0
   540  	for _, p := range ps.peers {
   541  		if p.version >= minProtocol && p.version <= maxProtocol {
   542  			if idleCheck(p) {
   543  				idle = append(idle, p)
   544  			}
   545  			total++
   546  		}
   547  	}
   548  	for i := 0; i < len(idle); i++ {
   549  		for j := i + 1; j < len(idle); j++ {
   550  			if throughput(idle[i]) < throughput(idle[j]) {
   551  				idle[i], idle[j] = idle[j], idle[i]
   552  			}
   553  		}
   554  	}
   555  	return idle, total
   556  }
   557  
   558  // medianRTT returns the median RTT of the peerset, considering only the tuning
   559  // peers if there are more peers available.
   560  func (ps *peerSet) medianRTT() time.Duration {
   561  	// Gather all the currently measured round trip times
   562  	ps.lock.RLock()
   563  	defer ps.lock.RUnlock()
   564  
   565  	rtts := make([]float64, 0, len(ps.peers))
   566  	for _, p := range ps.peers {
   567  		p.lock.RLock()
   568  		rtts = append(rtts, float64(p.rtt))
   569  		p.lock.RUnlock()
   570  	}
   571  	sort.Float64s(rtts)
   572  
   573  	median := rttMaxEstimate
   574  	if qosTuningPeers <= len(rtts) {
   575  		median = time.Duration(rtts[qosTuningPeers/2]) // Median of our tuning peers
   576  	} else if len(rtts) > 0 {
   577  		median = time.Duration(rtts[len(rtts)/2]) // Median of our connected peers (maintain even like this some baseline qos)
   578  	}
   579  	// Restrict the RTT into some QoS defaults, irrelevant of true RTT
   580  	if median < rttMinEstimate {
   581  		median = rttMinEstimate
   582  	}
   583  	if median > rttMaxEstimate {
   584  		median = rttMaxEstimate
   585  	}
   586  	return median
   587  }
   588  
   589  type peerConnectionFeed struct {
   590  	mu   sync.RWMutex
   591  	subs map[chan<- *peerConnection]string
   592  }
   593  
   594  func (f *peerConnectionFeed) Close() {
   595  	f.mu.Lock()
   596  	defer f.mu.Unlock()
   597  	for sub := range f.subs {
   598  		close(sub)
   599  	}
   600  	f.subs = nil
   601  }
   602  
   603  func (f *peerConnectionFeed) Subscribe(ch chan<- *peerConnection, name string) {
   604  	f.mu.Lock()
   605  	defer f.mu.Unlock()
   606  	if f.subs == nil {
   607  		f.subs = make(map[chan<- *peerConnection]string)
   608  	}
   609  	f.subs[ch] = name
   610  }
   611  
   612  func (f *peerConnectionFeed) Unsubscribe(ch chan<- *peerConnection) {
   613  	f.mu.Lock()
   614  	defer f.mu.Unlock()
   615  	if _, ok := f.subs[ch]; ok {
   616  		delete(f.subs, ch)
   617  		close(ch)
   618  	}
   619  }
   620  
   621  const timeout = 500 * time.Millisecond
   622  
   623  func (f *peerConnectionFeed) Send(e *peerConnection) {
   624  	f.mu.RLock()
   625  	defer f.mu.RUnlock()
   626  	for sub, name := range f.subs {
   627  		select {
   628  		case sub <- e:
   629  		default:
   630  			start := time.Now()
   631  			var action string
   632  			select {
   633  			case sub <- e:
   634  				action = "delayed"
   635  			case <-time.After(timeout):
   636  				action = "dropped"
   637  			}
   638  			dur := time.Since(start)
   639  			log.Warn(fmt.Sprintf("peerConnectionFeed send %s: channel full", action), "name", name, "cap", cap(sub), "time", dur, "val", e)
   640  		}
   641  	}
   642  }