-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.go
More file actions
137 lines (115 loc) · 3.71 KB
/
main.go
File metadata and controls
137 lines (115 loc) · 3.71 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright The Linux Foundation and each contributor to LFX.
// SPDX-License-Identifier: MIT
package main
import (
"context"
"flag"
"fmt"
"log/slog"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/linuxfoundation/lfx-v2-query-service/cmd/service"
querysvc "github.com/linuxfoundation/lfx-v2-query-service/gen/query_svc"
logging "github.com/linuxfoundation/lfx-v2-query-service/pkg/log"
"goa.design/clue/debug"
)
const (
defaultPort = "8080"
// gracefulShutdownSeconds should be higher than NATS client
// request timeout, and lower than the pod or liveness probe's
// terminationGracePeriodSeconds.
gracefulShutdownSeconds = 25
)
func init() {
// slog is the standard library logger, we use it to log errors and
logging.InitStructureLogConfig()
}
func main() {
// Define command line flags, add any other flag required to configure the
// service.
var (
dbgF = flag.Bool("d", false, "enable debug logging")
port = flag.String("p", defaultPort, "listen port")
bind = flag.String("bind", "*", "interface to bind on")
)
flag.Usage = func() {
flag.PrintDefaults()
os.Exit(2)
}
flag.Parse()
ctx := context.Background()
slog.InfoContext(ctx, "Starting query service",
"bind", *bind,
"http-port", *port,
"graceful-shutdown-seconds", gracefulShutdownSeconds,
)
// Initialize the resource searcher based on configuration
resourceSearcher := service.SearcherImpl(ctx)
accessControlChecker := service.AccessControlCheckerImpl(ctx)
organizationSearcher := service.OrganizationSearcherImpl(ctx)
authService := service.AuthServiceImpl(ctx)
resourceFilter := service.ResourceFilterImpl(ctx)
// Initialize the services.
var (
querySvcSvc querysvc.Service
)
{
querySvcSvc = service.NewQuerySvc(resourceSearcher, accessControlChecker, resourceFilter, organizationSearcher, authService)
}
// Wrap the services in endpoints that can be invoked from other services
// potentially running in different processes.
querySvcEndpoints := querysvc.NewEndpoints(querySvcSvc)
querySvcEndpoints.Use(debug.LogPayloads())
// Create channel used by both the signal handler and server goroutines
// to notify the main goroutine when to stop the server.
errc := make(chan error)
// Setup interrupt handler. This optional step configures the process so
// that SIGINT and SIGTERM signals cause the services to stop gracefully.
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
errc <- fmt.Errorf("%s", <-c)
}()
var wg sync.WaitGroup
ctx, cancel := context.WithCancel(ctx)
// Start the servers and send errors (if any) to the error channel.
addr := ":" + *port
if *bind != "*" {
addr = *bind + ":" + *port
}
handleHTTPServer(ctx, addr, querySvcEndpoints, &wg, errc, *dbgF)
// Wait for signal.
slog.InfoContext(ctx, "received shutdown signal, stopping servers",
"signal", <-errc,
)
// Send cancellation signal to the goroutines.
cancel()
// Create a timeout context for graceful shutdown
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), gracefulShutdownSeconds*time.Second)
defer shutdownCancel()
// Gracefully close the access control checker
go func() {
if accessControlChecker != nil {
slog.InfoContext(shutdownCtx, "closing access control checker")
if err := accessControlChecker.Close(); err != nil {
slog.ErrorContext(shutdownCtx, "failed to close access control checker", "error", err)
}
}
}()
// Wait for all goroutines to finish with timeout
done := make(chan struct{})
go func() {
wg.Wait()
close(done)
}()
select {
case <-done:
slog.InfoContext(ctx, "graceful shutdown completed")
case <-shutdownCtx.Done():
slog.WarnContext(ctx, "graceful shutdown timed out")
}
slog.InfoContext(ctx, "exited")
}