From 1da9a55cda5c83ddb2726c25a514fe578c363884 Mon Sep 17 00:00:00 2001 From: Emanuele Gallone <26747011+EmanueleGallone@users.noreply.github.com> Date: Thu, 10 Mar 2022 10:10:59 +0100 Subject: [PATCH] Fix incorrect disassociate RPC call and correctly handle gRPC connections (#53) * Fix incorrect disassociate RPC call * Close connections to avoid memory leaks * Implement disconnect method --- internal/pfcpctl/commands/helpers.go | 11 ++++++++++- internal/pfcpctl/commands/services.go | 6 +++++- internal/pfcpctl/commands/sessions.go | 3 +++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/internal/pfcpctl/commands/helpers.go b/internal/pfcpctl/commands/helpers.go index 99a7a83..7431bd1 100644 --- a/internal/pfcpctl/commands/helpers.go +++ b/internal/pfcpctl/commands/helpers.go @@ -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() + } +} diff --git a/internal/pfcpctl/commands/services.go b/internal/pfcpctl/commands/services.go index 100beb4..48e8053 100644 --- a/internal/pfcpctl/commands/services.go +++ b/internal/pfcpctl/commands/services.go @@ -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) } @@ -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 { @@ -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) } diff --git a/internal/pfcpctl/commands/sessions.go b/internal/pfcpctl/commands/sessions.go index 57c3045..e1f0fb7 100644 --- a/internal/pfcpctl/commands/sessions.go +++ b/internal/pfcpctl/commands/sessions.go @@ -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), @@ -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), @@ -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),