├── .travis.yml ├── LICENSE ├── example └── main.go ├── README.md ├── pidproxy.py └── gracegrpc.go /.travis.yml: -------------------------------------------------------------------------------- 1 | language: go 2 | 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2018 0x5010 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /example/main.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * modified from grpc-go/examples/helloworld/greeter_server/main.go 4 | * 5 | */ 6 | 7 | package main 8 | 9 | import ( 10 | "log" 11 | 12 | "github.com/0x5010/gracegrpc" // import 13 | "golang.org/x/net/context" 14 | "google.golang.org/grpc" 15 | pb "google.golang.org/grpc/examples/helloworld/helloworld" 16 | "google.golang.org/grpc/reflection" 17 | ) 18 | 19 | const ( 20 | port = ":50051" 21 | ) 22 | 23 | type server struct{} 24 | 25 | func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) { 26 | return &pb.HelloReply{Message: "Hello " + in.Name}, nil 27 | } 28 | 29 | func main() { 30 | // lis, err := net.Listen("tcp", port) 31 | // if err != nil { 32 | // log.Fatalf("failed to listen: %v", err) 33 | // } 34 | // s := grpc.NewServer() 35 | // pb.RegisterGreeterServer(s, &server{}) 36 | // // Register reflection service on gRPC server. 37 | // reflection.Register(s) 38 | // if err := s.Serve(lis); err != nil { 39 | // log.Fatalf("failed to serve: %v", err) 40 | // } 41 | 42 | pidPath := "example.pid" 43 | s := grpc.NewServer() 44 | pb.RegisterGreeterServer(s, &server{}) 45 | reflection.Register(s) 46 | gr, err := gracegrpc.New(s, "tcp", port, pidPath, nil) 47 | if err != nil { 48 | log.Fatalf("failed to new gracegrpc: %v", err) 49 | } 50 | if err := gr.Serve(); err != nil { 51 | log.Fatalf("failed to serve: %v", err) 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | gracegrpc is used to wrap a grpc server that can be gracefully terminated & restarted. 2 | 3 | [![LICENSE](https://img.shields.io/badge/license-MIT-orange.svg)](LICENSE) 4 | [![Build Status](https://travis-ci.org/0x5010/gracegrpc.png?branch=master)](https://travis-ci.org/0x5010/gracegrpc) 5 | [![Go Report Card](https://goreportcard.com/badge/github.com/0x5010/gracegrpc)](https://goreportcard.com/report/github.com/0x5010/gracegrpc) 6 | [![Godoc](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)](https://godoc.org/github.com/0x5010/gracegrpc) 7 | 8 | Installation 9 | ----------- 10 | 11 | go get github.com/0x5010/gracegrpc 12 | 13 | Usage 14 | ----------- 15 | 16 | Wrap grpc server and start server: 17 | ```go 18 | s := grpc.NewServer() 19 | pb.RegisterDeployServer(s, &server{}) 20 | reflection.Register(s) 21 | gr, err := gracegrpc.New(s, "tcp", addr, pidPath, nil) 22 | if err != nil { 23 | log.Fatalf("failed to new gracegrpc: %v", err) 24 | } 25 | if err := gr.Serve(); err != nil { 26 | log.Fatalf("failed to serve: %v", err) 27 | } 28 | 29 | ``` 30 | 31 | Custom logger 32 | ```go 33 | import log "github.com/sirupsen/logrus" 34 | 35 | ... 36 | gr, err := gracegrpc.New(s, "tcp", addr, pidPath, log.StandardLogger()) 37 | ... 38 | ``` 39 | 40 | In a terminal trigger a graceful server restart (using the pid from your output): 41 | ```bash 42 | kill -USR2 pid 43 | ``` 44 | 45 | Run with supervisor 46 | ```conf 47 | command = /path/to/pidproxy.py /path/to/pidPath /path/to/server 48 | 49 | kill -USR2 {pidproxy.py pid} 50 | ``` 51 | -------------------------------------------------------------------------------- /pidproxy.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ An executable which proxies for a subprocess; upon a signal, it sends that 4 | signal to the process identified by a pidfile. """ 5 | 6 | import os 7 | import sys 8 | import signal 9 | import time 10 | 11 | 12 | class PidProxy: 13 | pid = None 14 | def __init__(self, args): 15 | self.setsignals() 16 | self.reaped_parent = False 17 | 18 | try: 19 | self.pidfile, cmdargs = args[1], args[2:] 20 | self.command = os.path.abspath(cmdargs[0]) 21 | self.cmdargs = cmdargs 22 | except (ValueError, IndexError): 23 | self.usage() 24 | sys.exit(1) 25 | 26 | def getpidfromfile(self): 27 | try: 28 | with open(self.pidfile, 'r') as f: 29 | return int(f.read().strip()) 30 | except: 31 | return None 32 | 33 | def go(self): 34 | self.pid = os.spawnv(os.P_NOWAIT, self.command, self.cmdargs) 35 | while 1: 36 | time.sleep(5) 37 | try: 38 | self.pid = self.getpidfromfile() 39 | if self.pid is None: 40 | break 41 | pid, sts = os.waitpid(self.pid, os.WNOHANG) 42 | except OSError: 43 | pid, sts = None, None 44 | if pid: 45 | break 46 | 47 | def usage(self): 48 | print("pidproxy.py [ ...]") 49 | 50 | def setsignals(self): 51 | signal.signal(signal.SIGTERM, self.passtochild) 52 | signal.signal(signal.SIGHUP, self.passtochild) 53 | signal.signal(signal.SIGINT, self.passtochild) 54 | signal.signal(signal.SIGUSR1, self.passtochild) 55 | signal.signal(signal.SIGUSR2, self.passtochild) 56 | signal.signal(signal.SIGQUIT, self.passtochild) 57 | signal.signal(signal.SIGCHLD, self.reap) 58 | 59 | def reap(self, sig, frame): 60 | if not self.reaped_parent: 61 | os.waitpid(-1, 0) 62 | self.reaped_parent = True 63 | return 64 | try: 65 | pid = self.getpidfromfile() 66 | if pid is None: 67 | pid = self.pid 68 | _, _ = os.waitpid(pid, 0) 69 | sys.exit(0) 70 | except: 71 | pass 72 | 73 | def passtochild(self, sig, frame): 74 | pid = self.getpidfromfile() 75 | if pid is None: 76 | pid = self.pid 77 | os.kill(pid, sig) 78 | if sig in [signal.SIGTERM, signal.SIGINT, signal.SIGQUIT]: 79 | sys.exit(0) 80 | 81 | 82 | def main(): 83 | pp = PidProxy(sys.argv) 84 | pp.go() 85 | 86 | if __name__ == '__main__': 87 | main() 88 | -------------------------------------------------------------------------------- /gracegrpc.go: -------------------------------------------------------------------------------- 1 | package gracegrpc 2 | 3 | /* 4 | * 5 | * Created by 0x5010 on 2018/02/01. 6 | * gracegrpc 7 | * https://github.com/0x5010/gracegrpc 8 | * 9 | * Copyright 2018 0x5010. 10 | * Licensed under the MIT license. 11 | * 12 | */ 13 | 14 | import ( 15 | "fmt" 16 | "log" 17 | "net" 18 | "os" 19 | "os/signal" 20 | "syscall" 21 | 22 | "github.com/facebookgo/grace/gracenet" 23 | "google.golang.org/grpc" 24 | ) 25 | 26 | var ( 27 | // Used to indicate a graceful restart in the new process. 28 | envKey = "LISTEN_FDS" 29 | ppid = os.Getppid() 30 | ) 31 | 32 | type logger interface { 33 | Printf(string, ...interface{}) 34 | } 35 | 36 | // GraceGrpc is used to wrap a grpc server that can be gracefully terminated & restarted 37 | type GraceGrpc struct { 38 | server *grpc.Server 39 | grace *gracenet.Net 40 | listener net.Listener 41 | errors chan error 42 | pidPath string 43 | logger logger 44 | } 45 | 46 | // New is used to construct a new GraceGrpc 47 | func New(s *grpc.Server, net, addr, pidPath string, l logger) (*GraceGrpc, error) { 48 | if l == nil { 49 | l = log.New(os.Stderr, "", log.LstdFlags) 50 | } 51 | gr := &GraceGrpc{ 52 | server: s, 53 | grace: &gracenet.Net{}, 54 | 55 | //for StartProcess error. 56 | errors: make(chan error), 57 | pidPath: pidPath, 58 | logger: l, 59 | } 60 | listener, err := gr.grace.Listen(net, addr) 61 | if err != nil { 62 | return nil, err 63 | } 64 | gr.listener = listener 65 | return gr, nil 66 | } 67 | 68 | func (gr *GraceGrpc) startServe() { 69 | if err := gr.server.Serve(gr.listener); err != nil { 70 | gr.errors <- err 71 | } 72 | } 73 | 74 | func (gr *GraceGrpc) handleSignal() <-chan struct{} { 75 | terminate := make(chan struct{}) 76 | go func() { 77 | ch := make(chan os.Signal, 10) 78 | signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM, syscall.SIGUSR2) 79 | for { 80 | sig := <-ch 81 | switch sig { 82 | case syscall.SIGINT, syscall.SIGTERM: 83 | signal.Stop(ch) 84 | gr.server.GracefulStop() 85 | close(terminate) 86 | return 87 | case syscall.SIGUSR2: 88 | if _, err := gr.grace.StartProcess(); err != nil { 89 | gr.errors <- err 90 | } 91 | } 92 | } 93 | }() 94 | return terminate 95 | } 96 | 97 | // storePid is used to write out PID to pidPath 98 | func (gr *GraceGrpc) storePid(pid int) error { 99 | pidPath := gr.pidPath 100 | if pidPath == "" { 101 | return fmt.Errorf("No pid file path: %s", pidPath) 102 | } 103 | 104 | pidFile, err := os.OpenFile(pidPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666) 105 | if err != nil { 106 | return fmt.Errorf("Could not open pid file: %v", err) 107 | } 108 | defer pidFile.Close() 109 | 110 | _, err = pidFile.WriteString(fmt.Sprintf("%d", pid)) 111 | if err != nil { 112 | return fmt.Errorf("Could not write to pid file: %s", err) 113 | } 114 | return nil 115 | } 116 | 117 | // Serve is used to start grpc server. 118 | // Serve will gracefully terminated or restarted when handling signals. 119 | func (gr *GraceGrpc) Serve() error { 120 | if gr.listener == nil || gr.logger == nil { 121 | return fmt.Errorf("gracegrpc must construct by new") 122 | } 123 | 124 | inherit := os.Getenv(envKey) != "" 125 | pid := os.Getpid() 126 | addrString := gr.listener.Addr().String() 127 | 128 | if inherit { 129 | if ppid == 1 { 130 | gr.logger.Printf("Listening on init activated %s\n", addrString) 131 | } else { 132 | gr.logger.Printf("Graceful handoff of %s with new pid %d replace old pid %d\n", addrString, pid, ppid) 133 | } 134 | } else { 135 | gr.logger.Printf("Serving %s with pid %d\n", addrString, pid) 136 | } 137 | 138 | if err := gr.storePid(pid); err != nil { 139 | return err 140 | } 141 | 142 | go gr.startServe() 143 | 144 | if inherit && ppid != 1 { 145 | if err := syscall.Kill(ppid, syscall.SIGTERM); err != nil { 146 | return fmt.Errorf("failed to close parent: %s", err) 147 | } 148 | } 149 | 150 | terminate := gr.handleSignal() 151 | 152 | select { 153 | case err := <-gr.errors: 154 | return err 155 | case <-terminate: 156 | gr.logger.Printf("Exiting pid %d.", os.Getpid()) 157 | return nil 158 | } 159 | } 160 | --------------------------------------------------------------------------------