chore: 初始开源发布
iotlcm-worker-svr:异步执行中枢(RabbitMQ 消费者 + 全量同步编排)。 - Go module:iotlcm-worker-svr → git.5iot.com/iotlcm-public/iotlcm-worker-svr(214 文件) - 凭据与内部标识脱敏:MySQL/Redis 凭据、私网与生产 IP、内网域名、 库名 xiujie_admin → iotlcm、哨兵名占位 - 159 处真实手机号 → 合法 11 位编造号(保留号段与后 4 位,维持互异, 不破坏「校验 MSISDN 格式」等测试断言) - 移除内部资产:CI 触发器、Jenkins 触发测试文档、预演门禁与镜像 digest 记录 - Dockerfile 改用公共基础镜像(golang:1.25-alpine → alpine:3.23), 移除写死的 git 凭据 - README 重写(架构/全量同步模型/配置/已知失败),补 Apache-2.0 LICENSE,LF 行尾 + .gitattributes 已知:3 个测试包在源仓同一提交上同样失败(client/upstream、 service/allocate、service/inbound),属既有缺陷,本仓未引入亦未修复。
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
)
|
||||
|
||||
// Config is deliberately independent of worker/config-file wiring. Addr must
|
||||
// be explicit; use 127.0.0.1:0 to let the OS allocate a local test port.
|
||||
type Config struct {
|
||||
Addr string
|
||||
BearerToken string // Optional for standalone callers; production Worker leaves it empty.
|
||||
ShutdownTimeout time.Duration
|
||||
}
|
||||
|
||||
// Server is single-use. Start reports bind errors synchronously; after a
|
||||
// successful Start, Wait reports serve or shutdown errors. Canceling the Start
|
||||
// context stops accepting requests and drains active requests up to the timeout.
|
||||
type Server struct {
|
||||
server *http.Server
|
||||
shutdownTimeout time.Duration
|
||||
mu sync.Mutex
|
||||
started bool
|
||||
addr net.Addr
|
||||
done chan struct{}
|
||||
err error
|
||||
}
|
||||
|
||||
func NewServer(config Config, deps Dependencies) (*Server, error) {
|
||||
if config.Addr == "" || config.ShutdownTimeout < 0 {
|
||||
return nil, gerror.New("full-sync HTTP requires an address and a nonnegative shutdown timeout")
|
||||
}
|
||||
handler, err := NewHandler(config.BearerToken, deps)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if config.ShutdownTimeout == 0 {
|
||||
config.ShutdownTimeout = 10 * time.Second
|
||||
}
|
||||
return &Server{
|
||||
server: &http.Server{
|
||||
Addr: config.Addr, Handler: handler,
|
||||
ReadHeaderTimeout: 5 * time.Second,
|
||||
ReadTimeout: 30 * time.Second,
|
||||
WriteTimeout: 30 * time.Second,
|
||||
IdleTimeout: 60 * time.Second,
|
||||
},
|
||||
shutdownTimeout: config.ShutdownTimeout,
|
||||
done: make(chan struct{}),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Server) Start(ctx context.Context) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if s.started {
|
||||
return gerror.New("full-sync HTTP server has already been started")
|
||||
}
|
||||
s.started = true
|
||||
if err := ctx.Err(); err != nil {
|
||||
s.err = err
|
||||
close(s.done)
|
||||
return err
|
||||
}
|
||||
listener, err := (&net.ListenConfig{}).Listen(ctx, "tcp", s.server.Addr)
|
||||
if err != nil {
|
||||
s.err = gerror.Wrap(err, "listen for full-sync HTTP")
|
||||
close(s.done)
|
||||
return s.err
|
||||
}
|
||||
s.addr = listener.Addr()
|
||||
go s.run(ctx, listener)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Addr returns the bound address, or nil before a successful Start.
|
||||
func (s *Server) Addr() net.Addr {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
return s.addr
|
||||
}
|
||||
|
||||
// Wait may be called concurrently after Start (including a failed Start).
|
||||
func (s *Server) Wait() error {
|
||||
<-s.done
|
||||
return s.err
|
||||
}
|
||||
|
||||
func (s *Server) run(ctx context.Context, listener net.Listener) {
|
||||
serveDone := make(chan error, 1)
|
||||
go func() { serveDone <- s.server.Serve(listener) }()
|
||||
var err error
|
||||
select {
|
||||
case err = <-serveDone:
|
||||
// An unexpected listener failure also needs to drain active requests.
|
||||
case <-ctx.Done():
|
||||
}
|
||||
shutdownCtx, cancel := context.WithTimeout(context.Background(), s.shutdownTimeout)
|
||||
shutdownErr := s.server.Shutdown(shutdownCtx)
|
||||
cancel()
|
||||
if shutdownErr != nil {
|
||||
// Shutdown does not close active connections when its deadline expires.
|
||||
_ = s.server.Close()
|
||||
}
|
||||
if err == nil {
|
||||
err = <-serveDone
|
||||
}
|
||||
if errors.Is(err, http.ErrServerClosed) {
|
||||
err = nil
|
||||
}
|
||||
s.err = errors.Join(err, shutdownErr)
|
||||
close(s.done)
|
||||
}
|
||||
Reference in New Issue
Block a user