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()
更新日志:
- 添加CoroutineMappedDiagnosticContext。
- 修复AsynchronousTcpSession无法在Windows系统上写入大数据的问题。
转自 https://www.oschina.net/news/97028/firefly-4-8-1-released