rtm-ctrlz/keepalive 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

rtm-ctrlz/keepalive

Composer 安装命令:

composer require rtm-ctrlz/keepalive

包简介

TCP-Keepalive fine-tuning utility

README 文档

README

This is a helper for setting proper TCP-Keepalive options and values.

Reason

Common way to enable TCP-Keepalive on a socket looks like this:

socket_set_option($socket, SOL_SOCKET, SO_KEEPALIVE, 1);

And yes, it enables keepalive, but what are keepalive parameters? Example above will use system-default values!

Most systems (OSes) have the following defaults:

Option Default value Description
SO_KEEPALIVE 0 TCP Keep-Alive is disabled
TCP_KEEPIDLE 7200 Start keepalive probes after this period (2 hours)
TCP_KEEPINTVL 75 Interval between keepalive probes (75 seconds)
TCP_KEEPCNT 8 Number of keepalive probes before death

So, with default values, first TCP-Keepalive packet will be sent only in 2 hours after last packet, then wait more up to 10 minutes (75s * 8) before dropping dead connection.

Is this really your case?

Not for me, I'd like drop connection within 1 minute or less!

Install

composer require rtm-ctrlz/keepalive

Usage

Below you can find some examples (socket/stream/ssl-stream) for "client-side", but same operations could be done for "server-side".

In examples below we use following parameters:

  • idle time: 45s
  • probes interval: 5s
  • number of probes: 3

So we'd get maximum waiting time of 1 minute.

Raw sockets

// create socket
$socket = \socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
// enable tcp-keepalive
\RtmCtrlz\Keepalive\Keepalive::enable($socket, 45, 5, 3);

Streams (tcp)

// create stream
$stream = \stream_socket_client('tcp://127.0.0.1:80);
// get socket
$socket = \socket_import_stream($stream);
// enable tcp-keepalive
\RtmCtrlz\Keepalive\Keepalive::enable($socket, 45, 5, 3);

Streams (ssl)

This will be a bit harder, because socket_import_stream can not import socket.

But we can do a trick:

// create stream
// NOTE: stream has "tcp" proto
$stream = \stream_socket_client(
    'tcp://127.0.0.1:443',         // target
    $errno,                        // error number
    $errstr,                       // error description
    1.1,                           // timeout
    STREAM_CLIENT_CONNECT,         // flags
    stream_context_create(         // context
        [
            'ssl' => [
                // ... ssl options
            ],
        ]
    )
);

// get socket
$socket = \socket_import_stream($stream);
// enable tcp-keepalive
\RtmCtrlz\Keepalive\Keepalive::enable($socket, 45, 5, 3);
// enable encryption
\stream_socket_enable_crypto($stream, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);

More examples

See examples directory for more examples.

Gotchas

Option numbers

Unfortunately PHP (and ext-sockets) doesn't have TCP_KEEPIDLE, TCP_KEEPINTVL and TCP_KEEPCNT constants.

Found values

Option Linux Darwin BSD
TCP_KEEPIDLE 4 16 256
TCP_KEEPINTVL 5 257 512
TCP_KEEPCNT 6 258 1024

Linux: Linux tcp.h Darwin: Darwin XNU tcp.h BSD: FreeBSD tcp.h

Windows support

Well, Windows should have support for TCP-Keepalive options, but:

  • I have no Windows machine to run tests
  • I didn't look for TCP_KEEP* values on Windows

Feel free to make a PR ;)

Tested with

rtm-ctrlz/keepalive 适用场景与选型建议

rtm-ctrlz/keepalive 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.13k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2021 年 02 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 rtm-ctrlz/keepalive 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 rtm-ctrlz/keepalive 我们能提供哪些服务?
定制开发 / 二次开发

基于 rtm-ctrlz/keepalive 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 4.13k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-02-16