首页 > 解决方案 > Reactor TcpServer 丢弃传入连接

问题描述

拒绝/丢弃到Reactor TcpServer的传入连接的正确方法是什么?

我目前有以下内容:

TcpServer.create()
  .doOnConnection {
     if (notAllowed(it.address()) {
        throw IllegalStateException("Connection from ${it.address()} denied")
     }
  }
  .handle(...)
  .bindNow()

它似乎正在工作,它成功地从我notAllowed列表中的远程地址断开连接。但是每次将堆栈跟踪打印到控制台时,通常看起来并不好。

拒绝某些与 TcpServer 的连接的正确方法是什么?

标签: javaspringproject-reactorreactor-netty

解决方案


我会推荐你​​而不是 throwing IllegalStateException,只需调用Connection#dispose

TcpServer.create()
         .doOnConnection {
             if (notAllowed(it.address()) {
                 it.dispose()
             }
         }
         .handle(...)
         .bindNow()

推荐阅读