Last active
March 4, 2022 07:05
-
-
Save dio/a9903e64ae02a3d654786298f755efea to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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