1

我读到了 ratpack 和my-first-ratpack-app-what-i-learned状态:

线程池的大小取决于系统中可用的处理器内核数。例如,这通常意味着比使用 Spring Boot 和 Tomcat 的线程池要小得多。当一个请求进来时,一个计算线程处理它。如果需要执行阻塞 IO,则将该工作传递给异步阻塞线程,然后计算线程返回处理请求。当阻塞线程准备好时,计算线程会从阻塞线程中恢复执行。

让我们从Ratpack-and-Spring-Boot 中获取示例代码

...

  // construct a "promise" for the requested user object. This will
  // be subscribed to within the respective handlers, according to what
  // they must do. The promise uses the "blocking" fixture to ensure
  // the DB call doesn't take place on a "request taking" thread.
  Promise<User> userPromise = ctx.blocking(() -> userRepository.findByUsername(username));
  ctx.byMethod(method -> method
    .get(() ->
      // the .then() block will "subscribe" to the result, allowing
      // us to send the user domain object back to the client
      userPromise.then(user -> sendUser(ctx, user))
    )

...

存储库定义如下:

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends CrudRepository<User, Long> {

  User findByUsername(String username); (1)
}

让我们假设,

  • userRepository.findByUsername(username)速度慢,需要 10s 执行,它使用同步阻塞 io
  • 我们有 10 个并发客户端,每个客户端每 5 秒发送一个新请求
  • 客户端期望在最多 12 秒后收到响应(10 秒占用数据库,2 秒应该足以应付其他开销)

所以

  • 我们每秒有 2 个请求 - 真的没有那么多
  • 但是客户端发送请求的频率是服务它的两倍
  • 正如我们看到的存储库不是异步的,我们甚至可以假设它不是 spring 数据存储库,而是我们使用同步阻塞 java io 的自定义存储库

我的问题是:

  • 我假设,如果它是tomcat,它会在有限的时间内跪下,无论线程池大小如何,在有限的时间内,如果我错了,请纠正我
  • ratpack 会无限期地承受这样的负载吗?
  • 如果是,它使用什么java解决方案和机制来做到这一点???
  • 那神秘的阻塞线程是如何工作的?怎么实现??

在我看来这是不可能实现的。如果我们使用异步 io,那么我们可以单独使用一个线程来完成,但没有异步 io 则不行。而我们的线程民意调查,无论有多大,最终都会耗尽。

4

0 回答 0