Welcome to mirror list, hosted at ThFree Co, Russian Federation.

readiness.go « server « service « praefect « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0e26c351fa2e08b28a91ebed799385d7273eb9ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package server

import (
	"context"
	"io"
	"sort"

	"gitlab.com/gitlab-org/gitaly/v16/proto/go/gitalypb"
)

// ReadinessCheck runs the set of the checks to make sure service is in operational state.
func (s *Server) ReadinessCheck(ctx context.Context, req *gitalypb.ReadinessCheckRequest) (*gitalypb.ReadinessCheckResponse, error) {
	checkCtx := ctx
	checkCancel := func() {}
	timeout := req.GetTimeout().AsDuration()
	if req.GetTimeout().IsValid() && timeout > 0 {
		checkCtx, checkCancel = context.WithTimeout(ctx, timeout)
	}
	defer checkCancel()

	results := make(chan *gitalypb.ReadinessCheckResponse_Failure_Response, len(s.checks))
	for _, newCheck := range s.checks {
		check := newCheck(s.conf, io.Discard, true)
		go func() {
			if err := check.Run(checkCtx); err != nil {
				results <- &gitalypb.ReadinessCheckResponse_Failure_Response{
					Name:         check.Name,
					ErrorMessage: err.Error(),
				}
			} else {
				results <- nil
			}
		}()
	}

	var failedChecks []*gitalypb.ReadinessCheckResponse_Failure_Response
	for i := 0; i < cap(results); i++ {
		if result := <-results; result != nil {
			failedChecks = append(failedChecks, result)
		}
	}

	if len(failedChecks) > 0 {
		sort.Slice(failedChecks, func(i, j int) bool { return failedChecks[i].Name < failedChecks[j].Name })
		return &gitalypb.ReadinessCheckResponse{Result: &gitalypb.ReadinessCheckResponse_FailureResponse{
			FailureResponse: &gitalypb.ReadinessCheckResponse_Failure{
				FailedChecks: failedChecks,
			},
		}}, nil
	}

	return &gitalypb.ReadinessCheckResponse{Result: &gitalypb.ReadinessCheckResponse_OkResponse{}}, nil
}