-
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.
feat(rosenet-node): add bulkheads to pubsub APIs
- Loading branch information
1 parent
e998cb1
commit 2d87139
Showing
4 changed files
with
68 additions
and
12 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': minor | ||
--- | ||
|
||
Add pubsub limits |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,39 @@ | ||
import { Libp2p, PubSub } from '@libp2p/interface'; | ||
import { bulkhead, isBulkheadRejectedError } from 'cockatiel'; | ||
|
||
import RoseNetNodeContext from '../context/RoseNetNodeContext'; | ||
|
||
import { | ||
MAX_OUTBOUND_PUBSUB_QUEUE_SIZE, | ||
MAX_OUTBOUND_PUBSUB_THROUGHPUT, | ||
} from '../constants'; | ||
|
||
const textEncoder = new TextEncoder(); | ||
|
||
const bulkheadPolicy = bulkhead( | ||
MAX_OUTBOUND_PUBSUB_THROUGHPUT, | ||
MAX_OUTBOUND_PUBSUB_QUEUE_SIZE, | ||
); | ||
|
||
/** | ||
* factory for libp2p publish | ||
*/ | ||
const publishFactory = | ||
(node: Libp2p<{ pubsub: PubSub }>) => | ||
async (topic: string, message: string) => { | ||
node.services.pubsub.publish(topic, textEncoder.encode(message)); | ||
try { | ||
await bulkheadPolicy.execute(() => | ||
node.services.pubsub.publish(topic, textEncoder.encode(message)), | ||
); | ||
} catch (error) { | ||
if (isBulkheadRejectedError(error)) { | ||
RoseNetNodeContext.logger.warn('Maximum publish threshold reached'); | ||
} else { | ||
RoseNetNodeContext.logger.warn('Message publish failed', { | ||
message, | ||
}); | ||
} | ||
} | ||
}; | ||
|
||
export default publishFactory; |
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