gss

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 24, 2020 License: Apache-2.0 Imports: 6 Imported by: 1

Documentation

Overview

Example
package main

import (
	"fmt"
	"net/http"

	"github.com/TsvetanMilanov/go-graceful-server-shutdown/gss"
)

func main() {
	// Create server based on the provided handler.
	// The server will listen on port 8080 and there will be
	// no timeout for the connection draining.
	// StartServer will block the program execution until the server is closed.
	err := gss.StartServer(http.DefaultServeMux)

	fmt.Println(err)
}
Output:

Index

Examples

Constants

View Source
const (
	// DefaultAddr The default server address.
	DefaultAddr = ":8080"
	// DefaultShutdownTimeoutSeconds the default timeout which the module will wait for
	// connections to drain. -1 means that the module will wait until all connections
	// are drained.
	DefaultShutdownTimeoutSeconds = -1
)

Variables

View Source
var (
	// DefaultSignalsToWatch The default signals which will trigger the server shutdown.
	DefaultSignalsToWatch = []os.Signal{syscall.SIGINT, syscall.SIGTERM}
)

Functions

func StartCustomServerWithSettings

func StartCustomServerWithSettings(srv *http.Server, settings *Settings) error

StartCustomServerWithSettings starts the provided server and handles its shutdown based on the provided settings. This function is blocking.

Example
package main

import (
	"fmt"
	"net/http"
	"os"
	"syscall"

	"github.com/TsvetanMilanov/go-graceful-server-shutdown/gss"
)

func main() {
	settings := &gss.Settings{
		// Set the signals which will trigger the graceful server shutdown.
		SignalsToWatch: []os.Signal{syscall.SIGTERM, syscall.SIGKILL, syscall.SIGINT},
	}

	srv := &http.Server{Handler: http.DefaultServeMux, Addr: ":5678"}

	// StartServerWithSettings will block the program execution until the server is closed.
	err := gss.StartCustomServerWithSettings(srv, settings)

	fmt.Println(err)
}
Output:

func StartServer

func StartServer(router http.Handler) error

StartServer creates server from the provided handler, starts it and handles its shutdown based on the default settings. This function is blocking.

Example
package main

import (
	"fmt"
	"net/http"

	"github.com/TsvetanMilanov/go-graceful-server-shutdown/gss"
)

func main() {
	// StartServer will block the program execution until the server is closed.
	err := gss.StartServer(http.DefaultServeMux)

	fmt.Println(err)
}
Output:

func StartServerWithSettings

func StartServerWithSettings(router http.Handler, settings *Settings) error

StartServerWithSettings creates server from the provided handler, starts it and handles its shutdown based on the provided settings. This function is blocking.

Example
package main

import (
	"fmt"
	"net/http"
	"os"
	"syscall"

	"github.com/TsvetanMilanov/go-graceful-server-shutdown/gss"
)

func main() {
	var shutdownTimeoutSeconds int64 = 30
	settings := &gss.Settings{
		// Set the server address.
		Addr: ":5678",
		// Set the signals which will trigger the graceful server shutdown.
		SignalsToWatch: []os.Signal{syscall.SIGTERM, syscall.SIGKILL, syscall.SIGINT},
		// Set shutdown timeout.
		ShutdownTimeoutSeconds: &shutdownTimeoutSeconds,
	}

	// StartServerWithSettings will block the program execution until the server is closed.
	err := gss.StartServerWithSettings(http.DefaultServeMux, settings)

	fmt.Println(err)
}
Output:

Example (Shutdownchannel)
package main

import (
	"fmt"
	"net/http"
	"time"

	"github.com/TsvetanMilanov/go-graceful-server-shutdown/gss"
)

func main() {
	shutdownChannel := make(chan bool)
	settings := &gss.Settings{
		ShutdownChannel: shutdownChannel,
	}

	serverChannel := make(chan error)
	go func() {
		err := gss.StartServerWithSettings(http.DefaultServeMux, settings)

		serverChannel <- err
	}()

	time.Sleep(500 * time.Millisecond)

	shutdownChannel <- true

	err := <-serverChannel

	fmt.Println(err)
}
Output:

<nil>

Types

type Settings

type Settings struct {
	Addr                   string
	SignalsToWatch         []os.Signal
	ShutdownChannel        chan bool
	ShutdownTimeoutSeconds *int64
}

Settings the settings which will be used when handling the server shutdown.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL