Skip to content

Commit

Permalink
Merge pull request containerd#10657 from djdongjin/reduce-ptypes-Empt…
Browse files Browse the repository at this point in the history
…y-creation

Avoid repeated ptypes.Empty creation by predefining it as a var
  • Loading branch information
mxpv authored Sep 3, 2024
2 parents 0a29a8c + 9c34005 commit 4b28e29
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 14 deletions.
8 changes: 5 additions & 3 deletions plugins/services/containers/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import (
"google.golang.org/grpc/status"
)

var empty = &ptypes.Empty{}

func init() {
registry.Register(&plugin.Registration{
Type: plugins.ServicePlugin,
Expand Down Expand Up @@ -188,16 +190,16 @@ func (l *local) Delete(ctx context.Context, req *api.DeleteContainerRequest, _ .
if err := l.withStoreUpdate(ctx, func(ctx context.Context) error {
return l.Store.Delete(ctx, req.ID)
}); err != nil {
return &ptypes.Empty{}, errdefs.ToGRPC(err)
return empty, errdefs.ToGRPC(err)
}

if err := l.publisher.Publish(ctx, "/containers/delete", &eventstypes.ContainerDelete{
ID: req.ID,
}); err != nil {
return &ptypes.Empty{}, err
return empty, err
}

return &ptypes.Empty{}, nil
return empty, nil
}

func (l *local) withStore(ctx context.Context, fn func(ctx context.Context) error) func(tx *bolt.Tx) error {
Expand Down
6 changes: 4 additions & 2 deletions plugins/services/events/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import (
"google.golang.org/grpc"
)

var empty = &ptypes.Empty{}

func init() {
registry.Register(&plugin.Registration{
Type: plugins.GRPCPlugin,
Expand Down Expand Up @@ -84,15 +86,15 @@ func (s *service) Publish(ctx context.Context, r *api.PublishRequest) (*ptypes.E
return nil, errdefs.ToGRPC(err)
}

return &ptypes.Empty{}, nil
return empty, nil
}

func (s *service) Forward(ctx context.Context, r *api.ForwardRequest) (*ptypes.Empty, error) {
if err := s.events.Forward(ctx, fromProto(r.Envelope)); err != nil {
return nil, errdefs.ToGRPC(err)
}

return &ptypes.Empty{}, nil
return empty, nil
}

func (s *service) Subscribe(req *api.SubscribeRequest, srv api.Events_SubscribeServer) error {
Expand Down
2 changes: 1 addition & 1 deletion plugins/services/events/ttrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (s *ttrpcService) Forward(ctx context.Context, r *api.ForwardRequest) (*pty
return nil, errdefs.ToGRPC(err)
}

return &ptypes.Empty{}, nil
return empty, nil
}

func fromTProto(env *types.Envelope) *events.Envelope {
Expand Down
4 changes: 3 additions & 1 deletion plugins/services/images/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import (
"github.com/containerd/plugin/registry"
)

var empty = &ptypes.Empty{}

func init() {
registry.Register(&plugin.Registration{
Type: plugins.ServicePlugin,
Expand Down Expand Up @@ -183,7 +185,7 @@ func (l *local) Delete(ctx context.Context, req *imagesapi.DeleteImageRequest, _
}
}

return &ptypes.Empty{}, nil
return empty, nil
}

func (l *local) emitSchema1DeprecationWarning(ctx context.Context, image *images.Image) {
Expand Down
8 changes: 5 additions & 3 deletions plugins/services/leases/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import (
"google.golang.org/grpc"
)

var empty = &ptypes.Empty{}

func init() {
registry.Register(&plugin.Registration{
Type: plugins.GRPCPlugin,
Expand Down Expand Up @@ -87,7 +89,7 @@ func (s *service) Delete(ctx context.Context, r *api.DeleteRequest) (*ptypes.Emp
}, opts...); err != nil {
return nil, errdefs.ToGRPC(err)
}
return &ptypes.Empty{}, nil
return empty, nil
}

func (s *service) List(ctx context.Context, r *api.ListRequest) (*api.ListResponse, error) {
Expand Down Expand Up @@ -117,7 +119,7 @@ func (s *service) AddResource(ctx context.Context, r *api.AddResourceRequest) (*
}); err != nil {
return nil, errdefs.ToGRPC(err)
}
return &ptypes.Empty{}, nil
return empty, nil
}

func (s *service) DeleteResource(ctx context.Context, r *api.DeleteResourceRequest) (*ptypes.Empty, error) {
Expand All @@ -131,7 +133,7 @@ func (s *service) DeleteResource(ctx context.Context, r *api.DeleteResourceReque
}); err != nil {
return nil, errdefs.ToGRPC(err)
}
return &ptypes.Empty{}, nil
return empty, nil
}

func (s *service) ListResources(ctx context.Context, r *api.ListResourcesRequest) (*api.ListResourcesResponse, error) {
Expand Down
8 changes: 5 additions & 3 deletions plugins/services/namespaces/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import (
"google.golang.org/grpc/status"
)

var empty = &ptypes.Empty{}

func init() {
registry.Register(&plugin.Registration{
Type: plugins.ServicePlugin,
Expand Down Expand Up @@ -206,17 +208,17 @@ func (l *local) Delete(ctx context.Context, req *api.DeleteNamespaceRequest, _ .
if err := l.withStoreUpdate(ctx, func(ctx context.Context, store namespaces.Store) error {
return errdefs.ToGRPC(store.Delete(ctx, req.Name))
}); err != nil {
return &ptypes.Empty{}, err
return empty, err
}
// set the namespace in the context before publishing the event
ctx = namespaces.WithNamespace(ctx, req.Name)
if err := l.publisher.Publish(ctx, "/namespaces/delete", &eventstypes.NamespaceDelete{
Name: req.Name,
}); err != nil {
return &ptypes.Empty{}, err
return empty, err
}

return &ptypes.Empty{}, nil
return empty, nil
}

func (l *local) withStore(ctx context.Context, fn func(ctx context.Context, store namespaces.Store) error) func(tx *bolt.Tx) error {
Expand Down
4 changes: 3 additions & 1 deletion plugins/services/transfer/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import (
"google.golang.org/protobuf/types/known/emptypb"
)

var empty = &ptypes.Empty{}

func init() {
registry.Register(&plugin.Registration{
Type: plugins.GRPCPlugin,
Expand Down Expand Up @@ -130,7 +132,7 @@ func (s *service) Transfer(ctx context.Context, req *transferapi.TransferRequest

for _, t := range s.transferrers {
if err := t.Transfer(ctx, src, dst, transferOpts...); err == nil {
return &ptypes.Empty{}, nil
return empty, nil
} else if !errdefs.IsNotImplemented(err) {
return nil, errdefs.ToGRPC(err)
}
Expand Down

0 comments on commit 4b28e29

Please sign in to comment.