linkedin / flashback

mock the internet
BSD 2-Clause "Simplified" License
578 stars 54 forks source link

only write to channel when it's writable and optimize flushes #34

Open kawang108 opened 5 years ago

kawang108 commented 5 years ago

Summary:

  1. Check channel's writability before writing more bytes to it.
  2. Add FlushConsolidationHandler to optimize socket flushes which are expensive OS calls.

Currently we are writing to either server or client channel without first checking if the channel is writable. This can cause problems if we end up writing too fast. To improve on this, we need to check channel.isWritable before each write, and when the channel's writability changes to false, pause reading from the source channel.

We are also calling writeAndFlush for every payload that's read from the client or server (e.g., an http request is dissected into several payloads: HttpRequest, 1 or more HttpContent). The flush results in a syscall at the OS level and is usually expensive. We can optimize these calls by adding a FlushConsolidationHandler which is provided by netty in a recent minor version. This handler tries to batch multiple flushes (up to 256) into a single flush.