Add anyhttp idle support

This commit is contained in:
Balakrishnan Balasubramanian 2023-09-11 12:37:44 -04:00
parent 6f342c8888
commit eb032c50f1
3 changed files with 22 additions and 5 deletions

2
go.mod
View File

@ -8,3 +8,5 @@ require (
)
require golang.org/x/image v0.1.0 // indirect
replace go.balki.me/anyhttp => ../anyhttp

2
go.sum
View File

@ -1,6 +1,4 @@
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.balki.me/anyhttp v0.2.0 h1:W6aGcmjF5CMJvJYtbYCywxnYoErFhFc76vwaqUG5FAQ=
go.balki.me/anyhttp v0.2.0/go.mod h1:JhfekOIjgVODoVqUCficjpIgmB3wwlB7jhN0eN2EZ/s=
go.oneofone.dev/resize v1.0.1 h1:HjpVar/4pxMGrjO44ThaMX1Q5UOBw0KxzbxxRDZPQuA=
go.oneofone.dev/resize v1.0.1/go.mod h1:zGFmn7q4EUZVlnDmxqf+b0mWpxsTt0MH2yx6ng8tpq0=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=

23
main.go
View File

@ -1,6 +1,7 @@
package main
import (
"context"
"crypto/rand"
"embed"
"encoding/json"
@ -17,6 +18,7 @@ import (
"log/slog"
"go.balki.me/anyhttp"
"go.balki.me/anyhttp/idle"
"go.balki.me/collage-maker/collage"
)
@ -90,10 +92,25 @@ func main() {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Write([]byte(collageFile))
if _, err := w.Write([]byte(collageFile)); err != nil {
slog.Error("Failed to write collageFile", "error", err)
}
})
if err := anyhttp.ListenAndServe(listenAddr, nil); err != nil {
slog.Error("http ListenAndServe failed", "error", err)
addrType, server, err := anyhttp.ListenAndServeHTTP(listenAddr, idle.WrapHandler(nil))
if err != nil {
slog.Error("anyhttp ListenAndServeHTTP failed", "error", err)
}
if addrType == anyhttp.SystemdFD {
if err := idle.Wait(1 * time.Minute); err != nil {
slog.Error("Failed to wait for idler", "error", err)
}
ctx, _ := context.WithTimeout(context.Background(), 1*time.Minute) // Don't want any stuck connections
if err := server.Shutdown(ctx); err != nil {
slog.Error("http server Shutdown failed", "error", err)
}
} else {
select {}
}
}