Long Polling and HTTP Keep-Alive: 长轮询与连接复用

客户端想"一有新消息就知道",但只能由客户端主动发请求(比如它在内网里,服务端连不进来),常用的办法是长轮询。这篇记录长轮询怎么工作,以及它底下的 HTTP 连接复用(keep-alive)是怎么回事。起因是给 homelab 配 GitHub Actions 自托管 runner 时,想弄清楚 runner 在内网里是怎么及时拿到任务的。 短轮询和长轮询 短轮询:客户端每隔固定时间问一次"有没有新消息",服务端立刻回答,有就给,没有就回空。 间隔设短了,大部分请求都是空跑,浪费流量和服务端资源 间隔设长了,消息要等到下一次轮询才能拿到,延迟最多一个间隔 长轮询:客户端发出请求后,服务端先不回答,把请求挂住: 挂住期间来了消息,立刻在这个请求上返回 一直没有消息,等到约定的时间(比如 50 秒)返回一个空响应 客户端收到响应(不管有没有消息)后马上发下一个请求 client server |---- GET /messages (wait up to 50s) ---->| | | no message yet, hold the request | | ... 12s later a message arrives |<--------- 200 [message] ----------------| |---- GET /messages (wait up to 50s) ---->| sent again immediately | | nothing for 50s |<--------- 200 [] (empty) ---------------| |---- GET /messages (wait up to 50s) ---->| 所以长轮询几乎任何时候都有一个请求挂在服务端,消息一到就能送出去,延迟接近实时;没有消息时每 50 秒才一个请求,空跑也很少。消息在两次请求之间到达也不会丢:服务端先把它放进队列,下一个请求一到就取走。 ...

Created 20206-10-20 · Updated 20206-10-20 · 2 min · 310 words · -

Kubernetes Reflector:跨 namespace 自动同步 Secret 和 ConfigMap

在多 namespace 的 Kubernetes 集群里,TLS Secret、镜像拉取凭据、配置文件等资源经常需要在多个 namespace 里保持相同的内容。Secret 不能直接跨 namespace 引用,传统做法是手动 kubectl 复制,或者写脚本定时同步——两种方式都有维护成本和容易遗漏的问题。 Kubernetes Reflector 是一个轻量级的 controller,专门解决这个问题:在源资源(Secret 或 ConfigMap)上添加几个注解,它就会自动把资源同步到指定的 namespace,并在源资源更新时同步更新所有副本。 典型使用场景 TLS 通配符证书跨 namespace 分发 cert-manager 只在一个 namespace 签发证书,但多个 namespace 的 Ingress 都需要引用同一份 TLS Secret。 default/wiloon-tls-secret ──Reflector──▶ argocd/wiloon-tls-secret ──▶ nexus/wiloon-tls-secret ──▶ rssx/wiloon-tls-secret ──▶ ... 镜像拉取凭据(ImagePullSecret) 私有镜像仓库的登录凭据需要在所有运行业务 Pod 的 namespace 里都存在。 安装 helm repo add emberstack https://emberstack.github.io/helm-charts helm repo update helm upgrade --install reflector emberstack/reflector \ --namespace kube-system 或者用 ArgoCD Application 管理: ...

Created 14146-06-14 · Updated 14146-06-14 · 2 min · 337 words · -