Compare commits
2 Commits
log/v0.13.
...
exegroup/v
| Author | SHA1 | Date | |
|---|---|---|---|
| c82211c957 | |||
| a698a8135d |
73
exegroup/eghttp/actor.go
Normal file
73
exegroup/eghttp/actor.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package eghttp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
DefaultAddr = ":8080"
|
||||
DefaultReadHeaderTimeout = time.Second
|
||||
)
|
||||
|
||||
type Option interface {
|
||||
apply(server *http.Server)
|
||||
}
|
||||
|
||||
func WithPort(port int) Option {
|
||||
return optionFunc(func(server *http.Server) {
|
||||
server.Addr = net.JoinHostPort("", strconv.Itoa(port))
|
||||
})
|
||||
}
|
||||
|
||||
func WithHandler(handler http.Handler) Option {
|
||||
return optionFunc(func(server *http.Server) {
|
||||
server.Handler = handler
|
||||
})
|
||||
}
|
||||
|
||||
// WithServerOption 使用 fn 配置 [http.Server];
|
||||
func WithServerOption(fn func(server *http.Server)) Option {
|
||||
return optionFunc(fn)
|
||||
}
|
||||
|
||||
type optionFunc func(server *http.Server)
|
||||
|
||||
func (fn optionFunc) apply(server *http.Server) {
|
||||
fn(server)
|
||||
}
|
||||
|
||||
// HTTPListenAndServe 创建 [http.Server] 并提供启动和停止函数;
|
||||
// opts 按照顺序应用到 [http.Server] 上.
|
||||
func HTTPListenAndServe(opts ...Option) (func(ctx context.Context) error, func(ctx context.Context)) {
|
||||
server := &http.Server{
|
||||
Addr: DefaultAddr,
|
||||
ReadHeaderTimeout: DefaultReadHeaderTimeout,
|
||||
}
|
||||
for _, opt := range opts {
|
||||
opt.apply(server)
|
||||
}
|
||||
return HTTPServer(server)
|
||||
}
|
||||
|
||||
// HTTPServer 提供 [http.Server] 的启动和停止函数;
|
||||
func HTTPServer(server *http.Server) (func(ctx context.Context) error, func(ctx context.Context)) {
|
||||
inShutdown := &atomic.Bool{}
|
||||
c := make(chan error, 1)
|
||||
goFunc := func(_ context.Context) error {
|
||||
err := server.ListenAndServe()
|
||||
if inShutdown.Load() {
|
||||
err = <-c
|
||||
}
|
||||
return err
|
||||
}
|
||||
stopFunc := func(ctx context.Context) {
|
||||
inShutdown.Store(true)
|
||||
c <- server.Shutdown(ctx)
|
||||
}
|
||||
return goFunc, stopFunc
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
package exegroup
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
// HTTPListenAndServe 提供 [http.Server] 的启动和停止函数;
|
||||
func HTTPListenAndServe(port int, handler http.Handler) (func(ctx context.Context) error, func(ctx context.Context)) {
|
||||
server := &http.Server{
|
||||
Addr: net.JoinHostPort("", strconv.Itoa(port)),
|
||||
Handler: handler,
|
||||
ReadHeaderTimeout: time.Second,
|
||||
}
|
||||
inShutdown := &atomic.Bool{}
|
||||
c := make(chan error, 1)
|
||||
goFunc := func(_ context.Context) error {
|
||||
err := server.ListenAndServe()
|
||||
if inShutdown.Load() {
|
||||
err = <-c
|
||||
}
|
||||
return err
|
||||
}
|
||||
stopFunc := func(ctx context.Context) {
|
||||
inShutdown.Store(true)
|
||||
c <- server.Shutdown(ctx)
|
||||
}
|
||||
return goFunc, stopFunc
|
||||
}
|
||||
@@ -54,8 +54,7 @@ log.Logger()
|
||||
- `SetReportStack` 设置生成调用栈;
|
||||
- `SetReportStackLevel` 当日志等级小于设定值时强制生成调用栈;
|
||||
- `Reset` 把 Logger 恢复到初始状态;
|
||||
- `AddBeforeExit` 新增调用 `Logger.Exit` 时优先执行的函数, 先增加的后执行;
|
||||
- `SetExit` 设置 `Logger` 的退出函数(`Logger.Exit`), 当日志等级为 `LevelFatal` 时调用这个函数;
|
||||
- `AddBeforeExit` 增加 Exit 在调用 [os.Exit] 前执行的函数, 先增加的后执行;
|
||||
|
||||
### 日志生成
|
||||
|
||||
|
||||
@@ -20,7 +20,6 @@ type entry = Entry
|
||||
// Logger 中保存了日志所需的全局配置,
|
||||
// 使用 Logger 处理日志.
|
||||
type Logger struct {
|
||||
exit func(code int) // protected by lock
|
||||
beforeExitFns []func() // protected by lock
|
||||
levelProcessors levelProcessors // protected by lock
|
||||
entry
|
||||
@@ -99,7 +98,6 @@ func (logger *Logger) SetReportStackLevel(level Level) {
|
||||
// Reset 把 Logger 重置到初始状态
|
||||
func (logger *Logger) Reset() {
|
||||
logger.lock.Lock()
|
||||
logger.exit = nil
|
||||
logger.beforeExitFns = nil
|
||||
logger.levelProcessors = levelProcessors{}
|
||||
logger.lock.Unlock()
|
||||
@@ -125,27 +123,13 @@ func (logger *Logger) BeforeExit() {
|
||||
})
|
||||
}
|
||||
|
||||
// Exit 执行 BeforeExit 后退出程序, 执行的退出函数可以通过 SetExit 指定,
|
||||
// 当没有通过 SetExit 指定退出函数时调用 [os.Exit].
|
||||
// Exit 执行 BeforeExit 后调用 [os.Exit]退出程序.
|
||||
func (logger *Logger) Exit(code int) {
|
||||
logger.BeforeExit()
|
||||
logger.lock.RLock()
|
||||
exit := logger.exit
|
||||
logger.lock.RUnlock()
|
||||
if exit == nil {
|
||||
exit = os.Exit
|
||||
}
|
||||
exit(code)
|
||||
os.Exit(code)
|
||||
}
|
||||
|
||||
// SetExit 指定退出程序时执行的函数
|
||||
func (logger *Logger) SetExit(fn func(code int)) {
|
||||
logger.lock.Lock()
|
||||
logger.exit = fn
|
||||
logger.lock.Unlock()
|
||||
}
|
||||
|
||||
// AddBeforeExit 增加 Exit 在调用 SetExit 指定的函数前执行的函数,
|
||||
// AddBeforeExit 增加 Exit 在调用 [os.Exit] 前执行的函数,
|
||||
// 先增加的后执行.
|
||||
func (logger *Logger) AddBeforeExit(fn ...func()) {
|
||||
logger.lock.Lock()
|
||||
|
||||
Reference in New Issue
Block a user