Skip to content

Commit

Permalink
make check dumb
Browse files Browse the repository at this point in the history
  • Loading branch information
Trojan295 committed Sep 18, 2024
1 parent 8448b38 commit cc9d37a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 29 deletions.
2 changes: 1 addition & 1 deletion cmd/proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func main() {
cfg.ClusterID, GetVersion(), cfg.KeepAlive, cfg.KeepAliveTimeout)

go func() {
healthchecks := healthz.NewServer(logger, client)
healthchecks := healthz.NewServer(logger)

logger.Infof("Starting healthcheck server on address %v", cfg.HealthAddress)

Expand Down
32 changes: 8 additions & 24 deletions internal/healthz/healthz.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,13 @@ import (
"github.com/sirupsen/logrus"
)

type ProxyClient interface {
IsAlive() bool
}

type Server struct {
log *logrus.Logger
proxyClient ProxyClient
log *logrus.Logger
}

func NewServer(log *logrus.Logger, proxyClient ProxyClient) *Server {
func NewServer(log *logrus.Logger) *Server {
return &Server{
log: log,
proxyClient: proxyClient,
log: log,
}
}

Expand All @@ -32,30 +26,20 @@ func (hc *Server) Run(addr string) error {
}

func (hc *Server) healthCheck(w http.ResponseWriter, r *http.Request) {
status := true
response := make(map[string]string)

if hc.proxyClient.IsAlive() {
response["proxyClient"] = "alive"
} else {
response["proxyClient"] = "not alive"
status = false
}

w.Header().Set("content-type", "application/json")

response := map[string]string{
"status": "ok",
}

body, err := json.Marshal(response)
if err != nil {
hc.log.WithError(err).Errorf("Failed to marshal readiness check response")
w.WriteHeader(http.StatusInternalServerError)
return
}

if status {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusServiceUnavailable)
}
w.WriteHeader(http.StatusOK)

if _, err := w.Write(body); err != nil {
hc.log.WithError(err).Errorf("Failed to write response body")
Expand Down
11 changes: 7 additions & 4 deletions internal/proxy/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (c *Client) sendInitialRequest(stream cloudproxyv1alpha.CloudProxyAPI_Strea
if err != nil {
return fmt.Errorf("stream.Send: initial request %w", err)
}

c.lastSeen.Store(time.Now().UnixNano())
c.lastSeenError.Store(nil)

c.log.Info("Stream to castai started successfully")
Expand All @@ -141,6 +141,9 @@ func (c *Client) run(ctx context.Context, stream cloudproxyv1alpha.CloudProxyAPI
case <-stream.Context().Done():
return
default:
if !c.isAlive() {
return
}
}

c.log.Debugf("Polling stream for messages")
Expand All @@ -165,7 +168,7 @@ func (c *Client) run(ctx context.Context, stream cloudproxyv1alpha.CloudProxyAPI
case <-stream.Context().Done():
return fmt.Errorf("stream closed %w", stream.Context().Err())
case <-time.After(time.Duration(c.keepAlive.Load())):
if !c.IsAlive() {
if !c.isAlive() {
if err := c.lastSeenError.Load(); err != nil {
return fmt.Errorf("recived error: %w", *err)
}
Expand Down Expand Up @@ -247,7 +250,7 @@ func (c *Client) processHttpRequest(req *cloudproxyv1alpha.HTTPRequest) *cloudpr
return c.toResponse(resp)
}

func (c *Client) IsAlive() bool {
func (c *Client) isAlive() bool {
lastSeen := c.lastSeen.Load()
return time.Now().UnixNano()-lastSeen <= c.keepAliveTimeout.Load()
}
Expand All @@ -263,7 +266,7 @@ func (c *Client) sendKeepAlive(stream cloudproxyv1alpha.CloudProxyAPI_StreamClou
c.log.Infof("Stopping keep-alive loop: stream ended with %v", stream.Context().Err())
return
case <-ticker.C:
if !c.IsAlive() {
if !c.isAlive() {
c.log.Info("Stopping keep-alive loop: client connection is not alive")
return
}
Expand Down

0 comments on commit cc9d37a

Please sign in to comment.