mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-22 08:18:29 +02:00
refactored some stuff, added healthcheck support, fixed 'include file' reload not showing in log
This commit is contained in:
@@ -10,14 +10,19 @@ import (
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
E "github.com/yusing/go-proxy/internal/error"
|
||||
url "github.com/yusing/go-proxy/internal/net/types"
|
||||
P "github.com/yusing/go-proxy/internal/proxy"
|
||||
PT "github.com/yusing/go-proxy/internal/proxy/fields"
|
||||
"github.com/yusing/go-proxy/internal/watcher/health"
|
||||
)
|
||||
|
||||
type StreamRoute struct {
|
||||
*P.StreamEntry
|
||||
StreamImpl `json:"-"`
|
||||
|
||||
url url.URL
|
||||
healthMon health.HealthMonitor
|
||||
|
||||
wg sync.WaitGroup
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
@@ -40,8 +45,14 @@ func NewStreamRoute(entry *P.StreamEntry) (*StreamRoute, E.NestedError) {
|
||||
if !entry.Scheme.IsCoherent() {
|
||||
return nil, E.Unsupported("scheme", fmt.Sprintf("%v -> %v", entry.Scheme.ListeningScheme, entry.Scheme.ProxyScheme))
|
||||
}
|
||||
url, err := url.ParseURL(fmt.Sprintf("%s://%s:%d", entry.Scheme.ProxyScheme, entry.Host, entry.Port.ProxyPort))
|
||||
if err != nil {
|
||||
// !! should not happen
|
||||
panic(err)
|
||||
}
|
||||
base := &StreamRoute{
|
||||
StreamEntry: entry,
|
||||
url: url,
|
||||
connCh: make(chan any, 100),
|
||||
}
|
||||
if entry.Scheme.ListeningScheme.IsTCP() {
|
||||
@@ -49,6 +60,9 @@ func NewStreamRoute(entry *P.StreamEntry) (*StreamRoute, E.NestedError) {
|
||||
} else {
|
||||
base.StreamImpl = NewUDPRoute(base)
|
||||
}
|
||||
if !entry.Healthcheck.Disabled {
|
||||
base.healthMon = health.NewRawHealthMonitor(base.ctx, string(entry.Alias), url, entry.Healthcheck)
|
||||
}
|
||||
base.l = logrus.WithField("route", base.StreamImpl)
|
||||
return base, nil
|
||||
}
|
||||
@@ -57,6 +71,10 @@ func (r *StreamRoute) String() string {
|
||||
return fmt.Sprintf("%s stream: %s", r.Scheme, r.Alias)
|
||||
}
|
||||
|
||||
func (r *StreamRoute) URL() url.URL {
|
||||
return r.url
|
||||
}
|
||||
|
||||
func (r *StreamRoute) Start() E.NestedError {
|
||||
if r.Port.ProxyPort == PT.NoPort || r.started.Load() {
|
||||
return nil
|
||||
@@ -71,6 +89,9 @@ func (r *StreamRoute) Start() E.NestedError {
|
||||
r.wg.Add(2)
|
||||
go r.grAcceptConnections()
|
||||
go r.grHandleConnections()
|
||||
if r.healthMon != nil {
|
||||
r.healthMon.Start()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -78,7 +99,12 @@ func (r *StreamRoute) Stop() E.NestedError {
|
||||
if !r.started.Load() {
|
||||
return nil
|
||||
}
|
||||
l := r.l
|
||||
r.started.Store(false)
|
||||
|
||||
if r.healthMon != nil {
|
||||
r.healthMon.Stop()
|
||||
}
|
||||
|
||||
r.cancel()
|
||||
r.CloseListeners()
|
||||
|
||||
@@ -92,7 +118,7 @@ func (r *StreamRoute) Stop() E.NestedError {
|
||||
for {
|
||||
select {
|
||||
case <-done:
|
||||
l.Debug("stopped listening")
|
||||
r.l.Debug("stopped listening")
|
||||
return nil
|
||||
case <-timeout:
|
||||
return E.FailedWhy("stop", "timed out")
|
||||
|
||||
Reference in New Issue
Block a user