better
This commit is contained in:
@@ -17,13 +17,13 @@ import (
|
||||
// RemoteHost mirrors the lightweight controller used for remote node
|
||||
// interaction.
|
||||
type RemoteHost struct {
|
||||
Host string
|
||||
host string
|
||||
httpBase string
|
||||
conn *grpc.ClientConn
|
||||
transport *http.Transport
|
||||
client *http.Client
|
||||
controlClient messages.ControlPlaneClient
|
||||
MissedPings int
|
||||
missedPings int
|
||||
}
|
||||
|
||||
func NewRemoteHost(host string) (*RemoteHost, error) {
|
||||
@@ -38,22 +38,6 @@ func NewRemoteHost(host string) (*RemoteHost, error) {
|
||||
}
|
||||
|
||||
controlClient := messages.NewControlPlaneClient(conn)
|
||||
// go func() {
|
||||
// for retries := range 3 {
|
||||
// ctx, pingCancel := context.WithTimeout(context.Background(), time.Second)
|
||||
// _, pingErr := controlClient.Ping(ctx, &messages.Empty{})
|
||||
// pingCancel()
|
||||
// if pingErr == nil {
|
||||
// break
|
||||
// }
|
||||
// if retries == 2 {
|
||||
// log.Printf("AddRemote: ping %s failed after retries: %v", host, pingErr)
|
||||
// conn.Close()
|
||||
// p
|
||||
// }
|
||||
// time.Sleep(500 * time.Millisecond)
|
||||
// }
|
||||
// }()
|
||||
|
||||
transport := &http.Transport{
|
||||
MaxIdleConns: 100,
|
||||
@@ -64,18 +48,18 @@ func NewRemoteHost(host string) (*RemoteHost, error) {
|
||||
client := &http.Client{Transport: transport, Timeout: 10 * time.Second}
|
||||
|
||||
return &RemoteHost{
|
||||
Host: host,
|
||||
host: host,
|
||||
httpBase: fmt.Sprintf("http://%s:8080/cart", host),
|
||||
conn: conn,
|
||||
transport: transport,
|
||||
client: client,
|
||||
controlClient: controlClient,
|
||||
MissedPings: 0,
|
||||
missedPings: 0,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (h *RemoteHost) Name() string {
|
||||
return h.Host
|
||||
return h.host
|
||||
}
|
||||
|
||||
func (h *RemoteHost) Close() error {
|
||||
@@ -92,8 +76,8 @@ func (h *RemoteHost) Ping() bool {
|
||||
_, err = h.controlClient.Ping(ctx, &messages.Empty{})
|
||||
cancel()
|
||||
if err != nil {
|
||||
h.MissedPings++
|
||||
log.Printf("Ping %s failed (%d) %v", h.Host, h.MissedPings, err)
|
||||
h.missedPings++
|
||||
log.Printf("Ping %s failed (%d) %v", h.host, h.missedPings, err)
|
||||
}
|
||||
if !h.IsHealthy() {
|
||||
return false
|
||||
@@ -101,7 +85,7 @@ func (h *RemoteHost) Ping() bool {
|
||||
time.Sleep(time.Millisecond * 200)
|
||||
}
|
||||
|
||||
h.MissedPings = 0
|
||||
h.missedPings = 0
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -113,11 +97,11 @@ func (h *RemoteHost) Negotiate(knownHosts []string) ([]string, error) {
|
||||
KnownHosts: knownHosts,
|
||||
})
|
||||
if err != nil {
|
||||
h.MissedPings++
|
||||
log.Printf("Negotiate %s failed: %v", h.Host, err)
|
||||
h.missedPings++
|
||||
log.Printf("Negotiate %s failed: %v", h.host, err)
|
||||
return nil, err
|
||||
}
|
||||
h.MissedPings = 0
|
||||
h.missedPings = 0
|
||||
return resp.Hosts, nil
|
||||
}
|
||||
|
||||
@@ -126,8 +110,8 @@ func (h *RemoteHost) GetActorIds() []uint64 {
|
||||
defer cancel()
|
||||
reply, err := h.controlClient.GetLocalActorIds(ctx, &messages.Empty{})
|
||||
if err != nil {
|
||||
log.Printf("Init remote %s: GetCartIds error: %v", h.Host, err)
|
||||
h.MissedPings++
|
||||
log.Printf("Init remote %s: GetCartIds error: %v", h.host, err)
|
||||
h.missedPings++
|
||||
return []uint64{}
|
||||
}
|
||||
return reply.GetIds()
|
||||
@@ -135,48 +119,33 @@ func (h *RemoteHost) GetActorIds() []uint64 {
|
||||
|
||||
func (h *RemoteHost) AnnounceOwnership(uids []uint64) {
|
||||
_, err := h.controlClient.AnnounceOwnership(context.Background(), &messages.OwnershipAnnounce{
|
||||
Host: h.Host,
|
||||
Host: h.host,
|
||||
Ids: uids,
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("ownership announce to %s failed: %v", h.Host, err)
|
||||
h.MissedPings++
|
||||
log.Printf("ownership announce to %s failed: %v", h.host, err)
|
||||
h.missedPings++
|
||||
return
|
||||
}
|
||||
h.MissedPings = 0
|
||||
h.missedPings = 0
|
||||
}
|
||||
|
||||
func (h *RemoteHost) AnnounceExpiry(uids []uint64) {
|
||||
_, err := h.controlClient.AnnounceExpiry(context.Background(), &messages.ExpiryAnnounce{
|
||||
Host: h.Host,
|
||||
Host: h.host,
|
||||
Ids: uids,
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("expiry announce to %s failed: %v", h.Host, err)
|
||||
h.MissedPings++
|
||||
log.Printf("expiry announce to %s failed: %v", h.host, err)
|
||||
h.missedPings++
|
||||
return
|
||||
}
|
||||
h.MissedPings = 0
|
||||
h.missedPings = 0
|
||||
}
|
||||
|
||||
func (h *RemoteHost) Proxy(id uint64, w http.ResponseWriter, r *http.Request) (bool, error) {
|
||||
target := fmt.Sprintf("%s%s", h.httpBase, r.URL.RequestURI())
|
||||
// var bodyCopy []byte
|
||||
// if r.Body != nil && r.Body != http.NoBody {
|
||||
// var err error
|
||||
// bodyCopy, err = io.ReadAll(r.Body)
|
||||
// if err != nil {
|
||||
// http.Error(w, "proxy read error", http.StatusBadGateway)
|
||||
// return false, err
|
||||
// }
|
||||
// }
|
||||
// if r.Body != nil {
|
||||
// r.Body.Close()
|
||||
// }
|
||||
// var reqBody io.Reader
|
||||
// if len(bodyCopy) > 0 {
|
||||
// reqBody = bytes.NewReader(bodyCopy)
|
||||
// }
|
||||
|
||||
req, err := http.NewRequestWithContext(r.Context(), r.Method, target, r.Body)
|
||||
if err != nil {
|
||||
http.Error(w, "proxy build error", http.StatusBadGateway)
|
||||
@@ -214,5 +183,5 @@ func (h *RemoteHost) Proxy(id uint64, w http.ResponseWriter, r *http.Request) (b
|
||||
}
|
||||
|
||||
func (r *RemoteHost) IsHealthy() bool {
|
||||
return r.MissedPings < 3
|
||||
return r.missedPings < 3
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user