Skip to content

Commit

Permalink
refactor(cluster): catch publish errors
Browse files Browse the repository at this point in the history
Note: the current API does not currently allow the user to handle those
errors (and retry, for example). This might be worth investigating for
the next major version, maybe something like:

```js
try {
  await io.emit("hello");
} catch (e) {
  // something went wrong
}
```
  • Loading branch information
darrachequesne committed Feb 20, 2024
1 parent 1011ab3 commit f09a1c3
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions lib/cluster-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ export abstract class ClusterAdapter extends Adapter {

if (!onlyLocal) {
try {
const offset = await this.publish({
const offset = await this.publishAndReturnOffset({
type: MessageType.BROADCAST,
data: {
packet,
Expand Down Expand Up @@ -623,7 +623,17 @@ export abstract class ClusterAdapter extends Adapter {
});
}

protected publish(message: DistributiveOmit<ClusterMessage, "nsp" | "uid">) {
protected publish(
message: DistributiveOmit<ClusterMessage, "nsp" | "uid">
): void {
this.publishAndReturnOffset(message).catch((err) => {
debug("error while publishing message: %s", err);
});
}

protected publishAndReturnOffset(
message: DistributiveOmit<ClusterMessage, "nsp" | "uid">
) {
(message as ClusterMessage).uid = this.uid;
(message as ClusterMessage).nsp = this.nsp.name;
return this.doPublish(message as ClusterMessage);
Expand All @@ -644,7 +654,11 @@ export abstract class ClusterAdapter extends Adapter {
) {
(response as ClusterResponse).uid = this.uid;
(response as ClusterResponse).nsp = this.nsp.name;
return this.doPublishResponse(requesterUid, response as ClusterResponse);
this.doPublishResponse(requesterUid, response as ClusterResponse).catch(
(err) => {
debug("error while publishing response: %s", err);
}
);
}

/**
Expand All @@ -657,7 +671,7 @@ export abstract class ClusterAdapter extends Adapter {
protected abstract doPublishResponse(
requesterUid: string,
response: ClusterResponse
);
): Promise<void>;
}

interface CustomClusterRequest {
Expand Down

0 comments on commit f09a1c3

Please sign in to comment.