Skip to content

Instantly share code, notes, and snippets.

@billyfbrain
billyfbrain / callback.rs
Last active May 25, 2019 12:00
Callback in struct
struct Foo {
callback: Option<Box<dyn Fn()>>,
}
impl Foo {
fn new() -> Self {
Self {
callback: None,
}
}
@billyfbrain
billyfbrain / server.go
Created August 16, 2016 23:19
stop server
type Server struct {
ctx context.Context
Stop context.CancelFunc
}
func (s *Server) IsShutdown() bool {
select {
case <-s.ctx.Done():
return true
default:
package storage
import (
"encoding/json"
"io"
"os"
"sync"
"sync/atomic"
"github.com/boltdb/bolt"
@billyfbrain
billyfbrain / graceful_tcp_server.go
Last active December 18, 2022 07:45
golang graceful shutdown tcp listener
cmdAddr, _ := net.ResolveTCPAddr("tcp", n.cfg.Addr)
lcmd, err := net.ListenTCP("tcp", cmdAddr)
if err != nil {
log.Fatalln(err)
}
defer lcmd.Close()
quitChan := make(chan os.Signal, 1)
signal.Notify(quitChan, os.Interrupt, os.Kill, syscall.SIGTERM)
wg := sync.WaitGroup{}
for {