Skip to content

Commit

Permalink
fix: can start multi times (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Aug 24, 2023
1 parent c32523b commit 2d1895a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/server/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { ServerCredentials, Server as GRPCServer } from '@grpc/grpc-js';
import { AppCallbackService } from '../../proto/runtime/v1/appcallback_grpc_pb';
import GRPCServerImpl from './GRPCServerImpl';
import PubSub from './PubSub';
import { sleep } from '../utils';

const debug = debuglog('layotto:server:main');

Expand All @@ -35,6 +34,7 @@ export default class Server {
protected readonly logger: Console;
private readonly _serverImpl: GRPCServerImpl;
private readonly _server: GRPCServer;
#start = false;

constructor(port: string = process.env.appcallback_GRPC_PORT ?? '9999',
GRPCServerInstance?: any,
Expand All @@ -54,9 +54,10 @@ export default class Server {
}

async start(): Promise<void> {
if (this.#start) return;
this.#start = true;
await this._bind();
this._server.start();
await sleep(250);
}

async close(): Promise<void> {
Expand Down
2 changes: 2 additions & 0 deletions test/unit/server/GRPCServerImpl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ describe.skip('server/GRPCServerImpl.test.ts', () => {
const serverImpl = new CustomGRPCServerImpl(pubsubConfig);
server = new Server('9999', serverImpl);
await server.start();
// start multi times should work
await server.start();
});

afterAll(async () => {
Expand Down

0 comments on commit 2d1895a

Please sign in to comment.