Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "context"
- "io"
- "log"
- "net"
- "os"
- "os/signal"
- "sync"
- "syscall"
- "time"
- "github.com/cloudflare/tableflip"
- )
- func handleConnection(ctx context.Context, wg *sync.WaitGroup, c net.Conn) {
- defer wg.Done()
- select {
- case <-ctx.Done():
- return
- default:
- io.WriteString(c, "topsecret")
- }
- }
- func handleDataStream(ctx context.Context, wg *sync.WaitGroup, upg *tableflip.Upgrader, addr string) {
- defer wg.Done()
- // i can handle parent listeners here if they was created with such arguments in parent process, or create new listener
- l, _ := upg.Fds.Listen("tcp", addr)
- for {
- select {
- case <-ctx.Done():
- return
- default:
- l.(*net.TCPListener).SetDeadline(time.Now().Add(time.Second * 3))
- c, err := l.Accept() // now i want to store all accepted connections in order to pass them on to the child process in the future
- if err != nil {
- continue
- }
- wg.Add(1)
- go handleConnection(ctx, wg, c)
- }
- }
- }
- func main() {
- wg := &sync.WaitGroup{}
- ctx, cancel := context.WithCancel(context.Background())
- upg, _ := tableflip.New(tableflip.Options{})
- defer upg.Stop()
- listenOn := []string{":8080", ":8085", "8090"}
- for _, addr := range listenOn {
- wg.Add(1)
- go handleDataStream(ctx, wg, upg, addr)
- }
- for _, c := range upg.InheritConnections() { // i want to continue processing all parent connections if they exist.
- wg.Add(1)
- go handleConnection(ctx, wg, c)
- }
- go func() {
- sig := make(chan os.Signal, 1)
- signal.Notify(sig, syscall.SIGHUP)
- <-sig
- cancel()
- wg.Wait()
- _ := upg.Upgrade()
- }()
- _ = upg.Ready()
- <-upg.Exit()
- }
Add Comment
Please, Sign In to add comment