miyabi

package module
v0.0.0-...-7fc17ba Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2015 License: MIT Imports: 14 Imported by: 4

README

Miyabi Build Status

Graceful shutdown and restart for Go's net/http handlers.

Miyabi is pronounced me-ya-be. It means Graceful in Japanese.

Usage

It's very simple. Use miyabi.ListenAndServe instead of http.ListenAndServe. You don't have to change other code because miyabi.ListenAndServe is compatible with http.ListenAndServe.

package main

import (
    "io"
    "log"
    "net/http"

    "github.com/naoina/miyabi"
)

// hello world, the web server
func HelloServer(w http.ResponseWriter, req *http.Request) {
    io.WriteString(w, "hello, world!\n")
}

func main() {
    http.HandleFunc("/hello", HelloServer)
    log.Fatal(miyabi.ListenAndServe(":8080", nil))
}

See Godoc for more information.

NOTE: Miyabi is using features of Go 1.3, so doesn't work in Go 1.2.x and older versions. Also when using on Windows, it works but graceful shutdown/restart are disabled explicitly.

Graceful shutdown or restart

By default, send SIGTERM or SIGINT (Ctrl + c) signal to a process that is using Miyabi in order to graceful shutdown and send SIGHUP signal in order to graceful restart. If you want to change the these signal, please set another signal to miyabi.ShutdownSignal and/or miyabi.RestartSignal.

In fact, miyabi.ListenAndServe and miyabi.ListenAndServeTLS will fork a process that is using Miyabi in order to achieve the graceful restart. This means that you should write code as no side effects until the call of miyabi.ListenAndServe or miyabi.ListenAndServeTLS.

License

Miyabi is licensed under the MIT.

Documentation

Overview

Package miyabi provides graceful version of net/http compatible HTTP server.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ShutdownSignal is the signal for graceful shutdown.
	// syscall.SIGTERM by default. Please set another signal if you want.
	ShutdownSignal = syscall.SIGTERM

	// RestartSignal is the signal for graceful restart.
	// syscall.SIGHUP by default. Please set another signal if you want.
	RestartSignal = syscall.SIGHUP

	// Timeout specifies the timeout for terminate of the old process.
	// A zero value disables the timeout.
	Timeout = 3 * time.Minute

	// ServerState specifies the optional callback function that is called
	// when the server changes state. See the State type and associated
	// constants for details.
	ServerState func(state State)

	// FDEnvKey is the environment variable name of inherited file descriptor for graceful restart.
	FDEnvKey = "MIYABI_FD"
)

Functions

func IsMaster

func IsMaster() bool

IsMaster returns whether the current process is master.

func ListenAndServe

func ListenAndServe(addr string, handler http.Handler) error

ListenAndServe acts like http.ListenAndServe but can be graceful shutdown and restart. If addr begin with "unix:", will listen on a Unix domain socket instead of TCP.

func ListenAndServeTLS

func ListenAndServeTLS(addr, certFile, keyFile string, handler http.Handler) error

ListenAndServeTLS acts like http.ListenAndServeTLS but can be graceful shutdown and restart.

Types

type Server

type Server http.Server

Server is similar to http.Server. However, ListenAndServe, ListenAndServeTLS and Serve can be graceful shutdown and restart.

func (*Server) ListenAndServe

func (srv *Server) ListenAndServe() error

ListenAndServe acts like http.Server.ListenAndServe but can be graceful shutdown and restart. If srv.Addr begin with "unix:", will listen on a Unix domain socket instead of TCP.

func (*Server) ListenAndServeTLS

func (srv *Server) ListenAndServeTLS(certFile, keyFile string) error

ListenAndServeTLS acts like http.Server.ListenAndServeTLS but can be graceful shutdown and restart.

func (*Server) Serve

func (srv *Server) Serve(l net.Listener) error

Serve acts like http.Server.Serve but can be graceful shutdown. If you want to graceful restart, use ListenAndServe or ListenAndServeTLS instead.

func (*Server) SetKeepAlivesEnabled

func (srv *Server) SetKeepAlivesEnabled(v bool)

SetKeepAlivesEnabled is same as http.Server.SetKeepAlivesEnabled.

type State

type State uint8

A State represents the state of the server. It's used by the optional ServerState hook.

const (
	// StateStart represents a state that server has been started.
	StateStart State = iota

	// StateRestart represents a state that server has been restarted.
	StateRestart

	// StateShutdown represents a state that server has been shutdown.
	StateShutdown
)

func (State) String

func (i State) String() string

Jump to

Keyboard shortcuts

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