Skip to content

Commit

Permalink
handling edge case where socket.read throws error due to exceeding Hi…
Browse files Browse the repository at this point in the history
…ghWatermark.
  • Loading branch information
pieterjan84 committed Feb 23, 2024
1 parent 808cecc commit 78d2fa2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
22 changes: 17 additions & 5 deletions examples/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,23 @@ function connect() {
' for ledger ' +
stellarMessage.envelope().statement().slotIndex().toString()
);
/*if(stellarMessage.envelope().statement().pledges().switch() === xdr.ScpStatementType.scpStExternalize()){
const value = stellarMessage.envelope().statement().pledges().externalize().commit().value();
const closeTime = xdr.StellarValue.fromXDR(value).closeTime().toXDR().readBigUInt64BE();
console.log(new Date(1000 * Number(closeTime)));
}*/
if (
stellarMessage.envelope().statement().pledges().switch() ===
xdr.ScpStatementType.scpStExternalize()
) {
const value = stellarMessage
.envelope()
.statement()
.pledges()
.externalize()
.commit()
.value();
const closeTime = xdr.StellarValue.fromXDR(value)
.closeTime()
.toXDR()
.readBigUInt64BE();
//console.log(new Date(1000 * Number(closeTime)));
}
break;
default:
console.log(
Expand Down
19 changes: 17 additions & 2 deletions src/connection/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,23 @@ export class Connection extends Duplex {
}

protected processNextMessage(): Result<boolean, Error> {
//If size bytes are not available to be read, null will be returned unless the stream has ended, in which case all of the data remaining in the internal buffer will be returned.
const data = this.socket.read(this.lengthNextMessage);
//If size bytes are not available to be read,
// null will be returned unless the stream has ended,
// in which case all the data remaining in the internal buffer will be returned.
let data: Buffer | null = null;
try {
data = this.socket.read(this.lengthNextMessage);
} catch (e) {
this.logger.error(
{
remote: this.remoteAddress,
error: mapUnknownToError(e).message,
length: this.lengthNextMessage
},
'Error reading from socket'
);
return err(mapUnknownToError(e));
}
if (!data || data.length !== this.lengthNextMessage) {
this.logger.trace(
{
Expand Down

0 comments on commit 78d2fa2

Please sign in to comment.