-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(galois): introduce health endpoint for grpc call
Signed-off-by: kaancaglan <caglankaan@gmail.com>
- Loading branch information
1 parent
17f9053
commit 677c7d3
Showing
2 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"sync/atomic" | ||
|
||
provergrpc "galois/grpc/api/v3" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
func QueryStatsHealth() *cobra.Command { | ||
var cmd = &cobra.Command{ | ||
Short: "Service which query circuit statistics and expose a health endpoint based on the results", | ||
Use: "query-stats-health [uri]", | ||
Args: cobra.ExactArgs(1), | ||
RunE: MakeCobra(func(ctx context.Context, client provergrpc.UnionProverAPIClient, cmd *cobra.Command, args []string) error { | ||
var status int32 = 200 | ||
|
||
// Function to query stats and update status | ||
updateStatus := func() { | ||
res, err := client.QueryStats(ctx, &provergrpc.QueryStatsRequest{}) | ||
if err != nil { | ||
log.Println("Error querying stats:", err) | ||
atomic.StoreInt32(&status, 500) | ||
return | ||
} | ||
bz, err := json.Marshal(res) | ||
if err != nil { | ||
log.Println("Error marshaling response:", err) | ||
atomic.StoreInt32(&status, 500) | ||
return | ||
} | ||
fmt.Println(string(bz)) | ||
atomic.StoreInt32(&status, 200) | ||
} | ||
|
||
// HTTP server | ||
http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) { | ||
updateStatus() | ||
if atomic.LoadInt32(&status) == 200 { | ||
w.WriteHeader(http.StatusOK) | ||
w.Write([]byte("Healthy")) | ||
} else { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
w.Write([]byte("Unhealthy")) | ||
} | ||
}) | ||
|
||
server := &http.Server{Addr: ":9999"} | ||
go func() { | ||
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed { | ||
log.Fatalf("Could not listen on :9999: %v\n", err) | ||
} | ||
}() | ||
|
||
// Wait for context cancellation | ||
<-ctx.Done() | ||
if err := server.Shutdown(context.Background()); err != nil { | ||
log.Fatalf("HTTP server Shutdown: %v", err) | ||
} | ||
|
||
return nil | ||
}), | ||
} | ||
cmd.Flags().String(flagTLS, "", "Whether the gRPC endpoint expects TLS.") | ||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters