待辦事項 #40823

TCP KeepAlive パラメータを個別指定する

啟用日期: 2020-10-01 22:08 最後更新: 2020-10-02 01:42

回報者:
負責人:
(無)
類型:
狀態:
開啟
元件:
(無)
里程碑:
(無)
優先權:
5 - 中
嚴重程度:
5 - 中
處理結果:
檔案:

細節

/proc/sys/net/ipv4/tcp_keepalive_time 等の変更を行わずとも、shogi-serverでのTCP Keepalive間隔を独自に指定できるように。

https://osdn.net/users/mizar/pf/shogi-server/scm/commits/e37932a0d2387227928808ec86f49362ae29f9ea

  1. --------------------------------- shogi-server ---------------------------------
  2. @@ -208,7 +208,10 @@ def parse_command_line
  3. ["--least-time-per-move", GetoptLong::REQUIRED_ARGUMENT],
  4. ["--max-moves", GetoptLong::REQUIRED_ARGUMENT],
  5. ["--pid-file", GetoptLong::REQUIRED_ARGUMENT],
  6. - ["--player-log-dir", GetoptLong::REQUIRED_ARGUMENT])
  7. + ["--player-log-dir", GetoptLong::REQUIRED_ARGUMENT],
  8. + ["--tcpkeepalive-idle", GetoptLong::REQUIRED_ARGUMENT],
  9. + ["--tcpkeepalive-intvl", GetoptLong::REQUIRED_ARGUMENT],
  10. + ["--tcpkeepalive-cnt", GetoptLong::REQUIRED_ARGUMENT])
  11. parser.quiet = true
  12. begin
  13. parser.each_option do |name, arg|
  14. @@ -283,6 +286,15 @@ def check_command_line
  15. $options["least-time-per-move"] ||= ShogiServer::Default_Least_Time_Per_Move
  16. $options["least-time-per-move"] = $options["least-time-per-move"].to_i
  17. +
  18. + $options["tcpkeepalive-idle"] ||= ShogiServer::Default_TcpKeepAlive_Idle
  19. + $options["tcpkeepalive-idle"] = $options["tcpkeepalive-idle"].to_i
  20. +
  21. + $options["tcpkeepalive-intvl"] ||= ShogiServer::Default_TcpKeepAlive_Intvl
  22. + $options["tcpkeepalive-intvl"] = $options["tcpkeepalive-intvl"].to_i
  23. +
  24. + $options["tcpkeepalive-cnt"] ||= ShogiServer::Default_TcpKeepAlive_Cnt
  25. + $options["tcpkeepalive-cnt"] = $options["tcpkeepalive-cnt"].to_i
  26. end
  27. # See if a file can be created in the directory.
  28. @@ -455,8 +467,30 @@ def main
  29. server.start do |client|
  30. begin
  31. # client.sync = true # this is already set in WEBrick
  32. - client.setsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, true)
  33. - # Keepalive time can be set by /proc/sys/net/ipv4/tcp_keepalive_time
  34. + if Socket.const_defined?(:SOL_SOCKET) &&
  35. + Socket.const_defined?(:SO_KEEPALIVE) &&
  36. + Socket.const_defined?(:IPPROTO_TCP)
  37. + client.setsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, true)
  38. + # Keepalive time can be set by /proc/sys/net/ipv4/tcp_keepalive_time
  39. + if Socket.const_defined?(:TCP_KEEPIDLE) &&
  40. + Socket.const_defined?(:TCP_KEEPINTVL) &&
  41. + Socket.const_defined?(:TCP_KEEPCNT)
  42. + client.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_KEEPIDLE, $options["tcpkeepalive-idle"])
  43. + client.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_KEEPINTVL, $options["tcpkeepalive-intvl"])
  44. + client.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_KEEPCNT, $options["tcpkeepalive-cnt"])
  45. + elsif RUBY_PLATFORM.downcase =~ /mswin|mingw|cygwin|bccwin/
  46. + # Windows 10 (1709 or later) ws2ipdef.h
  47. + # https://docs.microsoft.com/en-us/windows/win32/winsock/ipproto-tcp-socket-options
  48. + client.setsockopt(Socket::IPPROTO_TCP, 3, $options["tcpkeepalive-idle"])
  49. + client.setsockopt(Socket::IPPROTO_TCP, 17, $options["tcpkeepalive-intvl"])
  50. + client.setsockopt(Socket::IPPROTO_TCP, 16, $options["tcpkeepalive-cnt"])
  51. + elsif RUBY_PLATFORM.downcase =~ /darwin/
  52. + # macOS 10.12.6 (Sierra) /usr/include/netinet/tcp.h
  53. + client.setsockopt(Socket::IPPROTO_TCP, 0x10, $options["tcpkeepalive-idle"])
  54. + client.setsockopt(Socket::IPPROTO_TCP, 0x101, $options["tcpkeepalive-intvl"])
  55. + client.setsockopt(Socket::IPPROTO_TCP, 0x102, $options["tcpkeepalive-cnt"])
  56. + end
  57. + end
  58. player, login = login_loop(client) # loop
  59. unless player
  60. log_error("Detected a timed out login attempt")
  61. ------------------------------- shogi_server.rb -------------------------------
  62. @@ -50,6 +50,9 @@ Default_Timeout = 60 # for single socket operation
  63. Default_Game_Name = "default-1500-0"
  64. Default_Max_Moves = 256
  65. Default_Least_Time_Per_Move = 0
  66. +Default_TcpKeepAlive_Idle = 50
  67. +Default_TcpKeepAlive_Intvl = 5
  68. +Default_TcpKeepAlive_Cnt = 9
  69. One_Time = 10
  70. Login_Time = 300 # time for LOGIN
  71. Revision = "20160409"

Ticket History (2/2 Histories)

2020-10-01 22:08 Updated by: mizar
  • New Ticket "TCP KeepAlive パラメータを個別指定する" created
2020-10-02 01:42 Updated by: mizar
  • Details Updated

Attachment File List

No attachments

編輯

You are not logged in. I you are not logged in, your comment will be treated as an anonymous post. » 登入