New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: use sort set to impl queue #277
Conversation
Use sort set to keep queue in order and keep same value only insert once
502b14d
to
3f1a3e4
Compare
*/ | ||
async push<T>(key: string, item: T): Promise<boolean> { | ||
const score = await this.redis.incr(this.getQueueScoreName(key)); | ||
const res = await this.redis.zadd(this.getQueueName(key), score, JSON.stringify(item)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
有什么文章看看是什么原理么
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
除了保证同步任务不重复,后面还是需要考虑真的有并发同步执行的时候,数据更新的原子性,目前可以先忽略。 |
oss 并发写入按道理是不会有问题的。 |
Codecov Report
@@ Coverage Diff @@
## main #277 +/- ##
==========================================
- Coverage 97.43% 97.02% -0.41%
==========================================
Files 119 119
Lines 3661 3665 +4
Branches 330 330
==========================================
- Hits 3567 3556 -11
- Misses 94 109 +15
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
* If queue not has the same item, return true | ||
*/ | ||
async push<T>(key: string, item: T): Promise<boolean> { | ||
const score = await this.redis.incr(this.getQueueScoreName(key)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First, Redis is not a good choice for the task queue. I think it would be better to use MQ or other tools.
Second, maybe you can use use Lua script to keep the command atomic
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MQ is good for queue, but we choice less infrastructure dependency so use the redis.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, lua script is better, we should use it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And infra can be replace with other implement. It's ok to use mq impl the new queue adapter.
According to the redis doc.
Use lua script with Strict order is not necessary for the task queue, it's ok to impl queue with two step. |
1.10.0 |
Actually, the script-losing problem because the client use the cached script. BTW, this PR is LGTM for me if we can assume that we don't need atomic action here. |
Use sort set to keep queue in order and keep same value only insert once