MCPcopy
hub / github.com/kopia/kopia / run

Method run

cli/command_server_start.go:199–310  ·  view source on GitHub ↗
(ctx context.Context)

Source from the content-addressed store, hash-verified

197}
198
199func (c *commandServerStart) run(ctx context.Context) (reterr error) {
200 if err := insecureserverbind.ValidateListenAddressIfRestricted(
201 c.serverStartInsecure,
202 c.serverStartWithoutPassword,
203 c.serverStartAllowDangerousUnauthenticatedNetwork,
204 c.sf.serverAddress,
205 ); err != nil {
206 return errors.Wrap(err, "listen address not allowed for insecure server without password")
207 }
208
209 opts, err := c.serverStartOptions(ctx)
210 if err != nil {
211 return err
212 }
213
214 srv, err := server.New(ctx, opts)
215 if err != nil {
216 return errors.Wrap(err, "unable to initialize server")
217 }
218
219 if err = c.initRepositoryPossiblyAsync(ctx, srv); err != nil {
220 return errors.Wrap(err, "unable to initialize repository")
221 }
222
223 defer func() {
224 // cleanup: disconnect repository
225 if err := srv.SetRepository(ctx, nil); err != nil {
226 reterr = stderrors.Join(reterr, errors.Wrap(err, "error disconnecting repository"))
227 }
228 }()
229
230 httpServer := &http.Server{
231 ReadHeaderTimeout: 15 * time.Second, //nolint:mnd
232 Addr: stripProtocol(c.sf.serverAddress),
233 BaseContext: func(_ net.Listener) context.Context {
234 return ctx
235 },
236 }
237
238 srv.OnShutdown = func(ctx context.Context) error {
239 ctx2, cancel := context.WithTimeout(ctx, c.shutdownGracePeriod)
240 defer cancel()
241
242 // wait for all connections to finish within a shutdown grace period
243 log(ctx2).Debugf("attempting graceful shutdown for %v", c.shutdownGracePeriod)
244
245 // Gracefully shutdown GRPC server first to close GRPC connections
246 log(ctx2).Debug("shutting down GRPC server")
247 srv.ShutdownGRPCServer()
248 log(ctx2).Debug("GRPC server shutdown completed")
249
250 if serr := httpServer.Shutdown(ctx2); serr != nil {
251 // graceful shutdown unsuccessful, force close
252 log(ctx2).Debugf("unable to shut down gracefully - closing: %v", serr)
253 return errors.Wrap(httpServer.Close(), "close")
254 }
255
256 log(ctx2).Debug("graceful shutdown succeeded")

Callers

nothing calls this directly

Calls 15

serverStartOptionsMethod · 0.95
setupHandlersMethod · 0.95
NewFunction · 0.92
NewJSONSenderFunction · 0.92
shutdownHTTPServerFunction · 0.85
initPrometheusFunction · 0.85
ShutdownGRPCServerMethod · 0.80
ShutdownMethod · 0.80
GRPCRouterHandlerMethod · 0.80

Tested by

no test coverage detected