【问题标题】:Buffer of requests请求缓冲区
【发布时间】:2020-06-09 18:35:45
【问题描述】:

我使用 Node 和 ExpressJS 开发了一个 REST-API,我需要创建一个临时缓冲区来比较 X 秒内请求接收到的数据。

例如,服务器接收到请求,将其存储在缓冲区中并启动计时器。虽然计时器没有完成,但所有请求数据也将存储在该缓冲区中。计时器完成后,我需要比较缓冲区内的所有数据,然后将其发送到数据库。

我的问题是我不知道是否可以使用 node 和 express 来实现,我搜索了但我没有找到任何可能的解决方案。也许有人可以帮助我解决我的问题。谢谢。

【问题讨论】:

  • 您的意思是您想要构建某种在特定时间内处于活动状态的请求记录功能?如何触发计时器?例如,每 5 分钟 1 分钟?还是根据特定要求?
  • 那是朋友,但我需要在服务器收到请求并且缓冲区为空时触发计时器。 X 秒后,此缓冲区将用于比较,然后再次清空

标签: javascript node.js api express buffer


【解决方案1】:

我确信有库可以做到这一点,但如果你想自己实现它,你可以这样做:

您可以编写一个小 Request-Recorder 类,该类公开一个recordData 方法,如果记录器当前处于活动状态或尚未记录任何数据,则该方法允许您记录请求数据。如果后一种情况为真,则启用计时器并让它记录数据,直到达到超时。这样的事情应该可以帮助您入门:

class RequestDataRecorder {
    static instance;

    constructor() {
        this.isActive = false;
        this.isLocked = false;
        // could also be a map, holding request data by request-id for example
        this.recordedData = [];
        this.recordDataDurationInSeconds = 10; // will capture request data within 10 second time frames
    }

    static shared() {
        if (!RequestDataRecorder.instance) {
            RequestDataRecorder.instance = new RequestDataRecorder();
        }
        return RequestDataRecorder.instance;
    }

    recordData(data) {
        if (this.canActivate()) {
            this.activate();
        }
        if (this.isCurrentlyActive()) {
            this.recordedData.push(data);
        }
    }

    canActivate() {
        return !this.isLocked && !this.isActive && this.recordedData.length === 0;
    }

    activate() {
        this.isLocked = true;
        this.timer = setTimeout(() => {
                this.deactivate();
                this.exportData();
            }, this.recordDataDurationInSeconds * 1000);
        this.isLocked = false;
        this.setActive(true);
    }

    deactivate() {
        this.isLocked = true;
        this.setActive(false);
        clearTimeout(this.timer);
        this.recordedData = [];
        this.isLocked = false;
    }

    setActive(val) {
        this.isActive = val;
    }

    isCurrentlyActive() {
        return this.isActive;
    }

    exportData() {
        // do your db-export here or use another class to to the export with this data (preferably the latter to comply with the single-responsibilty principle :))
        return this.recordedData;
    }

}

你可以像这样使用这个类:

const express = require('express');
const bodyParser = require('body-parser');
const app = express();

app.use(bodyParser.json());
const requestDataRecorder = RequestDataRecorder.shared();

app.post("/postSomeData", (req, res) => {
    requestDataRecorder.recordData(req.body);
    res.status(201).end();
});

app.get("/getRecordedData", (req, res) => {
    res.send(requestDataRecorder.getRecordedData());
});

app.listen(3000, function () {
    console.log("Server is listening!");
});

【讨论】:

  • 哇朋友,非常感谢,这正是我所需要的。
猜你喜欢
  • 2011-02-08
  • 2019-07-22
  • 2021-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-25
  • 2021-08-25
相关资源
最近更新 更多