Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start components with ctx, and wait for shutdown #8

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}
Loading