문제 개요

원인 분석

enQueueSend

  async enQueueSend(socket, packet) {
    await this.sendLock.runExclusive(() => {
      this.sendQueue.push({ socket, packet });
      this.processSendPacket();
    });
  }

deQueueSend

  async deQueueSend() {
    return await this.sendLock.runExclusive(() => {
      return this.sendQueue.shift();
    });
  }

processSendPacket

async processSendPacket() {
    if (!this.sendQueue.length) return;
    try {
      while (this.sendQueue.length) {
        const { socket, packet } = await this.deQueueSend();
        if (!socket || !packet) throw new Error('패킷 보내기 오류');
        await socket.write(packet);
      }
    } catch (err) {
      handleErr(null, err);
    }
  }

결론

processSendPacket

  async processSendPacket() {
    if (!this.sendQueue.length || this.sendProcessing) return;
    this.sendProcessing = true;
    try {
      while (this.sendQueue.length) {
        const { socket, packet } = await this.deQueueSend();
        if (!socket || !packet) throw new Error('패킷 보내기 오류');
        await socket.write(packet);
      }
    } catch (err) {
      handleErr(null, err);
    } finally {
      this.sendProcessing = false;
    }
  }

해결