major refactor
All checks were successful
Build and Publish / BuildAndDeployAmd64 (push) Successful in 28s
Build and Publish / BuildAndDeploy (push) Successful in 2m18s

This commit is contained in:
matst80
2024-11-13 21:56:40 +01:00
parent 9f7c8227c2
commit abf561c3fe
20 changed files with 310 additions and 1292 deletions

View File

@@ -7,13 +7,13 @@ import (
)
type RemoteHost struct {
*Client
*Connection
Host string
MissedPings int
}
func (h *RemoteHost) IsHealthy() bool {
return !h.PersistentConnection.Dead && h.MissedPings < 3
return h.MissedPings < 3
}
func (h *RemoteHost) Initialize(p *SyncedPool) {
@@ -38,15 +38,11 @@ func (h *RemoteHost) Initialize(p *SyncedPool) {
}
func (h *RemoteHost) Ping() error {
_, err := h.Call(Ping, Pong, []byte{})
result, err := h.Call(Ping, nil)
if err != nil {
if err != nil || result.StatusCode != 200 || result.Type != Pong {
h.MissedPings++
log.Printf("Error pinging remote %s, missed pings: %d", h.Host, h.MissedPings)
if !h.IsHealthy() {
h.Close()
return fmt.Errorf("remote %s is dead", h.Host)
}
} else {
h.MissedPings = 0
}
@@ -54,28 +50,28 @@ func (h *RemoteHost) Ping() error {
}
func (h *RemoteHost) Negotiate(knownHosts []string) ([]string, error) {
reply, err := h.Call(RemoteNegotiate, RemoteNegotiateResponse, []byte(strings.Join(knownHosts, ";")))
reply, err := h.Call(RemoteNegotiate, []byte(strings.Join(knownHosts, ";")))
if err != nil {
return nil, err
}
if reply.StatusCode != 200 {
return nil, fmt.Errorf("remote returned error on negotiate: %s", string(reply.Data))
return nil, fmt.Errorf("remote returned error on negotiate: %s", string(reply.Payload))
}
return strings.Split(string(reply.Data), ";"), nil
return strings.Split(string(reply.Payload), ";"), nil
}
func (g *RemoteHost) GetCartMappings() ([]CartId, error) {
reply, err := g.Call(GetCartIds, CartIdsResponse, []byte{})
reply, err := g.Call(GetCartIds, []byte{})
if err != nil {
return nil, err
}
if reply.StatusCode != 200 {
log.Printf("Remote returned error on get cart mappings: %s", string(reply.Data))
return nil, fmt.Errorf("remote returned error: %s", string(reply.Data))
if reply.StatusCode != 200 || reply.Type != CartIdsResponse {
log.Printf("Remote returned error on get cart mappings: %s", string(reply.Payload))
return nil, fmt.Errorf("remote returned incorrect data")
}
parts := strings.Split(string(reply.Data), ";")
parts := strings.Split(string(reply.Payload), ";")
ids := make([]CartId, 0, len(parts))
for _, p := range parts {
ids = append(ids, ToCartId(p))
@@ -84,14 +80,11 @@ func (g *RemoteHost) GetCartMappings() ([]CartId, error) {
}
func (r *RemoteHost) ConfirmChange(id CartId, host string) error {
reply, err := r.Call(RemoteGrainChanged, AckChange, []byte(fmt.Sprintf("%s;%s", id, host)))
reply, err := r.Call(RemoteGrainChanged, []byte(fmt.Sprintf("%s;%s", id, host)))
if err != nil {
if err != nil || reply.StatusCode != 200 || reply.Type != AckChange {
return err
}
if string(reply.Data) != "ok" {
return fmt.Errorf("remote grain change failed %s", string(reply.Data))
}
return nil
}