Skip to content

Commit 629c8d2

Browse files
authored
Merge pull request #224 from thockin/meaningful-healthcheck
Make health check meaningful
2 parents 2eb8bf2 + 72deefc commit 629c8d2

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

cmd/git-sync/main.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"path/filepath"
3636
"strconv"
3737
"strings"
38+
"sync"
3839
"time"
3940

4041
"github.com/go-logr/glogr"
@@ -307,7 +308,12 @@ func main() {
307308

308309
// This is a dumb liveliness check endpoint. Currently this checks
309310
// nothing and will always return 200 if the process is live.
310-
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {})
311+
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
312+
if !getRepoReady() {
313+
http.Error(w, "repo is not ready", http.StatusServiceUnavailable)
314+
}
315+
// Otherwise success
316+
})
311317
http.Serve(ln, mux)
312318
}()
313319
}
@@ -454,6 +460,22 @@ func updateSymlink(ctx context.Context, gitRoot, link, newDir string) (string, e
454460
return oldWorktreePath, nil
455461
}
456462

463+
// repoReady indicates that the repo has been cloned and synced.
464+
var readyLock sync.Mutex
465+
var repoReady = false
466+
467+
func getRepoReady() bool {
468+
readyLock.Lock()
469+
defer readyLock.Unlock()
470+
return repoReady
471+
}
472+
473+
func setRepoReady() {
474+
readyLock.Lock()
475+
defer readyLock.Unlock()
476+
repoReady = true
477+
}
478+
457479
// addWorktreeAndSwap creates a new worktree and calls updateSymlink to swap the symlink to point to the new worktree
458480
func addWorktreeAndSwap(ctx context.Context, gitRoot, dest, branch, rev string, depth int, hash string) error {
459481
log.V(0).Info("syncing git", "rev", rev, "hash", hash)
@@ -525,9 +547,12 @@ func addWorktreeAndSwap(ctx context.Context, gitRoot, dest, branch, rev string,
525547
}
526548

527549
// Flip the symlink.
528-
if oldWorktree, err := updateSymlink(ctx, gitRoot, dest, worktreePath); err != nil {
550+
oldWorktree, err := updateSymlink(ctx, gitRoot, dest, worktreePath)
551+
if err != nil {
529552
return err
530-
} else if oldWorktree != "" {
553+
}
554+
setRepoReady()
555+
if oldWorktree != "" {
531556
// Clean up previous worktree
532557
log.V(1).Info("removing old worktree", "path", oldWorktree)
533558
if err := os.RemoveAll(oldWorktree); err != nil {

slow_git.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
#!/bin/sh
2+
3+
if [ "$1" != "clone" ]; then
4+
git "$@"
5+
exit $?
6+
fi
7+
28
sleep 1.1
39
git "$@"

test_e2e.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ function GIT_SYNC() {
133133
}
134134

135135
function remove_containers() {
136+
sleep 2 # Let docker finish saving container metadata
136137
docker ps --filter label="git-sync-e2e=$RUNID" --format="{{.ID}}" \
137138
| while read CTR; do
138139
docker kill "$CTR" >/dev/null
@@ -772,6 +773,7 @@ BINDPORT=8888
772773
echo "$TESTCASE 1" > "$REPO"/file
773774
git -C "$REPO" commit -qam "$TESTCASE 1"
774775
GIT_SYNC \
776+
--git="$SLOW_GIT" \
775777
--logtostderr \
776778
--v=5 \
777779
--repo="file://$REPO" \
@@ -781,6 +783,14 @@ GIT_SYNC \
781783
--http-pprof \
782784
--dest="link" \
783785
> "$DIR"/log."$TESTCASE" 2>&1 &
786+
while ! curl --silent --output /dev/null http://localhost:$BINDPORT; do
787+
# do nothing, just wait for the HTTP to come up
788+
true
789+
done
790+
# check that health endpoint fails
791+
if [[ $(curl --write-out %{http_code} --silent --output /dev/null http://localhost:$BINDPORT) -ne 503 ]] ; then
792+
fail "health endpoint should have failed: $(curl --write-out %{http_code} --silent --output /dev/null http://localhost:$BINDPORT)"
793+
fi
784794
sleep 2
785795
# check that health endpoint is alive
786796
if [[ $(curl --write-out %{http_code} --silent --output /dev/null http://localhost:$BINDPORT) -ne 200 ]] ; then

0 commit comments

Comments
 (0)