simplify task package implementation

This commit is contained in:
yusing
2025-01-01 06:07:32 +08:00
parent e7aaa95ec5
commit 1ab34ed46f
35 changed files with 547 additions and 600 deletions

View File

@@ -22,6 +22,7 @@ type (
Path string `json:"path" validate:"required"`
Filters Filters `json:"filters"`
Fields Fields `json:"fields"`
// Retention *Retention
}
)
@@ -31,7 +32,7 @@ var (
FormatJSON Format = "json"
)
const DefaultBufferSize = 100
const DefaultBufferSize = 64 * 1024 // 64KB
func DefaultConfig() *Config {
return &Config{

View File

@@ -0,0 +1,22 @@
package accesslog
import (
"fmt"
"os"
"sync"
"github.com/yusing/go-proxy/internal/task"
)
type File struct {
*os.File
sync.Mutex
}
func NewFileAccessLogger(parent task.Parent, cfg *Config) (*AccessLogger, error) {
f, err := os.OpenFile(cfg.Path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
return nil, fmt.Errorf("access log open error: %w", err)
}
return NewAccessLogger(parent, &File{File: f}, cfg), nil
}

View File

@@ -7,18 +7,17 @@ import (
"net/http"
"net/url"
"strconv"
"time"
)
type (
CommonFormatter struct {
cfg *Fields
}
CombinedFormatter struct {
CommonFormatter
}
JSONFormatter struct {
CommonFormatter
cfg *Fields
GetTimeNow func() time.Time // for testing purposes only
}
CombinedFormatter CommonFormatter
JSONFormatter CommonFormatter
JSONLogEntry struct {
Time string `json:"time"`
IP string `json:"ip"`
@@ -39,6 +38,8 @@ type (
}
)
const LogTimeFormat = "02/Jan/2006:15:04:05 -0700"
func scheme(req *http.Request) string {
if req.TLS != nil {
return "https"
@@ -62,7 +63,7 @@ func clientIP(req *http.Request) string {
return req.RemoteAddr
}
func (f CommonFormatter) Format(line *bytes.Buffer, req *http.Request, res *http.Response) {
func (f *CommonFormatter) Format(line *bytes.Buffer, req *http.Request, res *http.Response) {
query := f.cfg.Query.ProcessQuery(req.URL.Query())
line.WriteString(req.Host)
@@ -71,7 +72,7 @@ func (f CommonFormatter) Format(line *bytes.Buffer, req *http.Request, res *http
line.WriteString(clientIP(req))
line.WriteString(" - - [")
line.WriteString(timeNow())
line.WriteString(f.GetTimeNow().Format(LogTimeFormat))
line.WriteString("] \"")
line.WriteString(req.Method)
@@ -86,8 +87,8 @@ func (f CommonFormatter) Format(line *bytes.Buffer, req *http.Request, res *http
line.WriteString(strconv.FormatInt(res.ContentLength, 10))
}
func (f CombinedFormatter) Format(line *bytes.Buffer, req *http.Request, res *http.Response) {
f.CommonFormatter.Format(line, req, res)
func (f *CombinedFormatter) Format(line *bytes.Buffer, req *http.Request, res *http.Response) {
(*CommonFormatter)(f).Format(line, req, res)
line.WriteString(" \"")
line.WriteString(req.Referer())
line.WriteString("\" \"")
@@ -95,14 +96,14 @@ func (f CombinedFormatter) Format(line *bytes.Buffer, req *http.Request, res *ht
line.WriteRune('"')
}
func (f JSONFormatter) Format(line *bytes.Buffer, req *http.Request, res *http.Response) {
func (f *JSONFormatter) Format(line *bytes.Buffer, req *http.Request, res *http.Response) {
query := f.cfg.Query.ProcessQuery(req.URL.Query())
headers := f.cfg.Headers.ProcessHeaders(req.Header)
headers.Del("Cookie")
cookies := f.cfg.Cookies.ProcessCookies(req.Cookies())
entry := JSONLogEntry{
Time: timeNow(),
Time: f.GetTimeNow().Format(LogTimeFormat),
IP: clientIP(req),
Method: req.Method,
Scheme: scheme(req),

View File

@@ -9,6 +9,7 @@ import (
"github.com/yusing/go-proxy/internal/common"
E "github.com/yusing/go-proxy/internal/error"
"github.com/yusing/go-proxy/internal/net/http/loadbalancer/types"
"github.com/yusing/go-proxy/internal/route/routes"
"github.com/yusing/go-proxy/internal/task"
"github.com/yusing/go-proxy/internal/watcher/health"
"github.com/yusing/go-proxy/internal/watcher/health/monitor"
@@ -52,10 +53,13 @@ func New(cfg *Config) *LoadBalancer {
}
// Start implements task.TaskStarter.
func (lb *LoadBalancer) Start(routeSubtask *task.Task) E.Error {
func (lb *LoadBalancer) Start(parent task.Parent) E.Error {
lb.startTime = time.Now()
lb.task = routeSubtask
lb.task.OnFinished("loadbalancer cleanup", func() {
lb.task = parent.Subtask("loadbalancer."+lb.Link, false)
parent.OnCancel("lb_remove_route", func() {
routes.DeleteHTTPRoute(lb.Link)
})
lb.task.OnFinished("cleanup", func() {
if lb.impl != nil {
lb.pool.RangeAll(func(k string, v *Server) {
lb.impl.OnRemoveServer(v)
@@ -66,6 +70,11 @@ func (lb *LoadBalancer) Start(routeSubtask *task.Task) E.Error {
return nil
}
// Task implements task.TaskStarter.
func (lb *LoadBalancer) Task() *task.Task {
return lb.task
}
// Finish implements task.TaskFinisher.
func (lb *LoadBalancer) Finish(reason any) {
lb.task.Finish(reason)

View File

@@ -32,10 +32,10 @@ func setup() {
return
}
task := task.GlobalTask("error page")
dirWatcher = W.NewDirectoryWatcher(task.Subtask("dir watcher"), errPagesBasePath)
t := task.RootTask("error_page", true)
dirWatcher = W.NewDirectoryWatcher(t, errPagesBasePath)
loadContent()
go watchDir(task)
go watchDir()
}
func GetStaticFile(filename string) ([]byte, bool) {
@@ -73,11 +73,11 @@ func loadContent() {
}
}
func watchDir(task *task.Task) {
eventCh, errCh := dirWatcher.Events(task.Context())
func watchDir() {
eventCh, errCh := dirWatcher.Events(task.RootContext())
for {
select {
case <-task.Context().Done():
case <-task.RootContextCanceled():
return
case event, ok := <-eventCh:
if !ok {

View File

@@ -24,8 +24,6 @@ type Server struct {
httpsStarted bool
startTime time.Time
task *task.Task
l zerolog.Logger
}
@@ -76,7 +74,6 @@ func NewServer(opt Options) (s *Server) {
CertProvider: opt.CertProvider,
http: httpSer,
https: httpsSer,
task: task.GlobalTask(opt.Name + " server"),
l: logger,
}
}
@@ -108,7 +105,7 @@ func (s *Server) Start() {
s.l.Info().Str("addr", s.https.Addr).Msgf("server started")
}
s.task.OnFinished("stop server", s.stop)
task.OnProgramExit("server."+s.Name+".stop", s.stop)
}
func (s *Server) stop() {
@@ -117,12 +114,12 @@ func (s *Server) stop() {
}
if s.http != nil && s.httpStarted {
s.handleErr("http", s.http.Shutdown(s.task.Context()))
s.handleErr("http", s.http.Shutdown(task.RootContext()))
s.httpStarted = false
}
if s.https != nil && s.httpsStarted {
s.handleErr("https", s.https.Shutdown(s.task.Context()))
s.handleErr("https", s.https.Shutdown(task.RootContext()))
s.httpsStarted = false
}
}