【发布时间】:2021-06-14 16:54:47
【问题描述】:
SpringBoot v2.5.1
有一个端点请求一个长时间运行的进程结果,它是通过某种方式创建的
(为简单起见,它是Mono.fromCallable( ... long running ... )。
客户端发出请求并触发发布者完成工作,但几秒钟后客户端中止请求(即连接丢失)。并且该过程仍然继续利用资源来计算结果以丢弃。
通知 Project Reactor 的事件循环有关应该取消的不必要工作的机制是什么?
@RestController
class EndpointSpin {
@GetMapping("/spin")
Mono<Long> spin() {
AtomicLong counter = new AtomicLong(0);
Instant stopTime = Instant.now().plus(Duration.of(1, ChronoUnit.HOURS));
return Mono.fromCallable(() -> {
while (Instant.now().isBefore(stopTime)) {
counter.incrementAndGet();
if (counter.get() % 10_000_000 == 0) {
System.out.println(counter.get());
}
// of course this does not work
if (Thread.currentThread().isInterrupted()){
break;
}
}
return counter.get();
});
}
}
【问题讨论】:
标签: java reactive-programming spring-webflux project-reactor cancellation