Highly-opinionated (ex-bullshit-free) MTPROTO proxy for Telegram. If you use v1.0 or upgrade broke you proxy, please read the chapter Version 2
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

sockopts_usertimeout.go 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. //go:build linux
  2. package network
  3. import (
  4. "net"
  5. "syscall"
  6. "time"
  7. "golang.org/x/sys/unix"
  8. )
  9. // Go runtime defaults for KeepAliveConfig when fields are zero.
  10. const (
  11. goDefaultKeepAliveIdle = 15 * time.Second
  12. goDefaultKeepAliveInterval = 15 * time.Second
  13. goDefaultKeepAliveCount = 9
  14. )
  15. // setTCPUserTimeout sets TCP_USER_TIMEOUT on a socket. If transmitted
  16. // data remains unacknowledged for this long, the kernel closes the
  17. // connection. As recommended by Cloudflare
  18. // (https://blog.cloudflare.com/when-tcp-sockets-refuse-to-die/),
  19. // the value is computed as: keepidle + keepintvl * keepcnt. This
  20. // ensures TCP_USER_TIMEOUT and keepalives agree on when to give up.
  21. // Best-effort: silently ignored if unsupported.
  22. func setTCPUserTimeout(conn syscall.RawConn, cfg net.KeepAliveConfig) {
  23. idle := cfg.Idle
  24. if idle == 0 {
  25. idle = goDefaultKeepAliveIdle
  26. }
  27. interval := cfg.Interval
  28. if interval == 0 {
  29. interval = goDefaultKeepAliveInterval
  30. }
  31. count := cfg.Count
  32. if count == 0 {
  33. count = goDefaultKeepAliveCount
  34. }
  35. timeout := idle + interval*time.Duration(count)
  36. conn.Control(func(fd uintptr) { //nolint: errcheck
  37. unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_USER_TIMEOUT, int(timeout.Milliseconds())) //nolint: errcheck
  38. })
  39. }