exegroup/rewrite control logic, make sync actor stop serial;

This commit is contained in:
2024-01-17 16:17:51 +08:00
parent 1100fbb1d4
commit a844e9e34c
8 changed files with 291 additions and 134 deletions

View File

@@ -2,11 +2,13 @@ package eghttp
import (
"context"
"errors"
"net"
"net/http"
"strconv"
"sync/atomic"
"time"
"git.blauwelle.com/go/crate/log"
)
const (
@@ -30,6 +32,7 @@ func WithHandler(handler http.Handler) Option {
})
}
// WithServer 指定要启动的服务器. 注意会替换原有的服务器, 导致早前配置服务器相关的 [Option] 失效.
func WithServer(server *http.Server) Option {
return optionFunc(func(cfg *config) {
cfg.server = server
@@ -49,9 +52,17 @@ func WithServerOption(fn func(server *http.Server)) Option {
})
}
// WithShutdownTimeout 设置关闭服务的超时时间. <0 没有最长等待时间.
func WithShutdownTimeout(timeout time.Duration) Option {
return optionFunc(func(cfg *config) {
cfg.shutdownTimeout = timeout
})
}
type config struct {
server *http.Server
startFn func(server *http.Server) error
server *http.Server
startFn func(server *http.Server) error
shutdownTimeout time.Duration
}
func newDefaultConfig() *config {
@@ -63,6 +74,7 @@ func newDefaultConfig() *config {
startFn: func(server *http.Server) error {
return server.ListenAndServe()
},
shutdownTimeout: -1,
}
}
@@ -72,24 +84,31 @@ func (fn optionFunc) apply(cfg *config) {
fn(cfg)
}
// HTTPListenAndServe 创建 [http.Server] 并提供启动和停止函数;
func HTTPListenAndServe(opts ...Option) (func(ctx context.Context) error, func(ctx context.Context)) {
// ListenAndServe 构造1个函数, 启动 [http.Server] 并受 ctx 控制终止.
func ListenAndServe(opts ...Option) func(ctx context.Context) error {
cfg := newDefaultConfig()
for _, opt := range opts {
opt.apply(cfg)
}
inShutdown := &atomic.Bool{}
c := make(chan error, 1)
goFunc := func(_ context.Context) error {
err := cfg.startFn(cfg.server)
if inShutdown.Load() {
err = <-c
}
return err
errChan := make(chan error, 2) //nolint:gomnd
return func(ctx context.Context) error {
go func() {
err := cfg.startFn(cfg.server)
log.Tracef(ctx, "server return with %s", err)
if !errors.Is(err, http.ErrServerClosed) {
errChan <- err
}
}()
go func() {
<-ctx.Done()
shutdownCtx := context.Background()
if cfg.shutdownTimeout >= 0 {
var cancel func()
shutdownCtx, cancel = context.WithTimeout(shutdownCtx, cfg.shutdownTimeout)
defer cancel()
}
errChan <- cfg.server.Shutdown(shutdownCtx)
}()
return <-errChan
}
stopFunc := func(ctx context.Context) {
inShutdown.Store(true)
c <- cfg.server.Shutdown(ctx)
}
return goFunc, stopFunc
}