chore: 并入 iotlcm-worker-svr 历史到 worker-svr/(原提交 96f0f4e)
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