Skip to content

Commit

Permalink
Merge pull request #209 from weaveworks-liquidmetal/fix_grpc_connecti…
Browse files Browse the repository at this point in the history
…on_close

fix: explicitly close grpc client connection
  • Loading branch information
richardcase authored Aug 18, 2022
2 parents af96cb5 + c9ad8c8 commit 8c6b50d
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 3 deletions.
2 changes: 2 additions & 0 deletions controllers/microvmmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ func (r *MicrovmMachineReconciler) reconcileNormal(
return ctrl.Result{}, err
}

defer mvmSvc.Dispose()

var microvm *flintlocktypes.MicroVM

providerID := machineScope.GetProviderID()
Expand Down
7 changes: 4 additions & 3 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,10 @@ func NewFlintlockClient(address string, opts ...Options) (microvm.Client, error)
return nil, fmt.Errorf("creating grpc connection: %w", err)
}

flClient := flintlockv1.NewMicroVMClient(conn)

return flClient, nil
return &disposableClient{
flintlockClient: flintlockv1.NewMicroVMClient(conn),
conn: conn,
}, nil
}

func buildConfig(opts ...Options) clientConfig {
Expand Down
40 changes: 40 additions & 0 deletions internal/client/disposable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package client

import (
"context"

flintlockv1 "github.com/weaveworks-liquidmetal/flintlock/api/services/microvm/v1alpha1"
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/emptypb"
)

type disposableClient struct {
flintlockClient flintlockv1.MicroVMClient
conn *grpc.ClientConn
}

func (c *disposableClient) Dispose() {
if c.conn != nil {
c.conn.Close()
}
}

func (c *disposableClient) CreateMicroVM(ctx context.Context, in *flintlockv1.CreateMicroVMRequest, opts ...grpc.CallOption) (*flintlockv1.CreateMicroVMResponse, error) { //nolint:lll // it would make it less readable
return c.flintlockClient.CreateMicroVM(ctx, in, opts...)
}

func (c *disposableClient) DeleteMicroVM(ctx context.Context, in *flintlockv1.DeleteMicroVMRequest, opts ...grpc.CallOption) (*emptypb.Empty, error) { //nolint:lll // it would make it less readable
return c.flintlockClient.DeleteMicroVM(ctx, in, opts...)
}

func (c *disposableClient) GetMicroVM(ctx context.Context, in *flintlockv1.GetMicroVMRequest, opts ...grpc.CallOption) (*flintlockv1.GetMicroVMResponse, error) { //nolint:lll // it would make it less readable
return c.flintlockClient.GetMicroVM(ctx, in, opts...)
}

func (c *disposableClient) ListMicroVMs(ctx context.Context, in *flintlockv1.ListMicroVMsRequest, opts ...grpc.CallOption) (*flintlockv1.ListMicroVMsResponse, error) { //nolint:lll // it would make it less readable
return c.flintlockClient.ListMicroVMs(ctx, in, opts...)
}

func (c *disposableClient) ListMicroVMsStream(ctx context.Context, in *flintlockv1.ListMicroVMsRequest, opts ...grpc.CallOption) (flintlockv1.MicroVM_ListMicroVMsStreamClient, error) { //nolint:lll // it would make it less readable
return c.flintlockClient.ListMicroVMsStream(ctx, in, opts...)
}
30 changes: 30 additions & 0 deletions internal/services/microvm/mock_client/fake_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions internal/services/microvm/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (

type Client interface {
flintlockv1.MicroVMClient
Dispose()
}

type Service struct {
Expand Down Expand Up @@ -113,6 +114,10 @@ func (s *Service) Delete(ctx context.Context) (*emptypb.Empty, error) {
return s.client.DeleteMicroVM(ctx, input)
}

func (s *Service) Dispose() {
s.client.Dispose()
}

func (s *Service) addMetadata(apiMicroVM *flintlocktypes.MicroVMSpec) error {
bootstrapData, err := s.scope.GetRawBootstrapData()
if err != nil {
Expand Down

0 comments on commit 8c6b50d

Please sign in to comment.