MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / serveClientsBlockedOnSortedSetKey

Function serveClientsBlockedOnSortedSetKey

src/blocked.cpp:350–395  ·  view source on GitHub ↗

Helper function for handleClientsBlockedOnKeys(). This function is called * when there may be clients blocked on a sorted set key, and there may be new * data to fetch (the key is ready). */

Source from the content-addressed store, hash-verified

348 * when there may be clients blocked on a sorted set key, and there may be new
349 * data to fetch (the key is ready). */
350void serveClientsBlockedOnSortedSetKey(robj *o, readyList *rl) {
351 /* We serve clients in the same order they blocked for
352 * this key, from the first blocked to the last. */
353 dictEntry *de = dictFind(rl->db->blocking_keys,rl->key);
354 if (de) {
355 list *clients = (list*)dictGetVal(de);
356 int numclients = listLength(clients);
357 unsigned long zcard = zsetLength(o);
358
359 while(numclients-- && zcard) {
360 listNode *clientnode = listFirst(clients);
361 client *receiver = (client*)clientnode->value;
362 std::unique_lock<decltype(receiver->lock)> lock(receiver->lock);
363
364 if (receiver->btype != BLOCKED_ZSET) {
365 /* Put at the tail, so that at the next call
366 * we'll not run into it again. */
367 listRotateHeadToTail(clients);
368 continue;
369 }
370
371 int where = (receiver->lastcmd &&
372 receiver->lastcmd->proc == bzpopminCommand)
373 ? ZSET_MIN : ZSET_MAX;
374 monotime replyTimer;
375 elapsedStart(&replyTimer);
376 genericZpopCommand(receiver,&rl->key,1,where,1,NULL);
377 updateStatsOnUnblock(receiver, 0, elapsedUs(replyTimer));
378 unblockClient(receiver);
379 zcard--;
380
381 /* Replicate the command. */
382 robj *argv[2];
383 struct redisCommand *cmd = where == ZSET_MIN ?
384 cserver.zpopminCommand :
385 cserver.zpopmaxCommand;
386 argv[0] = createStringObject(cmd->name,strlen(cmd->name));
387 argv[1] = rl->key;
388 incrRefCount(rl->key);
389 propagate(cmd,receiver->db->id,
390 argv,2,PROPAGATE_AOF|PROPAGATE_REPL);
391 decrRefCount(argv[0]);
392 decrRefCount(argv[1]);
393 }
394 }
395}
396
397/* Helper function for handleClientsBlockedOnKeys(). This function is called
398 * when there may be clients blocked on a stream key, and there may be new

Callers 1

Calls 12

zsetLengthFunction · 0.85
listRotateHeadToTailFunction · 0.85
elapsedStartFunction · 0.85
genericZpopCommandFunction · 0.85
updateStatsOnUnblockFunction · 0.85
elapsedUsFunction · 0.85
unblockClientFunction · 0.85
incrRefCountFunction · 0.85
propagateFunction · 0.85
decrRefCountFunction · 0.85
dictFindFunction · 0.70
createStringObjectFunction · 0.70

Tested by

no test coverage detected