zhboner / realm

A network relay tool
MIT License
1.48k stars 279 forks source link

Request to set default tcp keepalive interval #115

Closed MeteorsLiu closed 8 months ago

MeteorsLiu commented 9 months ago

In Golang, a default tcp keepalive interval(15 Seconds) is set when the connection is established, which prevents the super long interval set by Linux or other system causing the connection "unhealthy"(In Chinese, "unhealthy" means 断流).

However, in Rust, there's no default keepalive interval, which means it will use the system default tcp keepalive.

For example, in Linux, the tcp keepalive interval is 7200s, which means that it have to wait for 7200s to confirm whether the connection is healthy or not. If the connection is broken, it will cause the ETIMEDOUT error and shut down by user space programs.

In high traffic cases, 7200s makes kernel spend a lots of times to do interruption of checking "unhealthy tcp" and makes the numbers of connection increasing(old broken connection cannot be shutdown as soon as possible).

So request to set default tcp keepalive interval to 15s.

I tried to modify some codes (https://github.com/MeteorsLiu/realm) to archive that.

However, i cannot compile due to

error[E0658]: `impl Trait` in type aliases is unstable
  --> /root/.cargo/registry/src/github.com-1ecc6299db9ec823/kaminari-0.11.0/src/trick.rs:50:27
   |
50 |     type ConnectFut<'a> = impl Future<Output = Result<Self::Stream>> +'a where Self: 'a;
   |                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: see issue #63063 <https://github.com/rust-lang/rust/issues/63063> for more information
   = help: add `#![feature(type_alias_impl_trait)]` to the crate attributes to enable
zephyrchien commented 9 months ago

Thanks for pointing this out!

To be clear, Linux(which is most used) offers 3 kernel params:

All of them should be configured to a proper value.

Personally I consider it reasonable to set them in our app. It is similar to the nofile limit which we finally made it resolved by the app itself, in case some end-users are not well aware of this problem.

zephyrchien commented 9 months ago

@MeteorsLiu I'm not sure which toolchain you are using. Switch to the latest nightly one, and run cargo update may help solve the compiling error =-=

MeteorsLiu commented 9 months ago

@MeteorsLiu I'm not sure which toolchain you are using. Switch to the latest nightly one, and run cargo update may help solve the compiling error =-=

after cargo update

   Compiling lightws v0.6.9
error[E0635]: unknown feature `core_io_borrowed_buf`
 --> /root/.cargo/registry/src/github.com-1ecc6299db9ec823/lightws-0.6.9/src/lib.rs:6:12
  |
6 | #![feature(core_io_borrowed_buf)]
  |            ^^^^^^^^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0635`.
error: could not compile `lightws` due to previous error

My rustc version: rustc 1.74.1 (a28077b28 2023-12-04)

zephyrchien commented 9 months ago

@MeteorsLiu Please build with the nightly version rustc 1.76.0-nightly (f967532a4 2023-12-08). Realm relies on some nightly features(hopefully get stabilized in 2024).

MeteorsLiu commented 9 months ago

I have finished the job, can i open a PR?

zephyrchien commented 9 months ago

Of course. Thanks!