@rivascva/dt-idl 1.1.176 → 1.1.178
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/go/srv/srv.go +45 -0
- package/go/utils/context.go +9 -0
- package/package.json +1 -1
package/go/srv/srv.go
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
package srv
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"context"
|
|
5
|
+
"errors"
|
|
6
|
+
"fmt"
|
|
7
|
+
"net/http"
|
|
8
|
+
"time"
|
|
9
|
+
|
|
10
|
+
"github.com/RivasCVA/dt-idl/go/log"
|
|
11
|
+
"github.com/RivasCVA/dt-idl/go/utils"
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
// ListenAndServe starts the HTTP server and blocks until the context is cancelled.
|
|
15
|
+
// It then gracefully shuts down allowing in-flight requests to complete.
|
|
16
|
+
// The onShutdown functions are called after the server stops to clean up resources.
|
|
17
|
+
func ListenAndServe(ctx context.Context, log log.Logger, srv *http.Server, onShutdown []func()) {
|
|
18
|
+
// start the server in a separate goroutine
|
|
19
|
+
go func() {
|
|
20
|
+
log.Info(ctx, fmt.Sprintf("server running on %s", srv.Addr))
|
|
21
|
+
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
22
|
+
log.Error(ctx, err)
|
|
23
|
+
}
|
|
24
|
+
}()
|
|
25
|
+
|
|
26
|
+
// wait for the context to be cancelled
|
|
27
|
+
<-ctx.Done()
|
|
28
|
+
log.Info(ctx, "shutting down gracefully...")
|
|
29
|
+
|
|
30
|
+
// give in-flight requests time to complete
|
|
31
|
+
shutdownCtx, cancel := utils.DetachContext(ctx, 10*time.Second)
|
|
32
|
+
defer cancel()
|
|
33
|
+
|
|
34
|
+
// shutdown the server
|
|
35
|
+
if err := srv.Shutdown(shutdownCtx); err != nil {
|
|
36
|
+
log.Error(ctx, err)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// run cleanup functions
|
|
40
|
+
for _, fn := range onShutdown {
|
|
41
|
+
fn()
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
log.Info(ctx, "server stopped")
|
|
45
|
+
}
|
package/go/utils/context.go
CHANGED
|
@@ -4,6 +4,8 @@ import (
|
|
|
4
4
|
"context"
|
|
5
5
|
"errors"
|
|
6
6
|
"fmt"
|
|
7
|
+
"os/signal"
|
|
8
|
+
"syscall"
|
|
7
9
|
"time"
|
|
8
10
|
|
|
9
11
|
"github.com/RivasCVA/dt-idl/go/models"
|
|
@@ -90,3 +92,10 @@ func GetRequestIdFromContext(ctx context.Context) (string, error) {
|
|
|
90
92
|
func DetachContext(ctx context.Context, timeout time.Duration) (context.Context, context.CancelFunc) {
|
|
91
93
|
return context.WithTimeout(context.WithoutCancel(ctx), timeout)
|
|
92
94
|
}
|
|
95
|
+
|
|
96
|
+
// NewServerContext returns a new context that is canceled when the server receives an interrupt or kill signal.
|
|
97
|
+
// The context is returned along with a cancel function that can be used to cancel the context manually.
|
|
98
|
+
// It is recommended to use this context as the base context for a server and defer the cancel function.
|
|
99
|
+
func NewServerContext() (context.Context, context.CancelFunc) {
|
|
100
|
+
return signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
|
101
|
+
}
|