add observer

This commit is contained in:
ginuerzh 2024-01-03 20:53:00 +08:00
parent 6b5c04b5e4
commit 5a427b4eaf
3 changed files with 38 additions and 0 deletions

View File

@ -11,6 +11,7 @@ import (
"github.com/go-gost/core/limiter/traffic"
"github.com/go-gost/core/logger"
"github.com/go-gost/core/metadata"
"github.com/go-gost/core/observer"
)
type Options struct {
@ -22,6 +23,7 @@ type Options struct {
Limiter traffic.TrafficLimiter
TLSConfig *tls.Config
Logger logger.Logger
Observer observer.Observer
Service string
}
@ -75,6 +77,12 @@ func LoggerOption(logger logger.Logger) Option {
}
}
func ObserverOption(observer observer.Observer) Option {
return func(opts *Options) {
opts.Observer = observer
}
}
func ServiceOption(service string) Option {
return func(opts *Options) {
opts.Service = service

View File

@ -10,6 +10,7 @@ import (
"github.com/go-gost/core/limiter/conn"
"github.com/go-gost/core/limiter/traffic"
"github.com/go-gost/core/logger"
"github.com/go-gost/x/stats"
)
type Options struct {
@ -21,6 +22,7 @@ type Options struct {
TrafficLimiter traffic.TrafficLimiter
ConnLimiter conn.ConnLimiter
Chain chain.Chainer
Stats *stats.Stats
Logger logger.Logger
Service string
ProxyProtocol int
@ -76,6 +78,12 @@ func ChainOption(chain chain.Chainer) Option {
}
}
func StatsOption(stats *stats.Stats) Option {
return func(opts *Options) {
opts.Stats = stats
}
}
func LoggerOption(logger logger.Logger) Option {
return func(opts *Options) {
opts.Logger = logger

22
observer/observer.go Normal file
View File

@ -0,0 +1,22 @@
package observer
import "context"
type Options struct{}
type Option func(opts *Options)
type Observer interface {
Observe(ctx context.Context, events []Event, opts ...Option) error
}
type EventType string
const (
EventStatus EventType = "status"
EventStats EventType = "stats"
)
type Event interface {
Type() EventType
}