File tree Expand file tree Collapse file tree
src/main/kotlin/net/ccbluex/netty/http Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -122,7 +122,7 @@ class HttpServer {
122122 suspend fun stop () = lock.withLock {
123123 logger.info(" Shutting down Netty server..." )
124124 try {
125- webSocketController?.disconnect ()
125+ webSocketController?.disconnectAsync ()
126126 serverChannel?.close()?.awaitSuspend()
127127 bossGroup?.shutdownGracefully()?.awaitSuspend()
128128 workerGroup?.shutdownGracefully()?.awaitSuspend()
Original file line number Diff line number Diff line change @@ -30,6 +30,7 @@ import kotlinx.coroutines.asCoroutineDispatcher
3030import kotlinx.coroutines.cancel
3131import kotlinx.coroutines.joinAll
3232import kotlinx.coroutines.launch
33+ import kotlinx.coroutines.supervisorScope
3334import net.ccbluex.netty.http.HttpServer.Companion.logger
3435import net.ccbluex.netty.http.coroutines.syncSuspend
3536import java.nio.channels.ClosedChannelException
@@ -100,10 +101,25 @@ class WebSocketController(
100101 * Closes all active contexts.
101102 */
102103 fun disconnect () {
103- activeContexts.removeIf { handlerContext ->
104- runCatching {
105- handlerContext.channel().close().sync()
106- }.isSuccess
104+ val contexts = activeContexts.toTypedArray()
105+ activeContexts.clear()
106+
107+ contexts.forEach { handlerContext ->
108+ handlerContext.channel().close().sync()
109+ }
110+ }
111+
112+ /* *
113+ * Closes all active contexts async.
114+ */
115+ suspend fun disconnectAsync () = supervisorScope {
116+ val contexts = activeContexts.toTypedArray()
117+ activeContexts.clear()
118+
119+ contexts.forEach { handlerContext ->
120+ launch {
121+ handlerContext.channel().close().syncSuspend()
122+ }
107123 }
108124 }
109125
You can’t perform that action at this time.
0 commit comments