congestion.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package kcp
  2. import (
  3. "sync"
  4. )
  5. const (
  6. defaultRTT = 100
  7. queueSize = 10
  8. )
  9. type Queue struct {
  10. value [queueSize]uint32
  11. start uint32
  12. length uint32
  13. }
  14. func (v *Queue) Push(value uint32) {
  15. if v.length < queueSize {
  16. v.value[v.length] = value
  17. v.length++
  18. return
  19. }
  20. v.value[v.start] = value
  21. v.start++
  22. if v.start == queueSize {
  23. v.start = 0
  24. }
  25. }
  26. func (v *Queue) Max() uint32 {
  27. max := v.value[0]
  28. for i := 1; i < queueSize; i++ {
  29. if v.value[i] > max {
  30. max = v.value[i]
  31. }
  32. }
  33. return max
  34. }
  35. func (v *Queue) Min() uint32 {
  36. max := v.value[0]
  37. for i := 1; i < queueSize; i++ {
  38. if v.value[i] < max {
  39. max = v.value[i]
  40. }
  41. }
  42. return max
  43. }
  44. type CongestionState byte
  45. const (
  46. CongestionStateRTTProbe CongestionState = iota
  47. CongestionStateBandwidthProbe
  48. CongestionStateTransfer
  49. )
  50. type Congestion struct {
  51. sync.RWMutex
  52. state CongestionState
  53. stateSince uint32
  54. limit uint32 // bytes per 1000 seconds
  55. rtt uint32 // millisec
  56. rttHistory Queue
  57. rttUpdateTime uint32
  58. initialThroughput uint32 // bytes per 1000 seconds
  59. cycleStartTime uint32
  60. cycleBytesConfirmed uint32
  61. cycleBytesSent uint32
  62. cycleBytesLimit uint32
  63. cycle uint32
  64. bestCycleBytesConfirmed uint32
  65. bestCycleBytesSent uint32
  66. }
  67. func (v *Congestion) SetState(current uint32, state CongestionState) {
  68. v.state = state
  69. v.stateSince = current
  70. }
  71. func (v *Congestion) Update(current uint32) {
  72. switch v.state {
  73. case CongestionStateRTTProbe:
  74. if v.rtt > 0 {
  75. v.SetState(current, CongestionStateBandwidthProbe)
  76. v.cycleStartTime = current
  77. v.cycleBytesConfirmed = 0
  78. v.cycleBytesSent = 0
  79. v.cycleBytesLimit = v.initialThroughput * v.rtt / 1000 / 1000
  80. }
  81. case CongestionStateBandwidthProbe:
  82. if current-v.cycleStartTime >= v.rtt {
  83. }
  84. }
  85. }
  86. func (v *Congestion) AddBytesConfirmed(current uint32, bytesConfirmed uint32) {
  87. v.Lock()
  88. defer v.Unlock()
  89. v.cycleBytesConfirmed += bytesConfirmed
  90. v.Update(current)
  91. }
  92. func (v *Congestion) UpdateRTT(current uint32, rtt uint32) {
  93. v.Lock()
  94. defer v.Unlock()
  95. if v.state == CongestionStateRTTProbe || rtt < v.rtt {
  96. v.rtt = rtt
  97. v.rttUpdateTime = current
  98. }
  99. v.Update(current)
  100. }
  101. func (v *Congestion) GetBytesLimit() uint32 {
  102. v.RLock()
  103. defer v.RUnlock()
  104. if v.state == CongestionStateRTTProbe {
  105. return v.initialThroughput/1000/(1000/defaultRTT) - v.cycleBytesSent
  106. }
  107. return v.cycleBytesLimit
  108. }
  109. func (v *Congestion) RoundTripTime() uint32 {
  110. v.RLock()
  111. defer v.RUnlock()
  112. if v.state == CongestionStateRTTProbe {
  113. return defaultRTT
  114. }
  115. return v.rtt
  116. }
  117. func (v *Congestion) Timeout() uint32 {
  118. v.RLock()
  119. defer v.RUnlock()
  120. if v.state == CongestionStateRTTProbe {
  121. return defaultRTT * 3 / 2
  122. }
  123. return v.rtt * 3 / 2
  124. }