Coder Social home page Coder Social logo

ailncode / af Goto Github PK

View Code? Open in Web Editor NEW
20.0 1.0 2.0 23 KB

You can use this package write a graceful reload HTTP server easily

License: BSD 2-Clause "Simplified" License

Go 100.00%
go golang graceful reload restart zero down times hot upgrade http

af's Introduction

What is AF

The name of AF comes from air refueling.

AF is a package of Go.

You can use the AF write a graceful reload HTTP server easily.

Support

  • *nix
  • Windows (only build and run)

Features

  • graceful reload
  • graceful stop
  • custom signal handler

Install

go get -u github.com/ailncode/af
#or
go mod edit -require=github.com/ailncode/af@latest

Usage

  1. Import the AF
import "github.com/ailncode/af"
  1. Simple use
package main

import(
	"fmt"
	"net/http"
	"github.com/ailncode/af"
)

func main(){
    http.HandleFunc("/",func(w http.ResponseWriter,r *http.Request) {
		fmt.Fprintln(w, "ok")
	})
    refuel := af.Default()
    //refuel.Addr = ":8080"
    //:8080 is the default Addr
    //refuel.Handler = http.DefaultServeMux
    //http.DefaultServeMux is the default HTTP handler
    //refuel.ShutdownTimeOut = time.Second * 10
    //time.Second * 10 is the default ShutdownTimeOut
    refuel.Run()
    //You can check error in here.
}
  1. Reload & Stop your server
#Reload
kill -USR2 <pid>
#Stop
kill -INT <pid> #or kill -TERM <pid>
  1. Use your custom Listen Address Handler ShutdownTimeOut
package main

import(
    "net/http"
    "time"
    "github.com/ailncode/af"
    "github.com/gin-gonic/gin"
)

func main(){
    //Use handler like *gin.Engine
    router :=gin.Default()
	router.GET("/", func(context *gin.Context) {
		context.String(http.StatusOK,"ok")
	})
    refuel := af.Default()
    refuel.Handler = router
    //You can use the below code also.
    //af := af.NewWithHandler(r)
    refuel.Addr = ":80"
    refuel.ShutdownTimeOut = time.Second * 10
    refuel.Run()
}
  1. Use your custom system signal handler
package main

import (
	"github.com/ailncode/af"
	"syscall"
	"time"
)

func main() {
	refuel := af.Default()
	refuel.ShutdownTimeOut = time.Second * 10
	refuel.HandleSignal(func(r *af.AF) {
		r.Stop()
	}, syscall.SIGINT, syscall.SIGTERM)
	refuel.HandleSignal(func(r *af.AF) {
		r.Reload()
		//You can check error in here
	}, syscall.SIGUSR2)
	refuel.Run()
}
  1. Use your custom server
package main

import (
	"af"
	"net/http"
)

func main() {
	server := &http.Server{
		Addr:"8080",
		Handler:http.DefaultServeMux,
		//...
	}
	refuel := af.NewWithServer(server)
	refuel.Run()
}

af's People

Contributors

ailncode avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

af's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.