Skip to content

Commit

Permalink
Add parallel processing for appender events
Browse files Browse the repository at this point in the history
Introduced a `forEachParallel` extension function to run operations concurrently on collections using coroutines. Updated `RootLogger` to utilize this extension, improving efficiency in appender event processing.
  • Loading branch information
smyrgeorge committed Oct 18, 2024
1 parent 38d6636 commit a1aa04a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.github.smyrgeorge.log4k

import io.github.smyrgeorge.log4k.impl.SimpleLoggerFactory
import io.github.smyrgeorge.log4k.impl.appenders.ConsoleAppender
import io.github.smyrgeorge.log4k.impl.extensions.forEachParallel
import io.github.smyrgeorge.log4k.registry.AppenderRegistry
import io.github.smyrgeorge.log4k.registry.LoggerRegistry
import kotlinx.coroutines.CoroutineScope
Expand Down Expand Up @@ -31,7 +32,7 @@ object RootLogger {
LoggerScope.launch(Dispatchers.IO) {
events.consumeEach { event ->
event.id = nextIdx()
appenders.all().forEach { it.append(event) }
appenders.all().forEachParallel { it.append(event) }
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.github.smyrgeorge.log4k.impl.extensions

import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.withContext
import kotlin.coroutines.EmptyCoroutineContext

suspend fun <A> Iterable<A>.forEachParallel(f: suspend (A) -> Unit): Unit =
withContext(EmptyCoroutineContext) { map { async { f(it) } }.awaitAll() }

0 comments on commit a1aa04a

Please sign in to comment.