|
| 1 | +package gbn |
| 2 | + |
| 3 | +import ( |
| 4 | + "math" |
| 5 | + "sync" |
| 6 | + "time" |
| 7 | +) |
| 8 | + |
| 9 | +const ( |
| 10 | + defaultTMHandshakeTimeout = 100 * time.Millisecond |
| 11 | + defaultTMResendTimeout = 100 * time.Millisecond |
| 12 | + minimumResendTimeout = 100 * time.Millisecond |
| 13 | + defaultTMFinSendTimeout = 1000 * time.Millisecond |
| 14 | + defaultResendMultiplier = 5 |
| 15 | + DefaultTMSendTimeout = math.MaxInt64 |
| 16 | + DefaultTMRecvTimeout = math.MaxInt64 |
| 17 | +) |
| 18 | + |
| 19 | +// TimeoutManager manages the different timeouts used by the gbn package. |
| 20 | +type TimeoutManager struct { |
| 21 | + // isServer is used to indicate whether the timeout manager is used |
| 22 | + // by the server or the client. |
| 23 | + isServer bool |
| 24 | + |
| 25 | + // useStaticTimeout is used to indicate whether the resendTimeout |
| 26 | + // has been manually set, and if so, should not be updated dynamically. |
| 27 | + useStaticTimeout bool |
| 28 | + |
| 29 | + // resendTimeout defines the current resend timeout used by the |
| 30 | + // timeout manager. |
| 31 | + // The resend timeout is the duration that will be waited before |
| 32 | + // resending the packets in the current queue. The timeout is |
| 33 | + // dynamically set during the handshake process, and is set to the time |
| 34 | + // it took for the other party to respond, multiplied by the |
| 35 | + // resendMultiplier. |
| 36 | + resendTimeout time.Duration |
| 37 | + |
| 38 | + // resendMultiplier defines the multiplier used when multiplying the |
| 39 | + // duration it took for the other party to respond when setting the |
| 40 | + // resendTimeout dynamically during the handshake. |
| 41 | + resendMultiplier int |
| 42 | + |
| 43 | + // latestSentSYNTime is used to keep track of the time when the latest |
| 44 | + // SYN message was sent. This is used to dynamically set the resend |
| 45 | + // timeout, based on how long it took for the other party to respond to |
| 46 | + // the SYN message. |
| 47 | + latestSentSYNTime time.Time |
| 48 | + |
| 49 | + // handshakeTimeout is the time after which the server or client |
| 50 | + // will abort and restart the handshake if the expected response is |
| 51 | + // not received from the peer. |
| 52 | + handshakeTimeout time.Duration |
| 53 | + |
| 54 | + // finSendTimeout is the timeout after which the created context for |
| 55 | + // sending a FIN message will be time out. |
| 56 | + finSendTimeout time.Duration |
| 57 | + |
| 58 | + // sendTimeout defines the max time we will wait to send a msg before |
| 59 | + // timing out. |
| 60 | + sendTimeout time.Duration |
| 61 | + |
| 62 | + // recvTimeout defines the max time we will wait to receive a msg before |
| 63 | + // timing out. |
| 64 | + recvTimeout time.Duration |
| 65 | + |
| 66 | + // timeoutManagerMu should be locked when updating or accessing any of |
| 67 | + // timeout manager's timeout fields. |
| 68 | + timeoutManagerMu sync.RWMutex |
| 69 | +} |
| 70 | + |
| 71 | +// NewTimeOutManager creates a new timeout manager. |
| 72 | +func NewTimeOutManager(isServer bool) *TimeoutManager { |
| 73 | + return &TimeoutManager{ |
| 74 | + isServer: isServer, |
| 75 | + resendTimeout: defaultTMResendTimeout, |
| 76 | + handshakeTimeout: defaultTMHandshakeTimeout, |
| 77 | + useStaticTimeout: false, |
| 78 | + resendMultiplier: defaultResendMultiplier, |
| 79 | + finSendTimeout: defaultTMFinSendTimeout, |
| 80 | + recvTimeout: DefaultTMRecvTimeout, |
| 81 | + sendTimeout: DefaultTMSendTimeout, |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// Sent is should be called when a message is sent by the connection. |
| 86 | +func (m *TimeoutManager) Sent(msg Message) { |
| 87 | + // TODO(viktor): In the future, we may want to use this to keep track of |
| 88 | + // the time it took for the other party to respond to other types of |
| 89 | + // messages than the handshake, and dynamically keep updating the resend |
| 90 | + // timeout to ensure that it reflects the current response time. |
| 91 | + switch msg.(type) { //nolint:gocritic |
| 92 | + case *PacketSYN: |
| 93 | + // Note that we may send multiple SYN messages before receiving |
| 94 | + // a response. Therefore, this field might be updated multiple |
| 95 | + // times. |
| 96 | + m.latestSentSYNTime = time.Now() |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +// Received is should be called when a message is received by the connection. |
| 101 | +func (m *TimeoutManager) Received(msg Message) { |
| 102 | + // TODO(viktor): In the future, we may want to use this to keep track of |
| 103 | + // the time it took for the other party to respond to other types of |
| 104 | + // messages than the handshake, and dynamically keep updating the resend |
| 105 | + // timeout to ensure that it reflects the current response time. |
| 106 | + switch msg.(type) { |
| 107 | + case *PacketSYN: |
| 108 | + if !m.isServer { |
| 109 | + m.updateResendTimeout(time.Since(m.latestSentSYNTime)) |
| 110 | + } |
| 111 | + |
| 112 | + case *PacketSYNACK: |
| 113 | + if m.isServer { |
| 114 | + m.updateResendTimeout(time.Since(m.latestSentSYNTime)) |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +// updateResendTimeout updates the resend timeout based on the given response |
| 120 | +// time. The resend timeout will be only be updated if the given response time |
| 121 | +// is greater than the default resend timeout, after being multiplied by the |
| 122 | +// resendMultiplier. |
| 123 | +// If a static timeout has been manually set, then this function will do be a |
| 124 | +// no-op. |
| 125 | +func (m *TimeoutManager) updateResendTimeout(responseTime time.Duration) { |
| 126 | + if m.useStaticTimeout { |
| 127 | + log.Tracef("Not increasing resendTimeout as it has been set " + |
| 128 | + "manually") |
| 129 | + |
| 130 | + return |
| 131 | + } |
| 132 | + |
| 133 | + multipliedTimeout := time.Duration(m.resendMultiplier) * responseTime |
| 134 | + |
| 135 | + if multipliedTimeout > minimumResendTimeout { |
| 136 | + log.Tracef("Updating resendTimeout to %v", multipliedTimeout) |
| 137 | + |
| 138 | + m.resendTimeout = multipliedTimeout |
| 139 | + } else { |
| 140 | + log.Tracef("Not updating resendTimeout to %v as it is not "+ |
| 141 | + "greater than the minimum resendTimeout which is %v", |
| 142 | + multipliedTimeout, m.resendTimeout) |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +// GetResendTimeout returns the current resend timeout. |
| 147 | +func (m *TimeoutManager) GetResendTimeout() time.Duration { |
| 148 | + m.timeoutManagerMu.RLock() |
| 149 | + defer m.timeoutManagerMu.RUnlock() |
| 150 | + |
| 151 | + return m.resendTimeout |
| 152 | +} |
| 153 | + |
| 154 | +// GetHandshakeTimeout returns the handshake timeout. |
| 155 | +func (m *TimeoutManager) GetHandshakeTimeout() time.Duration { |
| 156 | + m.timeoutManagerMu.RLock() |
| 157 | + defer m.timeoutManagerMu.RUnlock() |
| 158 | + |
| 159 | + return m.handshakeTimeout |
| 160 | +} |
| 161 | + |
| 162 | +// GetFinSendTimeout returns the fin send timeout. |
| 163 | +func (m *TimeoutManager) GetFinSendTimeout() time.Duration { |
| 164 | + m.timeoutManagerMu.RLock() |
| 165 | + defer m.timeoutManagerMu.RUnlock() |
| 166 | + |
| 167 | + return m.finSendTimeout |
| 168 | +} |
| 169 | + |
| 170 | +// GetSendTimeout returns the send timeout. |
| 171 | +func (m *TimeoutManager) GetSendTimeout() time.Duration { |
| 172 | + m.timeoutManagerMu.RLock() |
| 173 | + defer m.timeoutManagerMu.RUnlock() |
| 174 | + |
| 175 | + return m.sendTimeout |
| 176 | +} |
| 177 | + |
| 178 | +// GetRecvTimeout returns the recv timeout. |
| 179 | +func (m *TimeoutManager) GetRecvTimeout() time.Duration { |
| 180 | + m.timeoutManagerMu.RLock() |
| 181 | + defer m.timeoutManagerMu.RUnlock() |
| 182 | + |
| 183 | + return m.recvTimeout |
| 184 | +} |
| 185 | + |
| 186 | +// SetStaticResendTimeout sets the resend timeout to the given value, and also |
| 187 | +// marks the timeout manager as using a static resend timeout, which will mean |
| 188 | +// that the resend timeout will not be updated dynamically. |
| 189 | +func (m *TimeoutManager) SetStaticResendTimeout(resendTimeout time.Duration) { |
| 190 | + m.timeoutManagerMu.Lock() |
| 191 | + defer m.timeoutManagerMu.Unlock() |
| 192 | + |
| 193 | + m.resendTimeout = resendTimeout |
| 194 | + m.useStaticTimeout = true |
| 195 | +} |
| 196 | + |
| 197 | +// SetResendMultiplier sets the resend multiplier used when dynamically |
| 198 | +// setting the resend timeout. |
| 199 | +func (m *TimeoutManager) SetResendMultiplier(resendMultiplier int) { |
| 200 | + m.timeoutManagerMu.Lock() |
| 201 | + defer m.timeoutManagerMu.Unlock() |
| 202 | + |
| 203 | + m.resendMultiplier = resendMultiplier |
| 204 | +} |
| 205 | + |
| 206 | +// SetHandshakeTimeout sets the handshake timeout. |
| 207 | +func (m *TimeoutManager) SetHandshakeTimeout(handshakeTimeout time.Duration) { |
| 208 | + m.timeoutManagerMu.Lock() |
| 209 | + defer m.timeoutManagerMu.Unlock() |
| 210 | + |
| 211 | + m.handshakeTimeout = handshakeTimeout |
| 212 | +} |
| 213 | + |
| 214 | +// SetSendTimeout sets the send timeout. |
| 215 | +func (m *TimeoutManager) SetSendTimeout(timeout time.Duration) { |
| 216 | + m.timeoutManagerMu.Lock() |
| 217 | + defer m.timeoutManagerMu.Unlock() |
| 218 | + |
| 219 | + m.sendTimeout = timeout |
| 220 | +} |
| 221 | + |
| 222 | +// SetRecvTimeout sets the receive timeout. |
| 223 | +func (m *TimeoutManager) SetRecvTimeout(timeout time.Duration) { |
| 224 | + m.timeoutManagerMu.Lock() |
| 225 | + defer m.timeoutManagerMu.Unlock() |
| 226 | + |
| 227 | + m.recvTimeout = timeout |
| 228 | +} |
0 commit comments