mirror of
https://github.com/eosswedenorg/antelope-api-healthcheck
synced 2026-08-23 20:18:14 +02:00
replace github.com/eosswedenorg-go/tcp_server with github.com/panjf2000/gnet
This commit is contained in:
parent
be7a246317
commit
1db04c0e76
4 changed files with 127 additions and 53 deletions
|
|
@ -1,64 +1,111 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
||||
"github.com/eosswedenorg-go/tcp_server"
|
||||
"github.com/eosswedenorg/antelope-api-healthcheck/internal/api"
|
||||
log "github.com/inconshreveable/log15"
|
||||
"github.com/panjf2000/gnet/v2"
|
||||
)
|
||||
|
||||
// onTcpMessage callback function
|
||||
// ---------------------------------------------------------
|
||||
type Server struct {
|
||||
gnet.BuiltinEventEngine
|
||||
|
||||
func onTcpMessage(c *tcp_server.Client, args string) {
|
||||
addr string
|
||||
eng gnet.Engine
|
||||
}
|
||||
|
||||
func New(addr string) *Server {
|
||||
return &Server{
|
||||
addr: fmt.Sprintf("tcp://%s", addr),
|
||||
}
|
||||
}
|
||||
|
||||
// OnBoot callback function
|
||||
//
|
||||
// ---------------------------------------------------------
|
||||
func (s *Server) OnBoot(eng gnet.Engine) gnet.Action {
|
||||
s.eng = eng
|
||||
log.Info("Server started", "addr", s.addr)
|
||||
return gnet.None
|
||||
}
|
||||
|
||||
// OnShutdown callback function
|
||||
//
|
||||
// ---------------------------------------------------------
|
||||
func (s *Server) OnShutdown(eng gnet.Engine) {
|
||||
log.Info("Server shutdown")
|
||||
}
|
||||
|
||||
// OnTick callback function
|
||||
//
|
||||
// ---------------------------------------------------------
|
||||
func (s *Server) OnTick() (time.Duration, gnet.Action) {
|
||||
log.Info("Server info", "connections", s.eng.CountConnections())
|
||||
return time.Second * 10, gnet.None
|
||||
}
|
||||
|
||||
// OnTraffic callback function
|
||||
//
|
||||
// ---------------------------------------------------------
|
||||
func (s *Server) OnTraffic(c gnet.Conn) gnet.Action {
|
||||
logger := log.Root()
|
||||
|
||||
req, err := c.Next(-1)
|
||||
if err != nil {
|
||||
logger.Error("Read", "message", err)
|
||||
return gnet.Close
|
||||
}
|
||||
|
||||
// Check api.
|
||||
// -------------------
|
||||
healthCheckApi, err := ParseRequest(args)
|
||||
healthCheckApi, err := ParseRequest(string(req))
|
||||
if err != nil {
|
||||
logger.Warn("Agent request error", "message", err)
|
||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Fail, "")
|
||||
|
||||
_, err = c.WriteString(resp.String())
|
||||
_, err = c.Write([]byte(resp.String()))
|
||||
if err != nil {
|
||||
logger.Error("WriteString", "message", err)
|
||||
logger.Error("Write", "message", err)
|
||||
}
|
||||
|
||||
c.Close()
|
||||
return
|
||||
return gnet.Close
|
||||
}
|
||||
|
||||
status, msg := healthCheckApi.Call()
|
||||
// gnet library does not like blocking calls.
|
||||
// as we do a blocking http call here, we need to wrap it in a goroutine.
|
||||
go func() {
|
||||
status, msg := healthCheckApi.Call()
|
||||
|
||||
params := api.LogParams{}
|
||||
params.Add("status", strings.TrimSpace(status.String()))
|
||||
params := api.LogParams{}
|
||||
params.Add("status", strings.TrimSpace(status.String()))
|
||||
|
||||
if msg != "OK" && len(msg) > 0 {
|
||||
params.Add("error", msg)
|
||||
}
|
||||
if msg != "OK" && len(msg) > 0 {
|
||||
params.Add("error", msg)
|
||||
}
|
||||
|
||||
logger.Info("API Check", params.Combine(healthCheckApi.LogInfo())...)
|
||||
// Report status to HAproxy
|
||||
_, err = c.WriteString(status.String())
|
||||
if err != nil {
|
||||
logger.Error("WriteString", "message", err)
|
||||
}
|
||||
c.Close()
|
||||
logger.Info("API Check", params.Combine(healthCheckApi.LogInfo())...)
|
||||
// Report status to HAproxy
|
||||
err = c.AsyncWrite([]byte(status.String()), nil)
|
||||
if err != nil {
|
||||
logger.Error("Write", "message", err)
|
||||
}
|
||||
}()
|
||||
|
||||
return gnet.None
|
||||
}
|
||||
|
||||
// Start
|
||||
func (s *Server) Close() error {
|
||||
return s.eng.Stop(context.Background())
|
||||
}
|
||||
|
||||
// Run the server event loop.
|
||||
//
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func Start(addr string) (*tcp_server.Server, error) {
|
||||
server := tcp_server.New(addr)
|
||||
server.OnMessage(onTcpMessage)
|
||||
|
||||
err := server.Connect()
|
||||
if err == nil {
|
||||
err = server.Listen()
|
||||
}
|
||||
return server, err
|
||||
func (s *Server) Run() error {
|
||||
return gnet.Run(s, s.addr, gnet.WithMulticore(true), gnet.WithTicker(true))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue