This function gets called every time Redis is entering the * main loop of the event driven library, that is, before to sleep * for ready file descriptors. * * Note: This function is (currently) called from two functions: * 1. aeMain - The main server loop * 2. processEventsWhileBlocked - Process clients during RDB/AOF load * * If it was called from processEventsWhileBlocked we don't want
| 2356 | * The most important is freeClientsInAsyncFreeQueue but we also |
| 2357 | * call some other low-risk functions. */ |
| 2358 | void beforeSleep(struct aeEventLoop *eventLoop) { |
| 2359 | UNUSED(eventLoop); |
| 2360 | |
| 2361 | size_t zmalloc_used = zmalloc_used_memory(); |
| 2362 | if (zmalloc_used > server.stat_peak_memory) |
| 2363 | server.stat_peak_memory = zmalloc_used; |
| 2364 | |
| 2365 | /* Just call a subset of vital functions in case we are re-entering |
| 2366 | * the event loop from processEventsWhileBlocked(). Note that in this |
| 2367 | * case we keep track of the number of events we are processing, since |
| 2368 | * processEventsWhileBlocked() wants to stop ASAP if there are no longer |
| 2369 | * events to handle. */ |
| 2370 | if (ProcessingEventsWhileBlocked) { |
| 2371 | uint64_t processed = 0; |
| 2372 | processed += handleClientsWithPendingReadsUsingThreads(); |
| 2373 | processed += tlsProcessPendingData(); |
| 2374 | processed += handleClientsWithPendingWrites(); |
| 2375 | processed += freeClientsInAsyncFreeQueue(); |
| 2376 | server.events_processed_while_blocked += processed; |
| 2377 | return; |
| 2378 | } |
| 2379 | |
| 2380 | /* Handle precise timeouts of blocked clients. */ |
| 2381 | handleBlockedClientsTimeout(); |
| 2382 | |
| 2383 | /* We should handle pending reads clients ASAP after event loop. */ |
| 2384 | handleClientsWithPendingReadsUsingThreads(); |
| 2385 | |
| 2386 | /* Handle TLS pending data. (must be done before flushAppendOnlyFile) */ |
| 2387 | tlsProcessPendingData(); |
| 2388 | |
| 2389 | /* If tls still has pending unread data don't sleep at all. */ |
| 2390 | aeSetDontWait(server.el, tlsHasPendingData()); |
| 2391 | |
| 2392 | /* Call the Redis Cluster before sleep function. Note that this function |
| 2393 | * may change the state of Redis Cluster (from ok to fail or vice versa), |
| 2394 | * so it's a good idea to call it before serving the unblocked clients |
| 2395 | * later in this function. */ |
| 2396 | if (server.cluster_enabled) clusterBeforeSleep(); |
| 2397 | |
| 2398 | /* Run a fast expire cycle (the called function will return |
| 2399 | * ASAP if a fast cycle is not needed). */ |
| 2400 | if (server.active_expire_enabled && server.masterhost == NULL) |
| 2401 | activeExpireCycle(ACTIVE_EXPIRE_CYCLE_FAST); |
| 2402 | |
| 2403 | /* Unblock all the clients blocked for synchronous replication |
| 2404 | * in WAIT. */ |
| 2405 | if (listLength(server.clients_waiting_acks)) |
| 2406 | processClientsWaitingReplicas(); |
| 2407 | |
| 2408 | /* Check if there are clients unblocked by modules that implement |
| 2409 | * blocking commands. */ |
| 2410 | if (moduleCount()) moduleHandleBlockedClients(); |
| 2411 | |
| 2412 | /* Try to process pending commands for clients that were just unblocked. */ |
| 2413 | if (listLength(server.unblocked_clients)) |
| 2414 | processUnblockedClients(); |
| 2415 |
nothing calls this directly
no test coverage detected