Igor Artamonov
Igor Artamonov

Reputation: 35961

Reactor TcpServer drop incoming connection

What is the proper way to deny/drop incoming connection to a Reactor TcpServer?

I currently have following:

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

It seems to be working and it successfully drops connections from remote addresses which are in my notAllowed list. But each time it prints the stack trace to the console, and in general it doesn't look good.

What is the proper approach for denying some connections to TcpServer?

Upvotes: 0

Views: 194

Answers (1)

Violeta Georgieva
Violeta Georgieva

Reputation: 2292

I would recommend you instead of throwing IllegalStateException, just invoke Connection#dispose

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

Upvotes: 1

Related Questions