Skip to content

Commit

Permalink
Start components with ctx, and wait for shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
louisroyer committed Dec 12, 2024
1 parent 99a5ab3 commit d07641f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
15 changes: 13 additions & 2 deletions internal/amf/amf.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Amf struct {
userAgent string
smf *smf.Smf
srv *http.Server
closed chan struct{}
}

func NewAmf(bindAddr netip.AddrPort, control jsonapi.ControlURI, userAgent string, smf *smf.Smf) *Amf {
Expand All @@ -35,6 +36,7 @@ func NewAmf(bindAddr netip.AddrPort, control jsonapi.ControlURI, userAgent strin
client: http.Client{},
userAgent: userAgent,
smf: smf,
closed: make(chan struct{}),
}
// TODO: gin.SetMode(gin.DebugMode) / gin.SetMode(gin.ReleaseMode) depending on log level
r := gin.Default()
Expand Down Expand Up @@ -65,19 +67,28 @@ func (amf *Amf) Start(ctx context.Context) error {
}
}(l)
go func(ctx context.Context) {
defer close(amf.closed)
select {
case <-ctx.Done():
ctxShutdown, cancel := context.WithTimeout(context.Background(), 1*time.Second)
ctxShutdown, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
if err := amf.srv.Shutdown(ctxShutdown); err != nil {
logrus.WithError(err).Info("HTTP Server Shutdown")
}
}
}(ctx)

return nil
}

func (amf *Amf) WaitShutdown(ctx context.Context) error {
select {
case <-ctx.Done():
return ctx.Err()
case <-amf.closed:
return nil
}
}

// get status of the controller
func Status(c *gin.Context) {
status := healthcheck.Status{
Expand Down
20 changes: 7 additions & 13 deletions internal/app/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package app

import (
"context"
"time"

"github.com/nextmn/cp-lite/internal/amf"
"github.com/nextmn/cp-lite/internal/config"
Expand All @@ -27,27 +28,20 @@ func NewSetup(config *config.CPConfig) *Setup {
smf: smf,
}
}
func (s *Setup) Init(ctx context.Context) error {

func (s *Setup) Run(ctx context.Context) error {
if err := s.smf.Start(ctx); err != nil {
return err
}
if err := s.amf.Start(ctx); err != nil {
return err
}
return nil
}

func (s *Setup) Run(ctx context.Context) error {
defer s.Exit()
if err := s.Init(ctx); err != nil {
return err
}
select {
case <-ctx.Done():
return nil
ctxShutdown, cancel := context.WithTimeout(ctx, 1*time.Second)
defer cancel()
s.amf.WaitShutdown(ctxShutdown)
s.smf.WaitShutdown(ctxShutdown)
}
}

func (s *Setup) Exit() error {
return nil
}
11 changes: 11 additions & 0 deletions internal/smf/smf.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Smf struct {
slices *SlicesMap
srv *pfcp.PFCPEntityCP
started bool
closed chan struct{}
}

func NewSmf(addr netip.Addr, slices map[string]config.Slice) *Smf {
Expand All @@ -35,12 +36,14 @@ func NewSmf(addr netip.Addr, slices map[string]config.Slice) *Smf {
srv: pfcp.NewPFCPEntityCP(addr.String(), addr),
slices: s,
upfs: upfs,
closed: make(chan struct{}),
}
}

func (smf *Smf) Start(ctx context.Context) error {
logrus.Info("Starting PFCP Server")
go func() {
defer close(smf.closed)
err := smf.srv.ListenAndServeContext(ctx)
if err != nil {
logrus.WithError(err).Trace("PFCP server stopped")
Expand Down Expand Up @@ -204,3 +207,11 @@ func (smf *Smf) CreateSessionUplink(ctx context.Context, ueCtrl jsonapi.ControlU
slice.sessions.Store(ueCtrl, &session)
return &session, nil
}
func (smf *Smf) WaitShutdown(ctx context.Context) error {
select {
case <-ctx.Done():
return ctx.Err()
case <-smf.closed:
return nil
}
}

0 comments on commit d07641f

Please sign in to comment.