@@ -26,31 +26,15 @@ import java.nio.file.Files
26
26
import java.nio.file.attribute.PosixFilePermission
27
27
import kotlin.io.path.absolutePathString
28
28
import kotlin.io.path.setPosixFilePermissions
29
- import kotlin.system.exitProcess
30
-
31
- private const val DEFAULT_MAX_CONNECTIONS = 4
32
29
33
30
private val logger = KotlinLogging .logger {}
34
31
35
- suspend fun main (vararg args : String ) {
36
- if (args.size !in 1 .. 2 ) {
37
- logger.error { " usage: <app> <fast cgi socket> [max connections=$DEFAULT_MAX_CONNECTIONS ]" }
38
- exitProcess(status = 1 )
39
- } else {
40
- val input = args[0 ]
41
- try {
42
- readFromSocket(input, args.getOrNull(1 )?.toInt() ? : DEFAULT_MAX_CONNECTIONS )
43
- } finally {
44
- logger.info { " stop kss process" }
45
- }
46
- }
47
- }
48
-
49
32
suspend fun readFromSocket (socket : String , maxConnections : Int ) {
50
33
logger.info { " start kss process" }
51
34
logger.debug { " reading from $socket with $maxConnections max connections" }
52
35
val socketAddress = UnixDomainSocketAddress .of(socket)
53
36
runInterruptible { Files .deleteIfExists(socketAddress.path) }
37
+ val socketPath = socketAddress.path.absolutePathString()
54
38
55
39
val globalState = GlobalState (maxConnections)
56
40
try {
@@ -64,7 +48,12 @@ suspend fun readFromSocket(socket: String, maxConnections: Int) {
64
48
PosixFilePermission .GROUP_READ , PosixFilePermission .GROUP_WRITE ,
65
49
),
66
50
)
67
- listenToConnections(maxConnections, serverChannel, socketAddress, globalState)
51
+ listenToConnections(
52
+ maxConnections = maxConnections,
53
+ serverChannel = serverChannel,
54
+ socketDescription = socketPath,
55
+ globalState = globalState,
56
+ )
68
57
}
69
58
} finally {
70
59
runInterruptible {
@@ -76,17 +65,17 @@ suspend fun readFromSocket(socket: String, maxConnections: Int) {
76
65
private suspend fun listenToConnections (
77
66
maxConnections : Int ,
78
67
serverChannel : ServerSocketChannel ,
79
- socketAddress : UnixDomainSocketAddress ,
68
+ socketDescription : String ,
80
69
globalState : GlobalState ,
81
70
) {
82
71
coroutineScope {
83
72
List (maxConnections) { index ->
84
73
withLoggingContext(" Connection Index" to index.toString()) {
85
74
launch(MDCContext ()) {
86
75
handleRequests(
87
- serverChannel,
88
- socketAddress ,
89
- globalState.connectionStates[index]
76
+ serverChannel = serverChannel ,
77
+ socketDescription = socketDescription ,
78
+ connectionState = globalState.connectionStates[index]
90
79
)
91
80
}
92
81
}
@@ -96,21 +85,23 @@ private suspend fun listenToConnections(
96
85
97
86
private suspend fun handleRequests (
98
87
serverChannel : ServerSocketChannel ,
99
- socketAddress : UnixDomainSocketAddress ,
88
+ socketDescription : String ,
100
89
connectionState : GlobalState .ConnectionState ,
101
90
): Nothing {
102
91
while (true ) {
92
+ logger.debug { " listen for connection" }
103
93
val socketChannel = try {
104
94
runInterruptible(Dispatchers .IO , block = serverChannel::accept)
105
95
} catch (e: Exception ) {
106
- throw IllegalStateException (" cannot accept socket of ${socketAddress.path.absolutePathString()} " , e)
96
+ throw IllegalStateException (" cannot accept socket of $socketDescription " , e)
107
97
}
108
98
logger.debug { " open connection" }
109
99
val source = socketChannel.source().buffer()
110
100
val sink = socketChannel.sink().buffer()
111
101
while (true ) {
112
102
try {
113
- val message = runInterruptible { FCGIRequestMessage .read(source) }
103
+ logger.trace { " read message" }
104
+ val message = runInterruptible(Dispatchers .IO ) { FCGIRequestMessage .read(source) }
114
105
val result = connectionState.handleMessage(message)
115
106
val close = result.fold(false ) { close, responseMessage ->
116
107
when (responseMessage) {
0 commit comments