-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #367 from porters-xyz/develop
Deploying updated health checks
- Loading branch information
Showing
4 changed files
with
64 additions
and
7 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
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
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
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 |
---|---|---|
@@ -1,14 +1,71 @@ | ||
package proxy | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"log/slog" | ||
"net/http" | ||
"os" | ||
"porters/common" | ||
"porters/db" | ||
|
||
"golang.org/x/sys/unix" | ||
) | ||
|
||
// TODO other healthchecks should be added | ||
type HealthStatus struct { | ||
CacheHealth *common.HealthCheckStatus `json:"cache_health"` // Use the correct type here | ||
} | ||
|
||
// New function to check file descriptor health and log the results using slog | ||
func checkFileDescriptorHealth() bool { | ||
var rLimit unix.Rlimit | ||
|
||
// Get the max number of allowed file descriptors | ||
err := unix.Getrlimit(unix.RLIMIT_NOFILE, &rLimit) | ||
if err != nil { | ||
slog.Error("Error retrieving file descriptor limit", "error", err) | ||
return false | ||
} | ||
|
||
// Get the actual number of file descriptors in use | ||
files, err := os.ReadDir("/proc/self/fd") | ||
if err != nil { | ||
slog.Error("Error reading /proc/self/fd", "error", err) | ||
return false | ||
} | ||
fdUsage := uint64(len(files)) | ||
|
||
healthy := fdUsage < rLimit.Cur*80/100 | ||
|
||
// Log the file descriptor information using slog | ||
slog.Info("File Descriptor Usage", | ||
"max", rLimit.Cur, | ||
"used", fdUsage, | ||
"healthy", healthy) | ||
|
||
return healthy | ||
} | ||
|
||
// Update your healthHandler to log file descriptor health but not expose it | ||
func healthHandler(resp http.ResponseWriter, req *http.Request) { | ||
hc := (&db.Cache{}).Healthcheck() | ||
// Existing cache health check | ||
cacheHealth := (&db.Cache{}).Healthcheck() | ||
|
||
// Log file descriptor health and determine if the server is healthy | ||
fdHealthy := checkFileDescriptorHealth() | ||
|
||
// Only return the non-sensitive health information (cache health) | ||
healthStatus := HealthStatus{ | ||
CacheHealth: cacheHealth, | ||
} | ||
|
||
// Marshal the health status into JSON | ||
resp.Header().Set("Content-Type", "application/json") | ||
io.WriteString(resp, hc.ToJson()) | ||
|
||
if fdHealthy { | ||
io.WriteString(resp, healthStatus.CacheHealth.ToJson()) | ||
} else { | ||
resp.WriteHeader(http.StatusInternalServerError) | ||
io.WriteString(resp, fmt.Sprintf(`{"error": "file descriptor limit exceeded"}`)) | ||
} | ||
} |