Skip to content

Commit

Permalink
Update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
vardius committed Jun 12, 2020
1 parent e68dde7 commit 52be71f
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 31 deletions.
57 changes: 28 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,40 +35,39 @@ For **GoDoc** reference, **visit [pkg.go.dev](https://pkg.go.dev/github.com/vard
package main

import (
"context"
"net/http"
"os"
"time"
"log"
"context"
"fmt"
"log"
"net/http"
"syscall"
"time"

"github.com/vardius/shutdown"
)

func main() {
ctx := context.Background()

http.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
io.WriteString(w, "Hello!\n")
})

stop := func() {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()

log.Printf("shutting down...\n")

if err := srv.Shutdown(ctx); err != nil {
log.Printf("shutdown error: %v\n", err)
} else {
log.Printf("gracefully stopped\n")
}
}

go func() {
log.Printf("%v\n", http.ListenAndServe(":8080", nil))
stop()
os.Exit(1)
}()
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, "Hello!")
})

httpServer := &http.Server{
Addr: ":8080",
Handler: mux,
}

stop := func() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

if err := httpServer.Shutdown(ctx); err != nil {
log.Printf("shutdown error: %v\n", err)
} else {
log.Printf("gracefully stopped\n")
}
}

shutdown.GracefulStop(stop) // will block until shutdown signal is received
}
```

Expand Down
42 changes: 40 additions & 2 deletions example_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package shutdown_test

import (
"context"
"fmt"
"log"
"net/http"
"syscall"
"time"

"github.com/vardius/shutdown"
)

func Example() {
// mock shutdown signall Ctrl + C
// mock shutdown signal Ctrl + C
go func() {
time.Sleep(10 * time.Millisecond)
syscall.Kill(syscall.Getpid(), syscall.SIGINT)
Expand All @@ -24,7 +27,7 @@ func Example() {
}

func Example_second() {
// mock shutdown signall Ctrl + C followed by second Ctrl + C
// mock shutdown signal Ctrl + C followed by second Ctrl + C
go func() {
// first signal interrupt
time.Sleep(10 * time.Millisecond)
Expand All @@ -42,3 +45,38 @@ func Example_second() {

// Output:
}

func Example_third() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, "Hello!")
})

httpServer := &http.Server{
Addr: ":8080",
Handler: mux,
}

stop := func() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

if err := httpServer.Shutdown(ctx); err != nil {
log.Printf("shutdown error: %v\n", err)
} else {
log.Printf("gracefully stopped\n")
}
}

// mock shutdown signal Ctrl + C
go func() {
// first signal interrupt
time.Sleep(10 * time.Millisecond)
syscall.Kill(syscall.Getpid(), syscall.SIGINT)
}()

shutdown.GracefulStop(stop)

// Output:
// gracefully stopped
}

0 comments on commit 52be71f

Please sign in to comment.