mirror of
https://github.com/eosswedenorg/antelope-api-healthcheck
synced 2026-06-19 05:10:02 +02:00
src/server.go: make tcp message function a "real" function instead of a inline one.
This commit is contained in:
parent
ef13af4eb7
commit
a310cb041d
1 changed files with 58 additions and 53 deletions
111
src/server.go
111
src/server.go
|
|
@ -109,6 +109,63 @@ func argv_listen_addr() string {
|
|||
return addr
|
||||
}
|
||||
|
||||
// onTcpMessage callback function
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func onTcpMessage(c *tcp_server.Client, args string) {
|
||||
params := eosapi.ReqParams{}
|
||||
var block_time int = 10
|
||||
var version string = "v1"
|
||||
|
||||
// Parse arguments.
|
||||
// -------------------
|
||||
split := strings.Split(strings.TrimSpace(args), "|")
|
||||
|
||||
// 1. url (scheme + ip/domain + port)
|
||||
params.Url = split[0]
|
||||
|
||||
// 2. Block time.
|
||||
if len(split) > 1 {
|
||||
p, err := strconv.ParseInt(split[1], 10, 32)
|
||||
if err == nil {
|
||||
block_time = int(p)
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Version
|
||||
if len(split) > 2 {
|
||||
version = split[2]
|
||||
}
|
||||
|
||||
// 4. Host
|
||||
if len(split) > 3 {
|
||||
params.Host = split[3]
|
||||
}
|
||||
|
||||
// Check api.
|
||||
// -------------------
|
||||
var status haproxy.HealthCheckStatus
|
||||
var msg string
|
||||
|
||||
if version == "v2" {
|
||||
status, msg = check_api_v2(params, int64(block_time / 2))
|
||||
} else {
|
||||
version = "v1"
|
||||
status, msg = check_api(params, float64(block_time))
|
||||
}
|
||||
|
||||
log.Info("Status %s - %s (%d blocks): %s",
|
||||
version, params.Url, block_time / 2, status)
|
||||
|
||||
if status != haproxy.HealthCheckUp && len(msg) > 0 {
|
||||
log.Warning(msg)
|
||||
}
|
||||
|
||||
// Report status to HAproxy
|
||||
c.Send(fmt.Sprintln(status))
|
||||
c.Close()
|
||||
}
|
||||
|
||||
// main
|
||||
// ---------------------------------------------------------
|
||||
func main() {
|
||||
|
|
@ -135,59 +192,7 @@ func main() {
|
|||
});
|
||||
|
||||
// TCP Client sends message.
|
||||
server.OnNewMessage(func(c *tcp_server.Client, args string) {
|
||||
params := eosapi.ReqParams{}
|
||||
var block_time int = 10
|
||||
var version string = "v1"
|
||||
|
||||
// Parse arguments.
|
||||
// -------------------
|
||||
split := strings.Split(strings.TrimSpace(args), "|")
|
||||
|
||||
// 1. url (scheme + ip/domain + port)
|
||||
params.Url = split[0]
|
||||
|
||||
// 2. Block time.
|
||||
if len(split) > 1 {
|
||||
p, err := strconv.ParseInt(split[1], 10, 32)
|
||||
if err == nil {
|
||||
block_time = int(p)
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Version
|
||||
if len(split) > 2 {
|
||||
version = split[2]
|
||||
}
|
||||
|
||||
// 4. Host
|
||||
if len(split) > 3 {
|
||||
params.Host = split[3]
|
||||
}
|
||||
|
||||
// Check api.
|
||||
// -------------------
|
||||
var status haproxy.HealthCheckStatus
|
||||
var msg string
|
||||
|
||||
if version == "v2" {
|
||||
status, msg = check_api_v2(params, int64(block_time / 2))
|
||||
} else {
|
||||
version = "v1"
|
||||
status, msg = check_api(params, float64(block_time))
|
||||
}
|
||||
|
||||
log.Info("Status %s - %s (%d blocks): %s",
|
||||
version, params.Url, block_time / 2, status)
|
||||
|
||||
if status != haproxy.HealthCheckUp && len(msg) > 0 {
|
||||
log.Warning(msg)
|
||||
}
|
||||
|
||||
// Report status to HAproxy
|
||||
c.Send(fmt.Sprintln(status))
|
||||
c.Close()
|
||||
});
|
||||
server.OnNewMessage(onTcpMessage);
|
||||
|
||||
// TCP Client disconnect.
|
||||
server.OnClientConnectionClosed(func(c *tcp_server.Client, err error) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue