Original post

The below is the code for grpc client, my question is inside NewLogisticsService function, can I not invoke conn.close() here? Will there be any problem if I don’t do it explicitly? Because if I do, the conn will be closed before sending requests.

package logisticsService

import (
        "context"
        "errors"
        "fmt"
        "strconv"
        "time"

        _ "github.com/joho/godotenv/autoload"
        pb "github.com/storehubnet/service_cron_jobs-scripts/logistics/v2/grpc/logistics"
        "google.golang.org/grpc"
        "google.golang.org/protobuf/reflect/protoreflect"
)

const (
        CONNECT_TIMEOUT_IN_SECONDS = 10
        CALL_TIMEOUT_IN_SECONDS    = 40
)

const (
        TRACKING_JOB_STATUS         = "trackingJobStatus"
        VERIFYING_JOB_STATUS        = "verifyingJobStatus"
        SEND_PREORDER_NOTIFICATIONS = "sendPreorderNotifications"
        CHECK_ONDEMAND_JOB_CREATION = "checkOnDemandJobCreation"
        UNCLAIMED_PREORDER_HANDLER  = "unclaimedPreOrderHandler"
)

type logisticsService struct {
        Client pb.LogisticsClient
}

func NewLogisticsService(ADDRESS string, PORT string) (l *logisticsService, e error) {
        conn, err := grpc.Dial(fmt.Sprintf("%s:%s", ADDRESS, PORT), grpc.WithInsecure(), grpc.WithBlock(), grpc.WithTimeout(time.Second*CONNECT_TIMEOUT_IN_SECONDS))
        if err != nil {
                return nil, err
        }
        // defer conn.Close() ?
        return &logisticsService{Client: pb.NewLogisticsClient(conn)}, nil
}

type responseInterface interface {
        Reset()
        String() string
        ProtoMessage()
        ProtoReflect() protoreflect.Message
        Descriptor() ([]byte, []int)
        GetCode() pb.ResponseCode
}

func (this *logisticsService) Execute(functionName string, input []string) (r responseInterface, e error) {

        ctx, cancel := context.WithTimeout(context.Background(), time.Second*CALL_TIMEOUT_IN_SECONDS)
        defer cancel()
        var err error
        var res responseInterface
        switch functionName {
        case TRACKING_JOB_STATUS:
                res, err = this.Client.JobStatusTrackingProcess(ctx, &pb.JobStatusTrackingReq{})
                break
        case VERIFYING_JOB_STATUS:
                res, err = this.Client.VerifyingJobStatusProcess(ctx, &pb.VerifyingJobStatusReq{})
                break
        case SEND_PREORDER_NOTIFICATIONS:
                res, err = this.Client.SendPreorderNotificationsProcess(ctx, &pb.SendPreorderNotificationsReq{})
                break
        case CHECK_ONDEMAND_JOB_CREATION:
                res, err = this.Client.CheckOnDemandJobCreationProcess(ctx, &pb.CheckOnDemandJobReq{})
                break
        case UNCLAIMED_PREORDER_HANDLER:
                if len(input) < 2 {
                        return nil, errors.New("Too few argument for this function")
                }
                peekInSeconds, err := strconv.Atoi(input[1])
                if err != nil {
                        return nil, err
                }
                res, err = this.Client.UnclaimedPreOrderHandlerProcess(ctx, &pb.UnclaimedPreOrderHandlerReq{Country: input[0], PeekJobsInSeconds: int32(peekInSeconds)})
                break
        }

        return res, err
}