仕事のために、Web サイトの 301/302/Frame リダイレクトのみを実行する特殊な HTTP サーバーを作成しました。最近、一部の悪質なクライアントが意図的にソケットを開き、TCP ソケットのタイムアウトを無効にするために 500 ミリ秒ごとに 1 文字を書き込んでいます。次に、ソケットを無期限に開いたままにし、複数のクライアントが分散サービス拒否で同じことを行うようにします。これにより、最終的に TCP 接続を処理するスレッド プールが使い果たされます。この種の悪い動作の影響を受けにくくするには、どのようにコードを記述しますか? これが私のソケット受け入れコードです:
while (true) {
// Blocks while waiting for a new connection
log.debug("Blocking while waiting for a new connection.") ;
try {
Socket server = httpServer.accept() ;
// After receiving a new connection, set the SO_LINGER and SO_TIMEOUT options
server.setReuseAddress(true) ;
server.setSoTimeout(timeout) ;
server.setSoLinger(true, socketTimeout) ;
// Hand off the new socket connection to a worker thread
threadPool.execute(new Worker(cache, server, requests, geoIp)) ;
} catch (IOException e) {
log.error("Unable to accept socket connection.", e) ;
continue ;
}
}
timeout と socketTimeout は現在 500 ミリ秒に設定されています。