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

Function acceptOnThread

src/networking.cpp:1339–1387  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1337}
1338
1339void acceptOnThread(connection *conn, int flags, char *cip)
1340{
1341 int ielCur = ielFromEventLoop(serverTL->el);
1342 bool fBootLoad = (g_pserver->loading == LOADING_BOOT);
1343
1344 int ielTarget = ielCur;
1345 if (fBootLoad)
1346 {
1347 ielTarget = IDX_EVENT_LOOP_MAIN; // During load only the main thread is active
1348 }
1349 else if (g_fTestMode)
1350 {
1351 // On test mode we don't want any bunching of clients
1352 while (cserver.cthreads > 1 && ielTarget == IDX_EVENT_LOOP_MAIN)
1353 ielTarget = rand() % cserver.cthreads;
1354 }
1355 else if (g_pserver->active_client_balancing)
1356 {
1357 // Cluster connections are more transient, so its not worth the cost to balance
1358 // we can trust that SO_REUSEPORT is doing its job of distributing connections
1359 ielTarget = g_pserver->cluster_enabled ? ielCur : chooseBestThreadForAccept();
1360 }
1361
1362 rgacceptsInFlight[ielTarget].fetch_add(1, std::memory_order_relaxed);
1363 if (ielTarget != ielCur)
1364 {
1365 char *szT = nullptr;
1366 if (cip != nullptr)
1367 {
1368 szT = (char*)zmalloc(NET_IP_STR_LEN, MALLOC_LOCAL);
1369 memcpy(szT, cip, NET_IP_STR_LEN);
1370 }
1371 int res = aePostFunction(g_pserver->rgthreadvar[ielTarget].el, [conn, flags, ielTarget, szT] {
1372 connMarshalThread(conn);
1373 acceptCommonHandler(conn,flags,szT,ielTarget);
1374 rgacceptsInFlight[ielTarget].fetch_sub(1, std::memory_order_relaxed);
1375 zfree(szT);
1376 });
1377
1378 if (res == AE_OK)
1379 return;
1380 // If res != AE_OK we can still try to accept on the local thread
1381 }
1382 rgacceptsInFlight[ielTarget].fetch_sub(1, std::memory_order_relaxed);
1383
1384 aeAcquireLock();
1385 acceptCommonHandler(conn,flags,cip,ielCur);
1386 aeReleaseLock();
1387}
1388
1389void acceptTcpHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
1390 int cport, cfd, max = MAX_ACCEPTS_PER_CALL;

Callers 3

acceptTcpHandlerFunction · 0.85
acceptTLSHandlerFunction · 0.85
acceptUnixHandlerFunction · 0.85

Calls 9

ielFromEventLoopFunction · 0.85
zmallocFunction · 0.85
aePostFunctionFunction · 0.85
connMarshalThreadFunction · 0.85
acceptCommonHandlerFunction · 0.85
zfreeFunction · 0.85
aeAcquireLockFunction · 0.85
aeReleaseLockFunction · 0.85

Tested by

no test coverage detected