MCPcopy Create free account
hub / github.com/cortexproject/cortex / NewServerService

Function NewServerService

pkg/cortex/server_service.go:19–55  ·  view source on GitHub ↗

NewServerService constructs service from Server component. servicesToWaitFor is called when server is stopping, and should return all services that need to terminate before server actually stops. N.B.: this function is NOT Cortex specific, please let's keep it that way. Passed server should not reac

(serv *server.Server, servicesToWaitFor func() []services.Service)

Source from the content-addressed store, hash-verified

17// N.B.: this function is NOT Cortex specific, please let's keep it that way.
18// Passed server should not react on signals. Early return from Run function is considered to be an error.
19func NewServerService(serv *server.Server, servicesToWaitFor func() []services.Service) services.Service {
20 serverDone := make(chan error, 1)
21
22 runFn := func(ctx context.Context) error {
23 go func() {
24 defer close(serverDone)
25 serverDone <- serv.Run()
26 }()
27
28 select {
29 case <-ctx.Done():
30 return nil
31 case err := <-serverDone:
32 if err != nil {
33 return err
34 }
35 return fmt.Errorf("server stopped unexpectedly")
36 }
37 }
38
39 stoppingFn := func(_ error) error {
40 // wait until all modules are done, and then shutdown server.
41 for _, s := range servicesToWaitFor() {
42 _ = s.AwaitTerminated(context.Background())
43 }
44
45 // shutdown HTTP and gRPC servers (this also unblocks Run)
46 serv.Shutdown()
47
48 // if not closed yet, wait until server stops.
49 <-serverDone
50 level.Info(util_log.Logger).Log("msg", "server stopped")
51 return nil
52 }
53
54 return services.NewBasicService(nil, runFn, stoppingFn)
55}
56
57// DisableSignalHandling puts a dummy signal handler
58func DisableSignalHandling(config *server.Config) {

Callers 4

initServerMethod · 0.85
TestServerStopViaContextFunction · 0.85
TestServerStopViaStopFunction · 0.85

Calls 6

NewBasicServiceFunction · 0.92
DoneMethod · 0.80
ShutdownMethod · 0.80
RunMethod · 0.65
AwaitTerminatedMethod · 0.65
LogMethod · 0.45

Tested by 3

TestServerStopViaContextFunction · 0.68
TestServerStopViaStopFunction · 0.68