-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(server): fix response to other node (#240)
* fix converse * unsubscribe * publish * small refactor * fix socket * remove todo * fix * remove changes to api.rest * add benchmark script (#241) * chore(engine): create engine package * base service class in engine * move table to engine * move distributed and logger to engine * move database service * move crypto service * move caching service * move batching * fix * move meta service * fix meta * move migrations * engine class * fix build * move kvs * move global config * fix tests * dispatcher * converse use dispatcher * dispatcher for socket * add more typings * include scope in callback * listen -> subscribe * redis handle callback errors * prevent promise rejection cache * fix * fix for local * send -> publish * data -> payload * fix Dockerfile * fix test Co-authored-by: Laurent Leclerc-Poulin <laurentleclercpoulin@gmail.com>
- Loading branch information
1 parent
2e89f39
commit a54b554
Showing
22 changed files
with
365 additions
and
105 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,17 @@ | ||
#!/bin/bash | ||
|
||
# To run this : | ||
# yarn global add loadtest | ||
|
||
clientId=YOUR_CLIENT_ID | ||
clientToken=YOUR_CLIENT_TOKEN | ||
conversationId=YOUR_CONVERSATION_ID | ||
authorId=YOUR_AUTHOR_ID | ||
|
||
loadtest http://localhost:3100/api/messages \ | ||
-m POST \ | ||
-T application/json \ | ||
-c 64 \ | ||
-P "{\"conversationId\":\"$conversationId\",\"authorId\":\"$authorId\",\"payload\":{\"type\":\"text\",\"text\":\"Hello this is a text message!\"}}" \ | ||
-H "x-bp-messaging-client-id:${clientId}" \ | ||
-H "x-bp-messaging-client-token:${clientToken}" |
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,70 @@ | ||
import { DistributedService } from '..' | ||
|
||
const SCOPE_SEPERATOR = '::' | ||
|
||
export class Dispatcher<T extends { [key: number]: any }> { | ||
private name!: string | ||
private distributed!: DistributedService | ||
|
||
private listeners: { [eventId: number]: ((scope: string, arg: any) => Promise<void>)[] } = {} | ||
private handleDispatchCallback!: (msg: any, channel: string) => Promise<void> | ||
|
||
async setup(name: string, distributed: DistributedService) { | ||
this.name = name | ||
this.distributed = distributed | ||
this.handleDispatchCallback = this.handleDispatch.bind(this) | ||
} | ||
|
||
async subscribe(scope: string) { | ||
await this.distributed.subscribe(this.getChannel(scope), this.handleDispatchCallback) | ||
} | ||
|
||
async unsubscribe(scope: string) { | ||
await this.distributed.unsubscribe(this.getChannel(scope)) | ||
} | ||
|
||
async publish<K extends keyof T>(event: K, scope: string, arg: T[K]) { | ||
await this.emit(event, scope, arg) | ||
await this.distributed.publish(this.getChannel(scope), { | ||
cmd: event, | ||
data: arg | ||
}) | ||
} | ||
|
||
private getChannel(scope: string) { | ||
return `${this.name}${SCOPE_SEPERATOR}${scope}` | ||
} | ||
|
||
private async handleDispatch<K extends keyof T>({ cmd, data }: { cmd: K; data: T[K] }, channel: string) { | ||
const scopeStart = channel.lastIndexOf(SCOPE_SEPERATOR) | ||
const scope = channel.substr(scopeStart + SCOPE_SEPERATOR.length) | ||
await this.emit(cmd, scope, data) | ||
} | ||
|
||
public on<K extends keyof T>( | ||
event: K, | ||
listener: (scope: string, arg: T[K]) => Promise<void>, | ||
pushBack: boolean = false | ||
) { | ||
const listeners = this.listeners[event as number] | ||
if (!listeners) { | ||
this.listeners[event as number] = [listener] | ||
} else if (!pushBack) { | ||
listeners.push(listener) | ||
} else { | ||
listeners.unshift(listener) | ||
} | ||
} | ||
|
||
private async emit<K extends keyof T>(event: K, scope: string, arg: T[K]): Promise<boolean> { | ||
const listeners = this.listeners[event as number] | ||
if (listeners?.length) { | ||
for (const listener of listeners) { | ||
await listener(scope, arg) | ||
} | ||
return true | ||
} else { | ||
return false | ||
} | ||
} | ||
} |
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,16 @@ | ||
import { Dispatcher, DistributedService } from '..' | ||
import { Service } from '../base/service' | ||
|
||
export class DispatchService extends Service { | ||
constructor(private distributed: DistributedService) { | ||
super() | ||
} | ||
|
||
async setup() {} | ||
|
||
async create<T extends Dispatcher<any>>(name: string, t: new () => T): Promise<T> { | ||
const dispatcher = new t() | ||
await dispatcher.setup(name, this.distributed) | ||
return dispatcher | ||
} | ||
} |
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
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,20 @@ | ||
import { Message, uuid } from '@botpress/messaging-base' | ||
import { Dispatcher } from '@botpress/messaging-engine' | ||
|
||
export enum ConverseDispatches { | ||
Message, | ||
Stop | ||
} | ||
|
||
export interface ConverseMessageDispatch { | ||
message: Message | ||
} | ||
|
||
export interface ConverseStopDispatch { | ||
conversationId: uuid | ||
} | ||
|
||
export class ConverseDispatcher extends Dispatcher<{ | ||
[ConverseDispatches.Message]: ConverseMessageDispatch | ||
[ConverseDispatches.Stop]: ConverseStopDispatch | ||
}> {} |
Oops, something went wrong.