-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWorker.h
50 lines (44 loc) · 1.42 KB
/
Worker.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#ifndef TOKENWIZARD_WORKER_H
#define TOKENWIZARD_WORKER_H
#include <thread>
#include <queue>
#include "BaseHeader.h"
#include "TokenRecord.h"
#include "InvalidateTask.h"
using namespace boost::asio;
class Worker {
boost::asio::io_service::work* gcWork;
boost::posix_time::seconds interval;
boost::asio::deadline_timer* timer;
time_t currTime;
void gcTick() {
currTime = time(0);
size_t currSize = removeQueue.size();
for(size_t i = 0; i < currSize; ++i) {
InvalidateTask<TOKEN_LENGTH>& task = removeQueue.front();
if(task.deleteTime < currTime)
removeQueue.pop();
else
break;
}
timer->expires_at(timer->expires_at() + interval);
timer->async_wait(std::bind(&Worker::gcTick, this));
}
public:
void pushRemoveTask(TokenString<TOKEN_LENGTH>* tokenString, size_t lifeTime) {
removeQueue.emplace(tokenString, lifeTime);
}
queue<InvalidateTask<TOKEN_LENGTH>> removeQueue;
io_service* workerService;
Worker(): interval(1) {
currTime = time(0);
workerService = new io_service();
gcWork = new io_service::work(*workerService);
timer = new deadline_timer(*workerService, interval);
timer->async_wait(std::bind(&Worker::gcTick, this));
thread([this]() {
workerService->run();
}).detach();
}
};
#endif //TOKENWIZARD_WORKER_H