-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feat/message-retry' into 'dev'
feat(rosenet-node): implement message retry Closes #67 See merge request ergo/rosen-bridge/rosenet!34
- Loading branch information
Showing
9 changed files
with
242 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@rosen-bridge/rosenet-node": major | ||
--- | ||
|
||
Retry failed direct messages |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
packages/rosenet-node/lib/rosenet-direct/handleIncomingMessage.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { pipe } from 'it-pipe'; | ||
|
||
import { Libp2p } from '@libp2p/interface'; | ||
|
||
import RoseNetNodeContext from '../context/RoseNetNodeContext'; | ||
|
||
import { decode } from '../utils/codec'; | ||
|
||
import { ACK_BYTE, ROSENET_DIRECT_PROTOCOL_V1 } from '../constants'; | ||
|
||
/** | ||
* protocol handler for RoseNet direct | ||
*/ | ||
const handleIncomingMessageFactory = | ||
(node: Libp2p) => (handler: (from: string, message?: string) => void) => { | ||
node.handle( | ||
ROSENET_DIRECT_PROTOCOL_V1, | ||
async ({ connection, stream }) => { | ||
RoseNetNodeContext.logger.debug( | ||
`incoming connection stream with protocol ${ROSENET_DIRECT_PROTOCOL_V1}`, | ||
{ | ||
remoteAddress: connection.remoteAddr.toString(), | ||
transient: connection.transient, | ||
}, | ||
); | ||
pipe( | ||
stream, | ||
decode, | ||
async function* (source) { | ||
for await (const message of source) { | ||
RoseNetNodeContext.logger.debug( | ||
'message received, calling handler and sending ack', | ||
{ | ||
message, | ||
}, | ||
); | ||
handler(connection.remotePeer.toString(), message); | ||
yield Uint8Array.of(ACK_BYTE); | ||
} | ||
}, | ||
stream, | ||
); | ||
}, | ||
{ runOnTransientConnection: true }, | ||
); | ||
RoseNetNodeContext.logger.debug( | ||
`handler for ${ROSENET_DIRECT_PROTOCOL_V1} protocol set`, | ||
); | ||
}; | ||
|
||
export default handleIncomingMessageFactory; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { default as handleIncomingMessageFactory } from './handleIncomingMessage'; | ||
export { default as sendMessageFactory } from './sendMessage'; |
135 changes: 135 additions & 0 deletions
135
packages/rosenet-node/lib/rosenet-direct/sendMessage.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import { Libp2p } from '@libp2p/interface'; | ||
|
||
import { ExponentialBackoff, handleAll, retry } from 'cockatiel'; | ||
import first from 'it-first'; | ||
import map from 'it-map'; | ||
import { pipe } from 'it-pipe'; | ||
import { AbortError, raceSignal } from 'race-signal'; | ||
|
||
import RoseNetNodeContext from '../context/RoseNetNodeContext'; | ||
import streamService from '../stream/stream-service'; | ||
import { encode } from '../utils/codec'; | ||
|
||
import RoseNetDirectAckError, { | ||
AckError, | ||
} from '../errors/RoseNetDirectAckError'; | ||
import RoseNetNodeError from '../errors/RoseNetNodeError'; | ||
|
||
import { | ||
ACK_BYTE, | ||
ACK_TIMEOUT, | ||
MESSAGE_RETRY_ATTEMPTS, | ||
MESSAGE_RETRY_EXPONENT, | ||
MESSAGE_RETRY_INITIAL_DELAY, | ||
} from '../constants'; | ||
|
||
/** | ||
* A factory returning a function to send a message to a specific peer via | ||
* RoseNet direct protocol | ||
*/ | ||
const sendMessageFactory = | ||
(node: Libp2p) => async (to: string, message: string) => { | ||
let stream; | ||
|
||
try { | ||
stream = await streamService.getRoseNetDirectStreamTo(to, node); | ||
|
||
const result = await pipe( | ||
[message], | ||
(source) => map(source, encode), | ||
stream, | ||
async (source) => | ||
await raceSignal(first(source), AbortSignal.timeout(ACK_TIMEOUT)), | ||
); | ||
|
||
if (result?.length !== 1) { | ||
throw new RoseNetDirectAckError( | ||
`There are more than one chunk in the ack message`, | ||
AckError.InvalidChunks, | ||
); | ||
} | ||
const ack = result?.subarray(); | ||
if (ack.length !== 1 || ack[0] !== ACK_BYTE) { | ||
throw new RoseNetDirectAckError( | ||
`Ack byte is invalid`, | ||
AckError.InvalidByte, | ||
); | ||
} | ||
|
||
RoseNetNodeContext.logger.debug('message sent successfully', { | ||
message, | ||
}); | ||
} catch (error) { | ||
if (error instanceof AbortError) { | ||
const errorToThrow = new RoseNetDirectAckError( | ||
`Ack was not received`, | ||
AckError.Timeout, | ||
); | ||
stream?.abort(errorToThrow); | ||
throw errorToThrow; | ||
} | ||
if (error instanceof RoseNetDirectAckError) { | ||
stream?.abort(error); | ||
throw error; | ||
} | ||
const errorToThrow = new RoseNetNodeError( | ||
`An unknown error occured: ${error instanceof Error ? error.message : error}`, | ||
); | ||
stream?.abort(errorToThrow); | ||
throw error; | ||
} | ||
}; | ||
|
||
/** | ||
* A wrapper around `sendMessageFactory` for retrying failed messages | ||
*/ | ||
const sendMessageWithRetryFactory = | ||
(node: Libp2p) => | ||
async ( | ||
to: string, | ||
message: string, | ||
/** | ||
* an optional callback that is called with an error if the message sending | ||
* fails after enough retrials, and with no arguments otherwise | ||
*/ | ||
onSettled?: (error?: Error) => Promise<void>, | ||
) => { | ||
const sendMessageInner = sendMessageFactory(node); | ||
try { | ||
const retryPolicy = retry(handleAll, { | ||
maxAttempts: MESSAGE_RETRY_ATTEMPTS, | ||
backoff: new ExponentialBackoff({ | ||
exponent: MESSAGE_RETRY_EXPONENT, | ||
initialDelay: MESSAGE_RETRY_INITIAL_DELAY, | ||
maxDelay: 300_000, | ||
}), | ||
}); | ||
retryPolicy.onFailure((data) => { | ||
RoseNetNodeContext.logger.debug('message sending failed', { | ||
message, | ||
reason: data.reason, | ||
}); | ||
}); | ||
retryPolicy.onRetry((data) => { | ||
RoseNetNodeContext.logger.debug( | ||
`retry sending message (attempt #${data.attempt}/${MESSAGE_RETRY_ATTEMPTS})`, | ||
{ | ||
message, | ||
}, | ||
); | ||
}); | ||
|
||
await retryPolicy.execute(() => sendMessageInner(to, message)); | ||
onSettled?.(); | ||
} catch (error) { | ||
RoseNetNodeContext.logger.error( | ||
'message sending failed regardless of 3 retries, dropping message', | ||
); | ||
RoseNetNodeContext.logger.debug('message was: ', { | ||
message, | ||
}); | ||
onSettled?.(new RoseNetNodeError('Message sending failed')); | ||
} | ||
}; | ||
|
||
export default sendMessageWithRetryFactory; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { default as publishFactory } from './publish'; | ||
export { default as subscribeFactory } from './subscribe'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Libp2p, PubSub } from '@libp2p/interface'; | ||
|
||
const textEncoder = new TextEncoder(); | ||
|
||
/** | ||
* factory for libp2p publish | ||
*/ | ||
const publishFactory = | ||
(node: Libp2p<{ pubsub: PubSub }>) => | ||
async (topic: string, message: string) => { | ||
node.services.pubsub.publish(topic, textEncoder.encode(message)); | ||
}; | ||
|
||
export default publishFactory; |
Oops, something went wrong.