返回市场
快速时间服务器-go

快速时间服务器-go

作者:crivetimihai2 星标更新:2025-06-21

项目介绍

🦫 快速时间服务器

作者:Mihai Criveti 一个使用Go语言编写的最小服务,通过标准输入输出(stdio)HTTP/JSON-RPC或**服务器发送事件(SSE)**来流式传输或返回当前的UTC时间。

Go 版本 许可证:Apache-2.0


功能

  • 三种传输方式:stdiohttp(JSON-RPC 2.0)和sse
  • 单一静态二进制文件(约2 MiB)
  • 构建时版本及日期通过main.appVersionmain.buildDate设置
  • 跨平台构建通过make cross
  • 轻量级容器的Dockerfile
  • 支持代码检查(golangci-lintstaticcheck)和预提交钩子
  • 单元测试与HTML覆盖率报告及Go基准测试
  • 使用hey支持负载测试

快速开始

git clone https://github.com/yourorg/fast-time-server.git
cd fast-time-server

# 通过标准输入输出构建并运行
make run

# 在8080端口上通过HTTP JSON-RPC运行
make run-http

# 在8080端口上通过SSE运行
make run-sse

安装

需要Go 1.23+。

go install github.com/yourorg/fast-time-server@latest

也可以从发布页面下载。

命令行标志

标志默认值描述
-transportstdio选项:stdiohttpssedual
-addr/-listen0.0.0.0绑定HTTP/SSE的地址
-port8080HTTP/SSE/dual的端口
-auth-token(空)SSE认证的Bearer令牌

API参考

HTTP(JSON-RPC 2.0)

POST /http

请求:

{"jsonrpc":"2.0","method":"Time.Now","id":1}

响应:

{"jsonrpc":"2.0","result":"2025-06-21T12:34:56Z","id":1}

SSE

GET /sse(可选头部:Authorization: Bearer <token>

每秒输出一次UTC时间戳:

data: 2025-06-21T12:34:56Z

负载测试

安装流行的HTTP负载测试工具hey

brew install hey            # macOS
wget https://hey-release... # Linux预编译二进制文件

运行快速负载测试:

# 总计500次请求,50个并发,HTTP传输
hey -n 500 -c 50 http://localhost:8080/http

生成详细的CSV输出用于分析:

hey -n 1000 -c 100 -o csv http://localhost:8080/http > 结果.csv

Docker

make docker-build
make docker-run           # HTTP模式

跨平台编译

make cross

二进制文件会出现在dist/fast-time-server-<os>-<arch>目录下。

开发

任务命令
格式化及整理make fmt tidy
检查及验证make lint staticcheck vet
运行预提交钩子make pre-commit

测试及基准测试

make test       # 单元测试(竞态检测)
make coverage   # HTML覆盖率报告
make bench      # Go基准测试

许可证

Apache 2.0