MCPcopy Index your code
hub / github.com/google/go-cloud / ExampleServer_Shutdown

Function ExampleServer_Shutdown

server/example_test.go:129–166  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

127}
128
129func ExampleServer_Shutdown() {
130 // OPTIONAL: Specify a driver in the options for the constructor.
131 // NewDefaultDriver will be used by default if it is not explicitly set, and
132 // uses http.Server with read, write, and idle timeouts set. When Shutdown
133 // is called on the server, it is called on the driver.
134 srvOptions := &server.Options{
135 Driver: server.NewDefaultDriver(),
136 }
137
138 // Pass the options to the Server constructor.
139 srv := server.New(http.DefaultServeMux, srvOptions)
140
141 // If your application will be behind a load balancer that handles graceful
142 // shutdown of requests, you may not need to call Shutdown on the server
143 // directly. If you need to ensure graceful shutdown directly, it is important
144 // to have a separate goroutine, because ListenAndServe blocks indefinitely.
145 go func() {
146 interrupt := make(chan os.Signal, 1)
147 signal.Notify(interrupt, os.Interrupt)
148 // Receive off the chanel in a loop, because the interrupt could be sent
149 // before ListenAndServe starts.
150 for {
151 <-interrupt
152 srv.Shutdown(context.Background())
153 }
154 }()
155
156 // Register a route.
157 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
158 fmt.Fprintln(w, "Hello, World!")
159 })
160
161 // Start the server. You will see requests logged to STDOUT.
162 // In the absence of an error, ListenAndServe blocks forever.
163 if err := srv.ListenAndServe(":8080"); err != nil {
164 log.Fatalf("%v", err)
165 }
166}

Callers

nothing calls this directly

Calls 4

NewDefaultDriverFunction · 0.92
NewFunction · 0.92
ShutdownMethod · 0.65
ListenAndServeMethod · 0.65

Tested by

no test coverage detected