Skip to content

Commit

Permalink
Fix incorrect disassociate RPC call and correctly handle gRPC connect…
Browse files Browse the repository at this point in the history
…ions (#53)

* Fix incorrect disassociate RPC call

* Close connections to avoid memory leaks

* Implement disconnect method
  • Loading branch information
EmanueleGallone authored Mar 10, 2022
1 parent 9414eb2 commit 1da9a55
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
11 changes: 10 additions & 1 deletion internal/pfcpctl/commands/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,21 @@ import (
"google.golang.org/grpc/credentials/insecure"
)

var conn *grpc.ClientConn

func connect() pb.PFCPSimClient {
// Create an insecure gRPC Channel
conn, err := grpc.Dial(config.GlobalConfig.Server, grpc.WithTransportCredentials(insecure.NewCredentials()))
var err error
conn, err = grpc.Dial(config.GlobalConfig.Server, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Fatalf("Error dialing %v: %v", config.GlobalConfig.Server, err)
}

return pb.NewPFCPSimClient(conn)
}

func disconnect() {
if conn != nil {
conn.Close()
}
}
6 changes: 5 additions & 1 deletion internal/pfcpctl/commands/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ func RegisterServiceCommands(parser *flags.Parser) {

func (c *configureRemoteAddresses) Execute(args []string) error {
client := connect()
defer disconnect()

res, err := client.Configure(context.Background(), &pb.ConfigureRequest{
UpfN3Address: c.N3InterfaceAddress,
RemotePeerAddress: c.RemotePeerAddress,
})

if err != nil {
log.Fatalf("Error while configuring remote addresses: %v", err)
}
Expand All @@ -46,6 +48,7 @@ func (c *configureRemoteAddresses) Execute(args []string) error {

func (c *associate) Execute(args []string) error {
client := connect()
defer disconnect()

res, err := client.Associate(context.Background(), &pb.EmptyRequest{})
if err != nil {
Expand All @@ -59,8 +62,9 @@ func (c *associate) Execute(args []string) error {

func (c *disassociate) Execute(args []string) error {
client := connect()
defer disconnect()

res, err := client.Associate(context.Background(), &pb.EmptyRequest{})
res, err := client.Disassociate(context.Background(), &pb.EmptyRequest{})
if err != nil {
log.Fatalf("Error while disassociating: %v", err)
}
Expand Down
3 changes: 3 additions & 0 deletions internal/pfcpctl/commands/sessions.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func (s *sessionCreate) Execute(args []string) error {
}

client := connect()
defer disconnect()

res, err := client.CreateSession(context.Background(), &pb.CreateSessionRequest{
Count: int32(s.Args.Count),
Expand All @@ -78,6 +79,7 @@ func (s *sessionCreate) Execute(args []string) error {

func (s *sessionModify) Execute(args []string) error {
client := connect()
defer disconnect()

res, err := client.ModifySession(context.Background(), &pb.ModifySessionRequest{
Count: int32(s.Args.Count),
Expand All @@ -99,6 +101,7 @@ func (s *sessionModify) Execute(args []string) error {

func (s *sessionDelete) Execute(args []string) error {
client := connect()
defer disconnect()

res, err := client.DeleteSession(context.Background(), &pb.DeleteSessionRequest{
Count: int32(s.Args.Count),
Expand Down

0 comments on commit 1da9a55

Please sign in to comment.