端口漂移:默认开启,端口被占用时自动换端口启动

Open
#23 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
55/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Active
Tech stack
go
Domain
backend

Research direction

Start by locating the server startup path that currently calls ListenAndServe, the configuration handling for OIS_PORT_DRIFT, and the self-check command. Read the existing startup and error handling, then add focused tests for occupied ports, :0, non-occupied errors, and the disabled setting. Done means startup logs show the original and selected addresses and attempts, and the README configuration table documents the default-on switch.

Written by the indexing model from the issue text.

Description

requirement
背景

服务端口被占用时进程直接启动失败。本地多服务并存(或残留进程未退出)时经常撞端口。参考需求:open-pdf 已实现的端口漂移机制。

需求
  1. 默认开启端口漂移:配置端口被占用时自动尝试后续端口(步进 +1,最多尝试 20 个),每次尝试记 info 日志,成功后以醒目日志输出最终监听地址(含漂移前的原地址)
  2. 支持关闭:OIS_PORT_DRIFT=false 时保持现状——端口被占立即报错退出,错误为 5xxx 稳定错误码(含「端口被占用」人话提示与原地址)
  3. 边界行为:
    • 端口为 :0(系统随机分配)不做漂移
    • 非「占用」类错误(权限不足、地址不可用等)不漂移,原样报错
    • 连续 20 个端口全部失败:报稳定错误码退出,日志列出尝试范围
  4. 容器形态不受影响(独立网络命名空间);self-check 子命令仍探测配置端口
  5. README 配置表补充该开关与默认值
技术要点
  • 用 net.Listen 探测代替直接 ListenAndServe,成功后 http.Server.Serve(ln)
  • 占用判定:errors.Is(err, syscall.EADDRINUSE),辅以错误串匹配兜底(Windows WSA 口径)
  • 地址解析需兼容 :8080、127.0.0.1:8080 两种形态;host 部分保持不变只递增端口
验收标准
  • 单测:占用某端口后开漂移能落到其他端口;关漂移时返回占用错误;:0 直通
  • 单测:非占用错误(如权限)不做漂移
  • 集成测试:模拟占用后启动服务,healthz 在漂移后的端口可访问
  • 启动日志包含原端口、最终端口与尝试次数
Dominant language
Go
Stars
2
Forks
1
Avg merge
1h 37m
Merged PRs (30d)
6

Getting set up

  • Ships a Dockerfile or Docker Compose file
  • Has a pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from LinuxSuRen/open-img-sync

All issues in LinuxSuRen/open-img-sync

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.