mirror of
https://github.com/labstack/echo.git
synced 2025-03-23 21:29:26 +02:00
Updated graceful shutdown recipe with go1.8
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
parent
a098bcd3b0
commit
e7c89c424f
43
cookbook/graceful-shutdown/server.go
Normal file
43
cookbook/graceful-shutdown/server.go
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/labstack/echo"
|
||||||
|
"github.com/labstack/gommon/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// Setup
|
||||||
|
e := echo.New()
|
||||||
|
e.Logger.SetLevel(log.INFO)
|
||||||
|
e.GET("/", func(c echo.Context) error {
|
||||||
|
time.Sleep(5 * time.Second)
|
||||||
|
return c.JSON(http.StatusOK, "OK")
|
||||||
|
})
|
||||||
|
|
||||||
|
// Start server
|
||||||
|
go func() {
|
||||||
|
if err := e.Start(":1323"); err != nil {
|
||||||
|
e.Logger.Info("shutting down the server")
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Wait for interrupt signal to gracefully shutdown the server with
|
||||||
|
// a timeout of 10 seconds.
|
||||||
|
quit := make(chan os.Signal)
|
||||||
|
signal.Notify(quit, os.Interrupt)
|
||||||
|
<-quit
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
if err := e.Shutdown(ctx); err != nil {
|
||||||
|
e.Logger.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
25
echo_go1.8.go
Normal file
25
echo_go1.8.go
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
// +build go1.8
|
||||||
|
|
||||||
|
package echo
|
||||||
|
|
||||||
|
import (
|
||||||
|
stdContext "context"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Close immediately stops the server.
|
||||||
|
// It internally calls `http.Server#Close()`.
|
||||||
|
func (e *Echo) Close() error {
|
||||||
|
if err := e.TLSServer.Close(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return e.Server.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Shutdown stops server the gracefully.
|
||||||
|
// It internally calls `http.Server#Shutdown()`.
|
||||||
|
func (e *Echo) Shutdown(ctx stdContext.Context) error {
|
||||||
|
if err := e.TLSServer.Shutdown(ctx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return e.Server.Shutdown(ctx)
|
||||||
|
}
|
30
echo_go1.8_test.go
Normal file
30
echo_go1.8_test.go
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
// +build go1.8
|
||||||
|
|
||||||
|
package echo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestEchoClose(t *testing.T) {
|
||||||
|
e := New()
|
||||||
|
errCh := make(chan error)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
errCh <- e.Start(":0")
|
||||||
|
}()
|
||||||
|
|
||||||
|
time.Sleep(200 * time.Millisecond)
|
||||||
|
|
||||||
|
if err := e.Close(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.NoError(t, e.Close())
|
||||||
|
|
||||||
|
err := <-errCh
|
||||||
|
assert.Equal(t, err.Error(), "http: Server closed")
|
||||||
|
}
|
@ -7,6 +7,14 @@ description = "Graceful shutdown example for Echo"
|
|||||||
weight = 13
|
weight = 13
|
||||||
+++
|
+++
|
||||||
|
|
||||||
|
## Using [http.Server#Shutdown()](https://golang.org/pkg/net/http/#Server.Shutdown)
|
||||||
|
|
||||||
|
`server.go`
|
||||||
|
|
||||||
|
{{< embed "graceful-shutdown/server.go" >}}
|
||||||
|
|
||||||
|
> Requires go1.8+
|
||||||
|
|
||||||
## Using [grace](https://github.com/facebookgo/grace)
|
## Using [grace](https://github.com/facebookgo/grace)
|
||||||
|
|
||||||
`server.go`
|
`server.go`
|
||||||
|
Loading…
x
Reference in New Issue
Block a user