2018年6月13日星期三

Firefly 4.8.1 正式版发布,增加 MDC 实现


Linuxeden 开源社区 --

Firefly 4.8.1 新增了基于 Coroutine 的 MDC 实现并修复了网络框架在 Windows 系统中无法发送大的数据的问题。

Firefly HTTP 服务器是异步的。一个 HTTP 请求会跨越多个线程。默认的 MDC 将数据保存在 ThreadLocal 中。这意味着默认的 MDC 无法跟踪用户请求。
我们添加了一个新的 CoroutineMappedDiagnosticContext,可以通过整个 HTTP 请求保存数据。
CoroutineMappedDiagnosticContext 使用协程拦截器机制。您只能在 Firefly HTTP 服务器 Kotlin 版中使用它。

首先,我们需要创建一个新的 Java ServiceLoader 配置来替换默认的 MDC 实现。在类路径中创建一个新文件:

classpath:/META-INF/services/com.firefly.utils.log.MappedDiagnosticContext

在此文件中添加一个新的 MDC 类名称:

com.firefly.kotlin.ext.log.CoroutineMappedDiagnosticContext

初始化 CoroutineMappedDiagnosticContext:

@Inject
private lateinit var requestCtx: CoroutineLocal<RoutingContext>

@InitialMethod
fun init() {
    val mdc = MappedDiagnosticContextFactory.getInstance()
            .mappedDiagnosticContext as CoroutineMappedDiagnosticContext
    mdc.setRequestCtx(requestCtx)
}

然后,我们可以在 Firefly HTTP 服务器 Kotlin 版本中使用 MDC API。如添加跟踪 ID:

val mdc = MDC.putCloseable("tracingId", UUID.randomUUID().toString().replace("-", ""))

如果在新的上下文中启动 Coroutine,则需要组合新的上下文和当前请求上下文。我们提供该方法 asyncTraceable。

fun <T> asyncTraceable(context: ContinuationInterceptor = Unconfined, block: suspend CoroutineScope.() -> T): Deferred<T> = asyncTraceable(getRequestCtx(), context, block)

将新的 Coroutine 上下文组合到当前的请求上下文中。如:

val data = asyncTraceable(ioBlocking) {
    fileInputStream.use {
        `$`.io.readBytes(it)
    }
}.await()

更新日志:

  1. 添加 CoroutineMappedDiagnosticContext。
  2. 修复 AsynchronousTcpSession 无法在 Windows 系统上写入大数据的问题。

转自 https://ift.tt/2HKLBlb

https://ift.tt/2l50Yfn

没有评论:

发表评论