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

Function acceptCommonHandler

src/networking.cpp:1248–1337  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1246#define MAX_ACCEPTS_PER_CALL_TLS 100
1247
1248static void acceptCommonHandler(connection *conn, int flags, char *ip, int iel) {
1249 client *c;
1250 char conninfo[100];
1251 UNUSED(ip);
1252 AeLocker locker;
1253 locker.arm(nullptr);
1254
1255 if (connGetState(conn) != CONN_STATE_ACCEPTING) {
1256 serverLog(LL_VERBOSE,
1257 "Accepted client connection in error state: %s (conn: %s)",
1258 connGetLastError(conn),
1259 connGetInfo(conn, conninfo, sizeof(conninfo)));
1260 connClose(conn);
1261 return;
1262 }
1263
1264 /* Prevent new connections if we're in a soft shutdown situation */
1265 if (g_pserver->soft_shutdown) {
1266 const char *err = "-SHUTDOWN";
1267 /* That's a best effort error message, don't check write errors.
1268 * Note that for TLS connections, no handshake was done yet so nothing
1269 * is written and the connection will just drop. */
1270 if (connWrite(conn,err,strlen(err)) == -1) {
1271 /* Nothing to do, Just to avoid the warning... */
1272 }
1273 g_pserver->stat_rejected_conn++;
1274 connClose(conn);
1275 return;
1276 }
1277
1278 /* Limit the number of connections we take at the same time.
1279 *
1280 * Admission control will happen before a client is created and connAccept()
1281 * called, because we don't want to even start transport-level negotiation
1282 * if rejected. */
1283 if (listLength(g_pserver->clients) + getClusterConnectionsCount()
1284 >= (g_pserver->maxclients - g_pserver->maxclientsReserved))
1285 {
1286 // Allow the connection if it comes from localhost and we're within the maxclientReserved buffer range
1287 if ((listLength(g_pserver->clients) + getClusterConnectionsCount()) >= g_pserver->maxclients || strcmp("127.0.0.1", ip)) {
1288 const char *err;
1289 if (g_pserver->cluster_enabled)
1290 err = "-ERR max number of clients + cluster "
1291 "connections reached\r\n";
1292 else
1293 err = "-ERR max number of clients reached\r\n";
1294
1295 /* That's a best effort error message, don't check write errors.
1296 * Note that for TLS connections, no handshake was done yet so nothing
1297 * is written and the connection will just drop. */
1298 if (connWrite(conn,err,strlen(err)) == -1) {
1299 /* Nothing to do, Just to avoid the warning... */
1300 }
1301 g_pserver->stat_rejected_conn++;
1302 connClose(conn);
1303 return;
1304 }
1305 }

Callers 1

acceptOnThreadFunction · 0.85

Calls 12

connGetStateFunction · 0.85
serverLogFunction · 0.85
connGetLastErrorFunction · 0.85
connGetInfoFunction · 0.85
connCloseFunction · 0.85
connWriteFunction · 0.85
connAcceptFunction · 0.85
connGetPrivateDataFunction · 0.85
armMethod · 0.80
createClientFunction · 0.70
freeClientFunction · 0.70

Tested by

no test coverage detected