Skip to content

Instantly share code, notes, and snippets.

@dio
Last active March 4, 2022 07:05
Show Gist options
  • Save dio/a9903e64ae02a3d654786298f755efea to your computer and use it in GitHub Desktop.
Save dio/a9903e64ae02a3d654786298f755efea to your computer and use it in GitHub Desktop.
package main
import (
"context"
"fmt"
"net"
"net/http"
"os"
"os/signal"
"time"
"github.com/lestrrat-go/rungroup"
)
var g rungroup.Group
func main() {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()
g.Add(rungroup.ActorFunc(func(ctx context.Context) error {
handler := http.NewServeMux()
handler.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("ok"))
})
s := http.Server{
Handler: handler,
}
l, err := net.Listen("tcp", ":8000")
if err != nil {
return err
}
fmt.Println("serve...")
err = s.Serve(l)
if err != nil {
return err
}
<-ctx.Done()
fmt.Println("this is not called?")
timeoutCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
return s.Shutdown(timeoutCtx)
}))
g.Add(rungroup.ActorFunc(func(ctx context.Context) error {
<-ctx.Done()
fmt.Println("this is called ")
return nil
}))
if err := <-g.Run(ctx); err != nil {
fmt.Println(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment