Compare commits
2 Commits
6b6b22092e
...
v0.2.0
| Author | SHA1 | Date | |
|---|---|---|---|
| 2a021b453d | |||
| 03bde25c6d |
@@ -7,11 +7,12 @@ import (
|
||||
"time"
|
||||
|
||||
"golang.org/x/exp/slog"
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
|
||||
// HealthCheckListener runs simple http server on the specified port for
|
||||
// health check probes
|
||||
func HealthCheckListener(ctx context.Context, port int, log *slog.Logger) {
|
||||
func HealthCheckListener(ctx context.Context, port int, log *slog.Logger) error {
|
||||
log.Info("Starting health listener", "port", port)
|
||||
|
||||
serveMux := http.NewServeMux()
|
||||
@@ -26,19 +27,31 @@ func HealthCheckListener(ctx context.Context, port int, log *slog.Logger) {
|
||||
Handler: serveMux,
|
||||
}
|
||||
|
||||
go func() {
|
||||
g, ctx := errgroup.WithContext(ctx)
|
||||
|
||||
g.Go(func() error {
|
||||
err := srv.ListenAndServe()
|
||||
if err != http.ErrServerClosed {
|
||||
log.Warn("health check server done listening", "err", err)
|
||||
return err
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
})
|
||||
|
||||
<-ctx.Done()
|
||||
|
||||
if err := srv.Shutdown(ctx); err != nil {
|
||||
log.Error("health check server shutdown failed", "err", err)
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
|
||||
defer cancel()
|
||||
|
||||
g.Go(func() error {
|
||||
if err := srv.Shutdown(ctx); err != nil {
|
||||
log.Error("health check server shutdown failed", "err", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
return g.Wait()
|
||||
}
|
||||
|
||||
func basicHealth(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
36
timeutil/duration.go
Normal file
36
timeutil/duration.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package timeutil
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Duration struct {
|
||||
time.Duration
|
||||
}
|
||||
|
||||
func (d Duration) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(time.Duration(d.Duration).String())
|
||||
}
|
||||
|
||||
func (d *Duration) UnmarshalJSON(b []byte) error {
|
||||
var v interface{}
|
||||
if err := json.Unmarshal(b, &v); err != nil {
|
||||
return err
|
||||
}
|
||||
switch value := v.(type) {
|
||||
case float64:
|
||||
*d = Duration{time.Duration(value)}
|
||||
return nil
|
||||
case string:
|
||||
tmp, err := time.ParseDuration(value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*d = Duration{tmp}
|
||||
return nil
|
||||
default:
|
||||
return errors.New("invalid duration")
|
||||
}
|
||||
}
|
||||
22
timeutil/duration_test.go
Normal file
22
timeutil/duration_test.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package timeutil
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDuration(t *testing.T) {
|
||||
js := []byte(`{"foo": "30s"}`)
|
||||
|
||||
foo := struct{ Foo Duration }{}
|
||||
|
||||
err := json.Unmarshal(js, &foo)
|
||||
if err != nil {
|
||||
t.Fatalf("could not unmarshal %q: %s", js, err)
|
||||
}
|
||||
|
||||
if foo.Foo.Seconds() != 30 {
|
||||
t.Fatalf("parsed time.Duration wasn't 30 seconds: %s", foo.Foo)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user