Skip to content

Commit d205609

Browse files
committed
fix: race condition causing intermittent http over webrtc fetch errors
1 parent a70d921 commit d205609

File tree

1 file changed

+33
-8
lines changed

1 file changed

+33
-8
lines changed

Diff for: src/remote/peer-fetch.js

+33-8
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,14 @@ export class PeerFetch {
5454
and simultaneously increment the ticket counter.
5555
*/
5656
_drawNewTicket () {
57-
return this._nextAvailableTicket++
57+
const nextAvailable = this._nextAvailableTicket
58+
this._nextAvailableTicket++
59+
const nextInLine = this._nextTicketInLine
60+
console.assert(
61+
nextInLine <= nextAvailable,
62+
{ nextInLine, nextAvailable }
63+
)
64+
return nextAvailable
5865
}
5966

6067
/**
@@ -70,6 +77,12 @@ export class PeerFetch {
7077
// remove entry from pending request map
7178
this._requestMap.delete(ticket)
7279
this._nextTicketInLine++
80+
const nextInLine = this._nextTicketInLine
81+
const nextAvailable = this._nextAvailableTicket
82+
console.assert(
83+
nextInLine <= nextAvailable,
84+
{ nextInLine, nextAvailable }
85+
)
7386
}
7487

7588
_configureDataConnection () {
@@ -114,9 +127,11 @@ export class PeerFetch {
114127
}
115128
})
116129
this._dataConnection.on('open', function () {
130+
console.debug('Peer connection is now open.')
117131
peerFetch._schedulePing()
118132
})
119133
this._dataConnection.on('close', function () {
134+
console.debug('Peer connection is now closed.')
120135
peerFetch._stopPing()
121136
})
122137
}
@@ -147,12 +162,13 @@ export class PeerFetch {
147162

148163
_enqueueRequest (request) {
149164
const ticket = this._drawNewTicket()
150-
console.debug(this._requestMap)
165+
const requestMap = this._requestMap
166+
console.debug('_enqueueRequest: ', { requestMap })
151167
this._requestMap.set(ticket, { request })
152168
if (this._requestMap.size === 1) {
153169
// there are no other pending requests
154170
// let's send this one on the wire
155-
this._sendNextRequest(ticket)
171+
this._sendNextRequest()
156172
}
157173
return ticket
158174
}
@@ -167,8 +183,18 @@ export class PeerFetch {
167183
and responses in parallel over the same data connection or
168184
even a pool of connections.
169185
*/
170-
_sendNextRequest (ticket) {
171-
const { request } = this._requestMap.get(ticket)
186+
_sendNextRequest () {
187+
const ticket = this._nextTicketInLine
188+
let { request, requestSent } = this._requestMap.get(ticket)
189+
if (requestSent) {
190+
// A request was sent and is waiting its response.
191+
// Wait for the full respones before sending another request.
192+
return
193+
} else {
194+
requestSent = true
195+
this._requestMap.set(ticket, { request, requestSent })
196+
}
197+
console.assert(request != null, { ticket, request })
172198
const jsonRequest = JSON.stringify(request)
173199
const requestMap = this._requestMap
174200
console.debug('Sending request to remote peer',
@@ -181,19 +207,18 @@ export class PeerFetch {
181207
}
182208

183209
_processNextTicketInLine () {
184-
const ticket = this._nextTicketInLine
185210
// check if there is a pending ticket
186211
// and process it
187212
if (this._pendingRequests()) {
188-
this._sendNextRequest(ticket)
213+
this._sendNextRequest()
189214
}
190215
}
191216

192217
/**
193218
* Check if there are any pending requests waiting in line.
194219
*/
195220
_pendingRequests () {
196-
if (this._nextTicketInLine < this._nextAvailableTicket) {
221+
if (this._requestMap.size > 0) {
197222
return true
198223
}
199224
}

0 commit comments

Comments
 (0)