This repository has been archived by the owner on Mar 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
it's important to stay healthy in 2020
Add a simple HTTP healthcheck. Or is it "health check"? Now more than ever I need to clean up some of this code. Put this healthcheck stuff into a "health" module just to help get it out of the way for now. I both love having most logic in proxy.go...and hate it at the same time. I like just running `go build` with no args. Anyways: you can now send an "HTTP GET /health" to the proxy and it will report back a 200 OK...if it's alive. No other "checks" are made yet, but downline can wire in details on the cluster status potentially. Basically if the proxy can't reach the backend, it's not worth sending traffic to it!
- Loading branch information
Showing
5 changed files
with
133 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package health | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"errors" | ||
"net" | ||
"net/http" | ||
) | ||
|
||
const ( | ||
HEALTH_REQ = "GET /health HTTP" | ||
OK_RESPONSE = "HTTP/1.1 200 OK\r\n" | ||
BAD_RESPONSE = "HTTP/1.1 400 Bad Request\r\n" | ||
) | ||
|
||
// Check if the given buf looks like an HTTP GET to our /health endpoint | ||
func IsHealthCheck(buf []byte) bool { | ||
return bytes.HasPrefix(buf, []byte(HEALTH_REQ)) | ||
} | ||
|
||
// Given a connectioned client conn and its message as a byte-slice buf, | ||
// validate it's an HTTP request. If so, write a "204 No Content" http | ||
// response letting the caller know bolt-proxy is alive. | ||
func HandleHealthCheck(conn net.Conn, buf []byte) error { | ||
reader := bytes.NewReader(buf) | ||
bufioReader := bufio.NewReader(reader) | ||
|
||
_, err := http.ReadRequest(bufioReader) | ||
if err != nil { | ||
conn.Write([]byte(BAD_RESPONSE)) | ||
return errors.New("malformed http health check request") | ||
} | ||
|
||
// TODO: eventually we should check things are working right, but for | ||
// now, just consider it a liveness check. | ||
conn.Write([]byte(OK_RESPONSE)) | ||
return nil | ||
} |
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,43 @@ | ||
package health | ||
|
||
import ( | ||
"bytes" | ||
"net" | ||
"testing" | ||
) | ||
|
||
func TestHealthCheckHandler(t *testing.T) { | ||
left, right := net.Pipe() | ||
c := make(chan []byte) | ||
bad := []byte("GET /health HTTP/xxxx") | ||
ok := []byte("GET /health HTTP/1.1\r\n\r\n") | ||
|
||
go func() { | ||
for i := 0; i < 2; i++ { | ||
buf := make([]byte, 128) | ||
n, err := right.Read(buf) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
c <- buf[:n] | ||
} | ||
}() | ||
|
||
err := HandleHealthCheck(left, bad) | ||
if err == nil { | ||
t.Fatal("expected to fail with bad healthcheck request") | ||
} | ||
msg := <-c | ||
if !bytes.Equal([]byte(BAD_RESPONSE), msg) { | ||
t.Fatal("expected bad response to healthcheck request") | ||
} | ||
|
||
err = HandleHealthCheck(left, ok) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
msg = <-c | ||
if !bytes.Equal([]byte(OK_RESPONSE), msg) { | ||
t.Fatal("expected OK response to healthcheck request") | ||
} | ||
} |
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