mirror of
https://github.com/yusing/godoxy.git
synced 2025-06-01 09:32:35 +02:00
implement health monitor for file server
This commit is contained in:
parent
ee5c9e0bf4
commit
4b87b080dd
2 changed files with 46 additions and 40 deletions
|
@ -2,7 +2,6 @@ package route
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/yusing/go-proxy/internal/common"
|
"github.com/yusing/go-proxy/internal/common"
|
||||||
gphttp "github.com/yusing/go-proxy/internal/net/http"
|
gphttp "github.com/yusing/go-proxy/internal/net/http"
|
||||||
|
@ -21,11 +20,12 @@ type (
|
||||||
FileServer struct {
|
FileServer struct {
|
||||||
*Route
|
*Route
|
||||||
|
|
||||||
|
Health *monitor.FileServerHealthMonitor `json:"health"`
|
||||||
|
|
||||||
task *task.Task
|
task *task.Task
|
||||||
middleware *middleware.Middleware
|
middleware *middleware.Middleware
|
||||||
handler http.Handler
|
handler http.Handler
|
||||||
accessLogger *accesslog.AccessLogger
|
accessLogger *accesslog.AccessLogger
|
||||||
startTime time.Time
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -49,8 +49,7 @@ func NewFileServer(base *Route) (*FileServer, E.Error) {
|
||||||
|
|
||||||
// Start implements task.TaskStarter.
|
// Start implements task.TaskStarter.
|
||||||
func (s *FileServer) Start(parent task.Parent) E.Error {
|
func (s *FileServer) Start(parent task.Parent) E.Error {
|
||||||
s.startTime = time.Now()
|
s.task = parent.Subtask("fileserver."+s.TargetName(), false)
|
||||||
s.task = parent.Subtask("fileserver."+s.Name(), false)
|
|
||||||
|
|
||||||
pathPatterns := s.PathPatterns
|
pathPatterns := s.PathPatterns
|
||||||
switch {
|
switch {
|
||||||
|
@ -90,6 +89,11 @@ func (s *FileServer) Start(parent task.Parent) E.Error {
|
||||||
s.task.OnCancel("reset_metrics", metricsLogger.ResetMetrics)
|
s.task.OnCancel("reset_metrics", metricsLogger.ResetMetrics)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if s.UseHealthCheck() {
|
||||||
|
s.Health = monitor.NewFileServerHealthMonitor(s.TargetName(), s.HealthCheck, s.Root)
|
||||||
|
s.Health.Start(s.task)
|
||||||
|
}
|
||||||
|
|
||||||
routes.SetHTTPRoute(s.TargetName(), s)
|
routes.SetHTTPRoute(s.TargetName(), s)
|
||||||
s.task.OnCancel("entrypoint_remove_route", func() {
|
s.task.OnCancel("entrypoint_remove_route", func() {
|
||||||
routes.DeleteHTTPRoute(s.TargetName())
|
routes.DeleteHTTPRoute(s.TargetName())
|
||||||
|
@ -114,40 +118,6 @@ func (s *FileServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Status implements health.HealthMonitor.
|
func (s *FileServer) HealthMonitor() health.HealthMonitor {
|
||||||
func (s *FileServer) Status() health.Status {
|
return s.Health
|
||||||
return health.StatusHealthy
|
|
||||||
}
|
|
||||||
|
|
||||||
// Uptime implements health.HealthMonitor.
|
|
||||||
func (s *FileServer) Uptime() time.Duration {
|
|
||||||
return time.Since(s.startTime)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Latency implements health.HealthMonitor.
|
|
||||||
func (s *FileServer) Latency() time.Duration {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// MarshalJSON implements json.Marshaler.
|
|
||||||
func (s *FileServer) MarshalJSON() ([]byte, error) {
|
|
||||||
return (&monitor.JSONRepresentation{
|
|
||||||
Name: s.Alias,
|
|
||||||
Config: nil,
|
|
||||||
Status: s.Status(),
|
|
||||||
Started: s.startTime,
|
|
||||||
Uptime: s.Uptime(),
|
|
||||||
Latency: s.Latency(),
|
|
||||||
LastSeen: time.Now(),
|
|
||||||
Detail: "",
|
|
||||||
URL: nil,
|
|
||||||
}).MarshalJSON()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *FileServer) String() string {
|
|
||||||
return "FileServer " + s.Alias
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *FileServer) Name() string {
|
|
||||||
return s.Alias
|
|
||||||
}
|
}
|
||||||
|
|
36
internal/watcher/health/monitor/fileserver.go
Normal file
36
internal/watcher/health/monitor/fileserver.go
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
package monitor
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/yusing/go-proxy/internal/watcher/health"
|
||||||
|
)
|
||||||
|
|
||||||
|
type FileServerHealthMonitor struct {
|
||||||
|
*monitor
|
||||||
|
path string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewFileServerHealthMonitor(alias string, config *health.HealthCheckConfig, path string) *FileServerHealthMonitor {
|
||||||
|
mon := &FileServerHealthMonitor{path: path}
|
||||||
|
mon.monitor = newMonitor(nil, config, mon.CheckHealth)
|
||||||
|
mon.service = alias
|
||||||
|
return mon
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *FileServerHealthMonitor) CheckHealth() (*health.HealthCheckResult, error) {
|
||||||
|
start := time.Now()
|
||||||
|
_, err := os.Stat(s.path)
|
||||||
|
|
||||||
|
detail := ""
|
||||||
|
if err != nil {
|
||||||
|
detail = err.Error()
|
||||||
|
}
|
||||||
|
|
||||||
|
return &health.HealthCheckResult{
|
||||||
|
Healthy: err == nil,
|
||||||
|
Latency: time.Since(start),
|
||||||
|
Detail: detail,
|
||||||
|
}, nil
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue