MCPcopy Create free account
hub / github.com/ambianic/peerfetch / _sendNextRequest

Method _sendNextRequest

javascript/src/peerfetch.ts:528–557  ·  view source on GitHub ↗

Send next pending request to remote peer. Requests are sent one at a time. Only when a previous request response arrives is the next request sent across the wire. In the future we can look at handling multiple requests and responses in parallel over the same data connection or

()

Source from the content-addressed store, hash-verified

526 even a pool of connections.
527 */
528 _sendNextRequest () {
529 const ticket = this._nextTicketInLine
530 let { request, requestSent } = this._requestMap.get(ticket)
531 if (requestSent) {
532 // A request was sent and is waiting its response.
533 // Wait for the full respones before sending another request.
534 return
535 } else {
536 requestSent = true
537 this._requestMap.set(ticket, { request, requestSent })
538 }
539 console.assert(request, 'request cannot be null or empty', { ticket, request })
540 const jsonRequest = JSON.stringify(request)
541 const requestMap = this._requestMap
542 const dataConnection = this._dataConnection
543 console.debug('PeerFetch: Sending request to remote peer',
544 { requestMap, ticket, request, dataConnection })
545 try {
546 if (this._dataConnection) {
547 this._dataConnection.send(jsonRequest)
548 } else {
549 const msg = 'No Peer DataConnection available!'
550 console.error(msg)
551 throw new Error(msg)
552 }
553 console.debug('PeerFetch: Request sent to remote peer: ', jsonRequest)
554 } catch (error) {
555 console.error('PeerFetch: Error sending message via Peer DataConnection', { error })
556 }
557 }
558
559 _processNextTicketInLine () {
560 // check if there is a pending ticket

Callers 2

_enqueueRequestMethod · 0.95

Calls 1

getMethod · 0.80

Tested by

no test coverage detected