-
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: add multiple servers scaling, reconnect gracefully
- Loading branch information
1 parent
a04911b
commit edecc1b
Showing
13 changed files
with
115 additions
and
152 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
This file was deleted.
Oops, something went wrong.
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
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
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import EventEmitter from 'events'; | ||
import { MessageDistributor } from '.'; | ||
const decoder = new TextDecoder(); | ||
|
||
export class InMemoryMessageDistributor implements MessageDistributor { | ||
initialized?: boolean; | ||
list: Map<string, Set<string>> = new Map(); | ||
keyStore: Map<string, string> = new Map(); | ||
eventEmitter = new EventEmitter(); | ||
constructor() {} | ||
async initialize() { | ||
this.initialized = true; | ||
} | ||
|
||
async addListItem(listId: string, item: string) { | ||
if (this.list.has(listId)) this.list.get(listId).add(item); | ||
else this.list.set(listId, new Set([item])); | ||
} | ||
async getListItems(listId: string) { | ||
return this.list.get(listId) || []; | ||
} | ||
async removeListItem(listId: string, item: string) { | ||
return this.list.get(listId)?.delete(item); | ||
} | ||
|
||
async set(key: string, value: string) { | ||
return this.keyStore.set(key, value); | ||
} | ||
async get(key: string) { | ||
return this.keyStore.get(key); | ||
} | ||
async enqueue(queueId: string, message: Uint8Array) { | ||
this.eventEmitter.emit(queueId, message); | ||
} | ||
async listen(channel: string, callback: (_: string, _s: Uint8Array) => void) { | ||
this.eventEmitter.on(channel, (message) => { | ||
const finalMessage = new Uint8Array(message); | ||
const groupLength = finalMessage[0]; | ||
const id = decoder.decode(finalMessage.subarray(1, 1 + groupLength)); | ||
|
||
const remaining = finalMessage.subarray(1 + groupLength, finalMessage.length); | ||
|
||
callback(id, remaining); | ||
}); | ||
} | ||
} |
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,12 @@ | ||
export interface MessageDistributor { | ||
initialize: (serverId: string) => Promise<void>; | ||
listen: (queueId: string, callback: (receiverId: string, message: Uint8Array) => void) => void; | ||
enqueue: (queueId: string, message: Uint8Array) => void; | ||
addListItem: (listId: string, item: string) => Promise<any>; | ||
getListItems: (listId: string) => Promise<Iterable<string>>; | ||
set: (key: string, value: string) => Promise<any>; | ||
get: (key: string) => Promise<string>; | ||
} | ||
|
||
export { InMemoryMessageDistributor } from './inMemory'; | ||
export { RedisMessageDistributor } from './redis'; |
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
Oops, something went wrong.