yes, i d say so.
About contexts,
https://www.youtube.com/watch?v=LSzR0VEraWwhttps://www.youtube.com/watch?v=8M90t0KvEDYFrom scratch,
with some mocks to test&try.
package main
import (
"context"
"log"
"time"
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Millisecond)
defer cancel()
var err error
listenSig := make(chan error)
go func() { listenSig <- <-listenAndServe() }()//IRL, not sure if that will return if server has shutdown.
// this is unclear at https://beta.golang.org/src/net/http/server.go?s=79791:79837#L2615
// i d say its all about timeouts.
select {
case err = <-listenSig:
log.Println("server soft end")
case <-ctx.Done():
log.Println("context ended")
log.Println("server hard stop")
if err = shutdown( /*context.Background()*/ ); err == nil {
err = ctx.Err()
}
log.Println("server ended")
}
log.Println(err)
}
func listenAndServe() chan error {
e := make(chan error)
go func() {
<-time.After(time.Second)
e <- nil
}()
return e
}
func shutdown() error {
<-time.After(2 * time.Second)
return nil
}
that being said, server.Shutdown seems blocking,
https://beta.golang.org/pkg/net/http/#Server.Shutdown>
Shutdown ... waiting
indefinitely for connections to return to idle and then shut down.
https://beta.golang.org/src/net/http/server.go?s=74304:74358#L2440If so, d go func that one and put its err to a sink logger.
It will hang there until all clients are out, so the server must be configured with timeouts.
Or use a context with deadline ?
I m not sure what s the best, maybe both are needed ?
It looks likes this ctx parameter is sugar only.
Apart, does server.Shutdown allows a straight restart ?
+/- like if i d use socket_reuse port/addr?