commit 40c43e7

uint  ·  2026-01-31 05:15:27 +0000 UTC
parent ce889b9
respond 503 when max_client reached; server busy
1 files changed,  +20, -5
+20, -5
 1@@ -33,6 +33,7 @@
 2 #include "http.h"
 3 #include "log.h"
 4 #include "scan.h"
 5+#include "util.h"
 6 
 7 #ifndef GIT_VER
 8 #define GIT_VER "unknown"
 9@@ -106,20 +107,34 @@ void run(void)
10 	for (;;) {
11 		int c;
12 
13-		/* client cap */
14-		(void)sem_wait(&slots);
15-
16 		c = accept(sock, NULL, NULL);
17 		if (c < 0) {
18-			(void)sem_post(&slots);
19 			if (errno == EINTR)
20 				continue;
21 			continue;
22 		}
23 
24 		fd_set_cloexec(c);
25-		LOG(verbose_log, "CORE", "Connection         Accepted");
26 
27+		/* client cap. if full 503 and close */
28+		if (sem_trywait(&slots) < 0) {
29+			/* short 503 + retry after */
30+			static const char resp[] =
31+				"HTTP/1.1 503 Service Unavailable\r\n"
32+				"Content-Type: text/plain\r\n"
33+				"Retry-After: 1\r\n"
34+				"Content-Length: 5\r\n"
35+				"Connection: close\r\n"
36+				"\r\n"
37+				"busy\n";
38+			(void)write_all(c, resp, sizeof(resp) - 1);
39+
40+			close(c);
41+			LOG(true, "CORE", "Connection         Busy (503)");
42+			continue;
43+		}
44+
45+		LOG(verbose_log, "CORE", "Connection         Accepted");
46 		pthread_t t;
47 		int err = pthread_create(&t, NULL, client_thread, (void*)(intptr_t)c);
48 		if (err != 0) {