peer.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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. // Contains the active peer-set of the downloader, maintaining both failures
  17. // as well as reputation metrics to prioritize the block retrievals.
  18. package downloader
  19. import (
  20. "errors"
  21. "fmt"
  22. "math"
  23. "math/big"
  24. "sort"
  25. "sync"
  26. "sync/atomic"
  27. "time"
  28. "github.com/ethereum/go-ethereum/common"
  29. "github.com/ethereum/go-ethereum/event"
  30. "github.com/ethereum/go-ethereum/log"
  31. )
  32. const (
  33. maxLackingHashes = 4096 // Maximum number of entries allowed on the list or lacking items
  34. measurementImpact = 0.1 // The impact a single measurement has on a peer's final throughput value.
  35. )
  36. var (
  37. errAlreadyFetching = errors.New("already fetching blocks from peer")
  38. errAlreadyRegistered = errors.New("peer is already registered")
  39. errNotRegistered = errors.New("peer is not registered")
  40. )
  41. // peerConnection represents an active peer from which hashes and blocks are retrieved.
  42. type peerConnection struct {
  43. id string // Unique identifier of the peer
  44. headerIdle int32 // Current header activity state of the peer (idle = 0, active = 1)
  45. blockIdle int32 // Current block activity state of the peer (idle = 0, active = 1)
  46. receiptIdle int32 // Current receipt activity state of the peer (idle = 0, active = 1)
  47. stateIdle int32 // Current node data activity state of the peer (idle = 0, active = 1)
  48. headerThroughput float64 // Number of headers measured to be retrievable per second
  49. blockThroughput float64 // Number of blocks (bodies) measured to be retrievable per second
  50. receiptThroughput float64 // Number of receipts measured to be retrievable per second
  51. stateThroughput float64 // Number of node data pieces measured to be retrievable per second
  52. rtt time.Duration // Request round trip time to track responsiveness (QoS)
  53. headerStarted time.Time // Time instance when the last header fetch was started
  54. blockStarted time.Time // Time instance when the last block (body) fetch was started
  55. receiptStarted time.Time // Time instance when the last receipt fetch was started
  56. stateStarted time.Time // Time instance when the last node data fetch was started
  57. lacking map[common.Hash]struct{} // Set of hashes not to request (didn't have previously)
  58. peer Peer
  59. version int // Eth protocol version number to switch strategies
  60. log log.Logger // Contextual logger to add extra infos to peer logs
  61. lock sync.RWMutex
  62. }
  63. // LightPeer encapsulates the methods required to synchronise with a remote light peer.
  64. type LightPeer interface {
  65. Head() (common.Hash, *big.Int)
  66. RequestHeadersByHash(common.Hash, int, int, bool) error
  67. RequestHeadersByNumber(uint64, int, int, bool) error
  68. }
  69. // Peer encapsulates the methods required to synchronise with a remote full peer.
  70. type Peer interface {
  71. LightPeer
  72. RequestBodies([]common.Hash) error
  73. RequestReceipts([]common.Hash) error
  74. RequestNodeData([]common.Hash) error
  75. }
  76. // lightPeerWrapper wraps a LightPeer struct, stubbing out the Peer-only methods.
  77. type lightPeerWrapper struct {
  78. peer LightPeer
  79. }
  80. func (w *lightPeerWrapper) Head() (common.Hash, *big.Int) { return w.peer.Head() }
  81. func (w *lightPeerWrapper) RequestHeadersByHash(h common.Hash, amount int, skip int, reverse bool) error {
  82. return w.peer.RequestHeadersByHash(h, amount, skip, reverse)
  83. }
  84. func (w *lightPeerWrapper) RequestHeadersByNumber(i uint64, amount int, skip int, reverse bool) error {
  85. return w.peer.RequestHeadersByNumber(i, amount, skip, reverse)
  86. }
  87. func (w *lightPeerWrapper) RequestBodies([]common.Hash) error {
  88. panic("RequestBodies not supported in light client mode sync")
  89. }
  90. func (w *lightPeerWrapper) RequestReceipts([]common.Hash) error {
  91. panic("RequestReceipts not supported in light client mode sync")
  92. }
  93. func (w *lightPeerWrapper) RequestNodeData([]common.Hash) error {
  94. panic("RequestNodeData not supported in light client mode sync")
  95. }
  96. // newPeerConnection creates a new downloader peer.
  97. func newPeerConnection(id string, version int, peer Peer, logger log.Logger) *peerConnection {
  98. return &peerConnection{
  99. id: id,
  100. lacking: make(map[common.Hash]struct{}),
  101. peer: peer,
  102. version: version,
  103. log: logger,
  104. }
  105. }
  106. // Reset clears the internal state of a peer entity.
  107. func (p *peerConnection) Reset() {
  108. p.lock.Lock()
  109. defer p.lock.Unlock()
  110. atomic.StoreInt32(&p.headerIdle, 0)
  111. atomic.StoreInt32(&p.blockIdle, 0)
  112. atomic.StoreInt32(&p.receiptIdle, 0)
  113. atomic.StoreInt32(&p.stateIdle, 0)
  114. p.headerThroughput = 0
  115. p.blockThroughput = 0
  116. p.receiptThroughput = 0
  117. p.stateThroughput = 0
  118. p.lacking = make(map[common.Hash]struct{})
  119. }
  120. // FetchHeaders sends a header retrieval request to the remote peer.
  121. func (p *peerConnection) FetchHeaders(from uint64, count int) error {
  122. // Sanity check the protocol version
  123. if p.version < 62 {
  124. panic(fmt.Sprintf("header fetch [eth/62+] requested on eth/%d", p.version))
  125. }
  126. // Short circuit if the peer is already fetching
  127. if !atomic.CompareAndSwapInt32(&p.headerIdle, 0, 1) {
  128. return errAlreadyFetching
  129. }
  130. p.headerStarted = time.Now()
  131. // Issue the header retrieval request (absolut upwards without gaps)
  132. go p.peer.RequestHeadersByNumber(from, count, 0, false)
  133. return nil
  134. }
  135. // FetchBodies sends a block body retrieval request to the remote peer.
  136. func (p *peerConnection) FetchBodies(request *fetchRequest) error {
  137. // Sanity check the protocol version
  138. if p.version < 62 {
  139. panic(fmt.Sprintf("body fetch [eth/62+] requested on eth/%d", p.version))
  140. }
  141. // Short circuit if the peer is already fetching
  142. if !atomic.CompareAndSwapInt32(&p.blockIdle, 0, 1) {
  143. return errAlreadyFetching
  144. }
  145. p.blockStarted = time.Now()
  146. // Convert the header set to a retrievable slice
  147. hashes := make([]common.Hash, 0, len(request.Headers))
  148. for _, header := range request.Headers {
  149. hashes = append(hashes, header.Hash())
  150. }
  151. go p.peer.RequestBodies(hashes)
  152. return nil
  153. }
  154. // FetchReceipts sends a receipt retrieval request to the remote peer.
  155. func (p *peerConnection) FetchReceipts(request *fetchRequest) error {
  156. // Sanity check the protocol version
  157. if p.version < 63 {
  158. panic(fmt.Sprintf("body fetch [eth/63+] requested on eth/%d", p.version))
  159. }
  160. // Short circuit if the peer is already fetching
  161. if !atomic.CompareAndSwapInt32(&p.receiptIdle, 0, 1) {
  162. return errAlreadyFetching
  163. }
  164. p.receiptStarted = time.Now()
  165. // Convert the header set to a retrievable slice
  166. hashes := make([]common.Hash, 0, len(request.Headers))
  167. for _, header := range request.Headers {
  168. hashes = append(hashes, header.Hash())
  169. }
  170. go p.peer.RequestReceipts(hashes)
  171. return nil
  172. }
  173. // FetchNodeData sends a node state data retrieval request to the remote peer.
  174. func (p *peerConnection) FetchNodeData(hashes []common.Hash) error {
  175. // Sanity check the protocol version
  176. if p.version < 63 {
  177. panic(fmt.Sprintf("node data fetch [eth/63+] requested on eth/%d", p.version))
  178. }
  179. // Short circuit if the peer is already fetching
  180. if !atomic.CompareAndSwapInt32(&p.stateIdle, 0, 1) {
  181. return errAlreadyFetching
  182. }
  183. p.stateStarted = time.Now()
  184. go p.peer.RequestNodeData(hashes)
  185. return nil
  186. }
  187. // SetHeadersIdle sets the peer to idle, allowing it to execute new header retrieval
  188. // requests. Its estimated header retrieval throughput is updated with that measured
  189. // just now.
  190. func (p *peerConnection) SetHeadersIdle(delivered int) {
  191. p.setIdle(p.headerStarted, delivered, &p.headerThroughput, &p.headerIdle)
  192. }
  193. // SetBlocksIdle sets the peer to idle, allowing it to execute new block retrieval
  194. // requests. Its estimated block retrieval throughput is updated with that measured
  195. // just now.
  196. func (p *peerConnection) SetBlocksIdle(delivered int) {
  197. p.setIdle(p.blockStarted, delivered, &p.blockThroughput, &p.blockIdle)
  198. }
  199. // SetBodiesIdle sets the peer to idle, allowing it to execute block body retrieval
  200. // requests. Its estimated body retrieval throughput is updated with that measured
  201. // just now.
  202. func (p *peerConnection) SetBodiesIdle(delivered int) {
  203. p.setIdle(p.blockStarted, delivered, &p.blockThroughput, &p.blockIdle)
  204. }
  205. // SetReceiptsIdle sets the peer to idle, allowing it to execute new receipt
  206. // retrieval requests. Its estimated receipt retrieval throughput is updated
  207. // with that measured just now.
  208. func (p *peerConnection) SetReceiptsIdle(delivered int) {
  209. p.setIdle(p.receiptStarted, delivered, &p.receiptThroughput, &p.receiptIdle)
  210. }
  211. // SetNodeDataIdle sets the peer to idle, allowing it to execute new state trie
  212. // data retrieval requests. Its estimated state retrieval throughput is updated
  213. // with that measured just now.
  214. func (p *peerConnection) SetNodeDataIdle(delivered int) {
  215. p.setIdle(p.stateStarted, delivered, &p.stateThroughput, &p.stateIdle)
  216. }
  217. // setIdle sets the peer to idle, allowing it to execute new retrieval requests.
  218. // Its estimated retrieval throughput is updated with that measured just now.
  219. func (p *peerConnection) setIdle(started time.Time, delivered int, throughput *float64, idle *int32) {
  220. // Irrelevant of the scaling, make sure the peer ends up idle
  221. defer atomic.StoreInt32(idle, 0)
  222. p.lock.Lock()
  223. defer p.lock.Unlock()
  224. // If nothing was delivered (hard timeout / unavailable data), reduce throughput to minimum
  225. if delivered == 0 {
  226. *throughput = 0
  227. return
  228. }
  229. // Otherwise update the throughput with a new measurement
  230. elapsed := time.Since(started) + 1 // +1 (ns) to ensure non-zero divisor
  231. measured := float64(delivered) / (float64(elapsed) / float64(time.Second))
  232. *throughput = (1-measurementImpact)*(*throughput) + measurementImpact*measured
  233. p.rtt = time.Duration((1-measurementImpact)*float64(p.rtt) + measurementImpact*float64(elapsed))
  234. p.log.Trace("Peer throughput measurements updated",
  235. "hps", p.headerThroughput, "bps", p.blockThroughput,
  236. "rps", p.receiptThroughput, "sps", p.stateThroughput,
  237. "miss", len(p.lacking), "rtt", p.rtt)
  238. }
  239. // HeaderCapacity retrieves the peers header download allowance based on its
  240. // previously discovered throughput.
  241. func (p *peerConnection) HeaderCapacity(targetRTT time.Duration) int {
  242. p.lock.RLock()
  243. defer p.lock.RUnlock()
  244. return int(math.Min(1+math.Max(1, p.headerThroughput*float64(targetRTT)/float64(time.Second)), float64(MaxHeaderFetch)))
  245. }
  246. // BlockCapacity retrieves the peers block download allowance based on its
  247. // previously discovered throughput.
  248. func (p *peerConnection) BlockCapacity(targetRTT time.Duration) int {
  249. p.lock.RLock()
  250. defer p.lock.RUnlock()
  251. return int(math.Min(1+math.Max(1, p.blockThroughput*float64(targetRTT)/float64(time.Second)), float64(MaxBlockFetch)))
  252. }
  253. // ReceiptCapacity retrieves the peers receipt download allowance based on its
  254. // previously discovered throughput.
  255. func (p *peerConnection) ReceiptCapacity(targetRTT time.Duration) int {
  256. p.lock.RLock()
  257. defer p.lock.RUnlock()
  258. return int(math.Min(1+math.Max(1, p.receiptThroughput*float64(targetRTT)/float64(time.Second)), float64(MaxReceiptFetch)))
  259. }
  260. // NodeDataCapacity retrieves the peers state download allowance based on its
  261. // previously discovered throughput.
  262. func (p *peerConnection) NodeDataCapacity(targetRTT time.Duration) int {
  263. p.lock.RLock()
  264. defer p.lock.RUnlock()
  265. return int(math.Min(1+math.Max(1, p.stateThroughput*float64(targetRTT)/float64(time.Second)), float64(MaxStateFetch)))
  266. }
  267. // MarkLacking appends a new entity to the set of items (blocks, receipts, states)
  268. // that a peer is known not to have (i.e. have been requested before). If the
  269. // set reaches its maximum allowed capacity, items are randomly dropped off.
  270. func (p *peerConnection) MarkLacking(hash common.Hash) {
  271. p.lock.Lock()
  272. defer p.lock.Unlock()
  273. for len(p.lacking) >= maxLackingHashes {
  274. for drop := range p.lacking {
  275. delete(p.lacking, drop)
  276. break
  277. }
  278. }
  279. p.lacking[hash] = struct{}{}
  280. }
  281. // Lacks retrieves whether the hash of a blockchain item is on the peers lacking
  282. // list (i.e. whether we know that the peer does not have it).
  283. func (p *peerConnection) Lacks(hash common.Hash) bool {
  284. p.lock.RLock()
  285. defer p.lock.RUnlock()
  286. _, ok := p.lacking[hash]
  287. return ok
  288. }
  289. // peerSet represents the collection of active peer participating in the chain
  290. // download procedure.
  291. type peerSet struct {
  292. peers map[string]*peerConnection
  293. newPeerFeed event.Feed
  294. peerDropFeed event.Feed
  295. lock sync.RWMutex
  296. }
  297. // newPeerSet creates a new peer set top track the active download sources.
  298. func newPeerSet() *peerSet {
  299. return &peerSet{
  300. peers: make(map[string]*peerConnection),
  301. }
  302. }
  303. // SubscribeNewPeers subscribes to peer arrival events.
  304. func (ps *peerSet) SubscribeNewPeers(ch chan<- *peerConnection) event.Subscription {
  305. return ps.newPeerFeed.Subscribe(ch)
  306. }
  307. // SubscribePeerDrops subscribes to peer departure events.
  308. func (ps *peerSet) SubscribePeerDrops(ch chan<- *peerConnection) event.Subscription {
  309. return ps.peerDropFeed.Subscribe(ch)
  310. }
  311. // Reset iterates over the current peer set, and resets each of the known peers
  312. // to prepare for a next batch of block retrieval.
  313. func (ps *peerSet) Reset() {
  314. ps.lock.RLock()
  315. defer ps.lock.RUnlock()
  316. for _, peer := range ps.peers {
  317. peer.Reset()
  318. }
  319. }
  320. // Register injects a new peer into the working set, or returns an error if the
  321. // peer is already known.
  322. //
  323. // The method also sets the starting throughput values of the new peer to the
  324. // average of all existing peers, to give it a realistic chance of being used
  325. // for data retrievals.
  326. func (ps *peerSet) Register(p *peerConnection) error {
  327. // Retrieve the current median RTT as a sane default
  328. p.rtt = ps.medianRTT()
  329. // Register the new peer with some meaningful defaults
  330. ps.lock.Lock()
  331. if _, ok := ps.peers[p.id]; ok {
  332. ps.lock.Unlock()
  333. return errAlreadyRegistered
  334. }
  335. if len(ps.peers) > 0 {
  336. p.headerThroughput, p.blockThroughput, p.receiptThroughput, p.stateThroughput = 0, 0, 0, 0
  337. for _, peer := range ps.peers {
  338. peer.lock.RLock()
  339. p.headerThroughput += peer.headerThroughput
  340. p.blockThroughput += peer.blockThroughput
  341. p.receiptThroughput += peer.receiptThroughput
  342. p.stateThroughput += peer.stateThroughput
  343. peer.lock.RUnlock()
  344. }
  345. p.headerThroughput /= float64(len(ps.peers))
  346. p.blockThroughput /= float64(len(ps.peers))
  347. p.receiptThroughput /= float64(len(ps.peers))
  348. p.stateThroughput /= float64(len(ps.peers))
  349. }
  350. ps.peers[p.id] = p
  351. ps.lock.Unlock()
  352. ps.newPeerFeed.Send(p)
  353. return nil
  354. }
  355. // Unregister removes a remote peer from the active set, disabling any further
  356. // actions to/from that particular entity.
  357. func (ps *peerSet) Unregister(id string) error {
  358. ps.lock.Lock()
  359. p, ok := ps.peers[id]
  360. if !ok {
  361. defer ps.lock.Unlock()
  362. return errNotRegistered
  363. }
  364. delete(ps.peers, id)
  365. ps.lock.Unlock()
  366. ps.peerDropFeed.Send(p)
  367. return nil
  368. }
  369. // Peer retrieves the registered peer with the given id.
  370. func (ps *peerSet) Peer(id string) *peerConnection {
  371. ps.lock.RLock()
  372. defer ps.lock.RUnlock()
  373. return ps.peers[id]
  374. }
  375. // Len returns if the current number of peers in the set.
  376. func (ps *peerSet) Len() int {
  377. ps.lock.RLock()
  378. defer ps.lock.RUnlock()
  379. return len(ps.peers)
  380. }
  381. // AllPeers retrieves a flat list of all the peers within the set.
  382. func (ps *peerSet) AllPeers() []*peerConnection {
  383. ps.lock.RLock()
  384. defer ps.lock.RUnlock()
  385. list := make([]*peerConnection, 0, len(ps.peers))
  386. for _, p := range ps.peers {
  387. list = append(list, p)
  388. }
  389. return list
  390. }
  391. // HeaderIdlePeers retrieves a flat list of all the currently header-idle peers
  392. // within the active peer set, ordered by their reputation.
  393. func (ps *peerSet) HeaderIdlePeers() ([]*peerConnection, int) {
  394. idle := func(p *peerConnection) bool {
  395. return atomic.LoadInt32(&p.headerIdle) == 0
  396. }
  397. throughput := func(p *peerConnection) float64 {
  398. p.lock.RLock()
  399. defer p.lock.RUnlock()
  400. return p.headerThroughput
  401. }
  402. return ps.idlePeers(62, 64, idle, throughput)
  403. }
  404. // BodyIdlePeers retrieves a flat list of all the currently body-idle peers within
  405. // the active peer set, ordered by their reputation.
  406. func (ps *peerSet) BodyIdlePeers() ([]*peerConnection, int) {
  407. idle := func(p *peerConnection) bool {
  408. return atomic.LoadInt32(&p.blockIdle) == 0
  409. }
  410. throughput := func(p *peerConnection) float64 {
  411. p.lock.RLock()
  412. defer p.lock.RUnlock()
  413. return p.blockThroughput
  414. }
  415. return ps.idlePeers(62, 64, idle, throughput)
  416. }
  417. // ReceiptIdlePeers retrieves a flat list of all the currently receipt-idle peers
  418. // within the active peer set, ordered by their reputation.
  419. func (ps *peerSet) ReceiptIdlePeers() ([]*peerConnection, int) {
  420. idle := func(p *peerConnection) bool {
  421. return atomic.LoadInt32(&p.receiptIdle) == 0
  422. }
  423. throughput := func(p *peerConnection) float64 {
  424. p.lock.RLock()
  425. defer p.lock.RUnlock()
  426. return p.receiptThroughput
  427. }
  428. return ps.idlePeers(63, 64, idle, throughput)
  429. }
  430. // NodeDataIdlePeers retrieves a flat list of all the currently node-data-idle
  431. // peers within the active peer set, ordered by their reputation.
  432. func (ps *peerSet) NodeDataIdlePeers() ([]*peerConnection, int) {
  433. idle := func(p *peerConnection) bool {
  434. return atomic.LoadInt32(&p.stateIdle) == 0
  435. }
  436. throughput := func(p *peerConnection) float64 {
  437. p.lock.RLock()
  438. defer p.lock.RUnlock()
  439. return p.stateThroughput
  440. }
  441. return ps.idlePeers(63, 64, idle, throughput)
  442. }
  443. // idlePeers retrieves a flat list of all currently idle peers satisfying the
  444. // protocol version constraints, using the provided function to check idleness.
  445. // The resulting set of peers are sorted by their measure throughput.
  446. func (ps *peerSet) idlePeers(minProtocol, maxProtocol int, idleCheck func(*peerConnection) bool, throughput func(*peerConnection) float64) ([]*peerConnection, int) {
  447. ps.lock.RLock()
  448. defer ps.lock.RUnlock()
  449. idle, total := make([]*peerConnection, 0, len(ps.peers)), 0
  450. for _, p := range ps.peers {
  451. if p.version >= minProtocol && p.version <= maxProtocol {
  452. if idleCheck(p) {
  453. idle = append(idle, p)
  454. }
  455. total++
  456. }
  457. }
  458. for i := 0; i < len(idle); i++ {
  459. for j := i + 1; j < len(idle); j++ {
  460. if throughput(idle[i]) < throughput(idle[j]) {
  461. idle[i], idle[j] = idle[j], idle[i]
  462. }
  463. }
  464. }
  465. return idle, total
  466. }
  467. // medianRTT returns the median RTT of the peerset, considering only the tuning
  468. // peers if there are more peers available.
  469. func (ps *peerSet) medianRTT() time.Duration {
  470. // Gather all the currently measured round trip times
  471. ps.lock.RLock()
  472. defer ps.lock.RUnlock()
  473. rtts := make([]float64, 0, len(ps.peers))
  474. for _, p := range ps.peers {
  475. p.lock.RLock()
  476. rtts = append(rtts, float64(p.rtt))
  477. p.lock.RUnlock()
  478. }
  479. sort.Float64s(rtts)
  480. median := rttMaxEstimate
  481. if qosTuningPeers <= len(rtts) {
  482. median = time.Duration(rtts[qosTuningPeers/2]) // Median of our tuning peers
  483. } else if len(rtts) > 0 {
  484. median = time.Duration(rtts[len(rtts)/2]) // Median of our connected peers (maintain even like this some baseline qos)
  485. }
  486. // Restrict the RTT into some QoS defaults, irrelevant of true RTT
  487. if median < rttMinEstimate {
  488. median = rttMinEstimate
  489. }
  490. if median > rttMaxEstimate {
  491. median = rttMaxEstimate
  492. }
  493. return median
  494. }