Skip to content

Commit

Permalink
Add WaitReady(ctx) error to PFCPEntityInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
louisroyer committed Dec 12, 2024
1 parent 788cba7 commit 24fe848
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
### UPF

```golang
ctx := context.TODO()
upNode := NewPFCPEntityUP(UPF_NODE_ID, UPF_IP_ADDR) // node id can be an IP Address or a FQDN
go upNode.ListenAndServe()
defer upNode.Close()
go upNode.ListenAndServeContext(ctx)
upnode.WaitReady(ctx)
// Access list of associations
associations := upNode.GetPFCPAssociations()
// Access list of sessions
Expand All @@ -22,9 +23,10 @@ sessions := upNode.GetPFCPSessions()
### SMF

```golang
ctx := context.TODO()
cpNode := NewPFCPEntityCP(SMF_NODE_ID, SMF_IP_ADDR) // node id can be an IP Address or a FQDN
go cpNode.ListenAndServe()
defer cpNode.Close()
go cpNode.ListenAndServeContext(ctx)
cpNode.WaitReady(ctx)
association, _ := cpNode.NewEstablishedPFCPAssociation(ie.NewNodeIDHeuristic(UPFADDR))
session, _ := a.CreateSession(pdrs, fars)

Expand Down
2 changes: 2 additions & 0 deletions pfcp/api/entity_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package api

import (
"context"
"net/netip"

"github.com/wmnsk/go-pfcp/ie"
Expand All @@ -25,4 +26,5 @@ type PFCPEntityInterface interface {
AddEstablishedPFCPSession(session PFCPSessionInterface) error
LogPFCPRules()
Options() EntityOptionsInterface
WaitReady(ctx context.Context) error
}
15 changes: 15 additions & 0 deletions pfcp/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ type PFCPEntity struct {
mu sync.Mutex
pfcpConns []*onceClosePfcpConn
closeFunc context.CancelFunc

// check start
waitReady chan struct{}
}

// onceClosePfcpConn wraps a PfcpConn, protecting it from multiple Close calls.
Expand Down Expand Up @@ -133,6 +136,7 @@ func NewPFCPEntity(nodeID string, listenAddr netip.Addr, kind string, handlers m
kind: kind,
options: options,
pfcpConns: nil,
waitReady: make(chan struct{}),
}
}

Expand Down Expand Up @@ -224,6 +228,16 @@ func (e *PFCPEntity) ListenAndServeContext(ctx context.Context) error {
}
}

// Blocks until the PFCPEntity is ready or context is done
func (e *PFCPEntity) WaitReady(ctx context.Context) error {
select {
case <-e.waitReady:
return nil
case <-ctx.Done():
return ctx.Err()
}
}

// Run the entity with the provided context.
// Always return a non-nil error and close the PFCPConn.
func (e *PFCPEntity) Serve(ctx context.Context, conn *PFCPConn) error {
Expand All @@ -240,6 +254,7 @@ func (e *PFCPEntity) Serve(ctx context.Context, conn *PFCPConn) error {
select {
case <-serveCtx.Done():
// Stop signal received
close(e.waitReady)
return serveCtx.Err()
default:
buf := make([]byte, pfcputil.DEFAULT_MTU) // TODO: get MTU of interface instead of using DEFAULT_MTU
Expand Down

0 comments on commit 24fe848

Please sign in to comment.