erlang网络库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
2.6 KiB

5 years ago
5 years ago
5 years ago
5 years ago
  1. -ifndef(eNet_H).
  2. -define(eNet_H, true).
  3. %% gen_tcp ready maybe to set sock options
  4. %% ssl ready and then need do ntSslAcceptor:handshake/3 and maybe to set other options
  5. %% ppt ready and then need do ntPptAcceptor:pptAndHS/5 and maybe to set other options
  6. -define(mSockReady, mSockReady).
  7. -define(DefTpOpts, [
  8. binary
  9. , {packet, 4}
  10. , {active, false}
  11. , {reuseaddr, true}
  12. , {nodelay, true} % 禁用Nagle 可以减少延迟(对于需要低延迟的游戏)
  13. , {delay_send, true} % 提升吞吐量 尤其在高并发场景下,减少调度器争用 增加约10-50μs的延迟(取决于队列深度和调度频率)
  14. , {send_timeout, 15000} % 发送超时时间
  15. , {send_timeout_close, true} % 发送超时自动关闭连接(防止半开连接)
  16. , {keepalive, true} % 检测死连接(默认间隔2小时,需系统级调整)
  17. , {exit_on_close, true} % 当socket被关闭时,与其关联的控制进程(controlling process) 会收到{'EXIT', Port, Reason}信号 强制清理异常连接
  18. , {backlog, 1024} % 等待连接队列长度
  19. , {buffer, 128 * 1024} % 接收缓冲区1MB
  20. , {recbuf, 128 * 1024} % 内核接收缓冲区 通常接受的数据比较小
  21. , {sndbuf, 512 * 1024} % 内核发送缓冲区
  22. , {high_watermark, 32 * 1024} % 32KB Erlang内部Socket实现的数据队列 当队列数据量达到此阈值时,Socket标记为**繁忙(busy)**,发送进程会被挂起。
  23. , {low_watermark, 16 * 1024} % 16KB 当队列数据量降低到此阈值时,Socket恢复**非繁忙状态**,允许继续发送。
  24. , {high_msgq_watermark, 64 * 1024} % 64KB Erlang进程消息队列**的繁忙状态 消息队列数据量达到此值时,队列标记为繁忙,阻止新消息进入
  25. , {low_msgq_watermark, 32 * 1024} % 32KB 消息队列数据量低于此值时,恢复非繁忙状态
  26. ]).
  27. -define(AptCnt, 16).
  28. -define(DefSslHSTet, 15000).
  29. -define(DefProxyPtTet, 5000).
  30. -export_type([listenOpt/0]).
  31. -type listenOpt() ::
  32. {aptCnt, non_neg_integer()} |
  33. {conMod, atom()} |
  34. {conArgs, atom()} |
  35. {tcpOpts, [gen_tcp:listen_option()]} |
  36. {sslOpts, [ssl:ssl_option()]} |
  37. {sslHSTet, timeout()} |
  38. {udpOpts, [gen_udp:option()]} |
  39. {proxyPt, boolean()} |
  40. {proxyPtTet, timeout()}.
  41. %% 令牌桶相关定义
  42. -record(tBucket, {
  43. rate :: pos_integer() %% 速率
  44. , tokens :: non_neg_integer() %% 剩余tokens数量
  45. , lastTime :: pos_integer() %% 最后一次更新访问时间单位毫秒
  46. , bucketSize :: pos_integer() %% 桶大小 可以容纳的令牌数量
  47. }).
  48. -endif.