From 03a63e48b233c60d0e6c235e5736f22494601502 Mon Sep 17 00:00:00 2001 From: Vasil Averyanau Date: Thu, 9 Jan 2025 13:23:58 +0100 Subject: [PATCH] refactor(cloudmeta): simplifies context cancellation checks --- pkg/cloudmeta/metadata.go | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/pkg/cloudmeta/metadata.go b/pkg/cloudmeta/metadata.go index 06044b116..7a2e7c37b 100644 --- a/pkg/cloudmeta/metadata.go +++ b/pkg/cloudmeta/metadata.go @@ -87,10 +87,8 @@ func (cloud *CloudMeta) GetInstanceMetadata(ctx context.Context) (InstanceMetada go func(provider CloudMetadataProvider) { meta, err := cloud.runWithTimeout(ctx, provider) - select { - case <-ctx.Done(): + if ctx.Err() != nil { return - default: } results <- msg{meta: meta, err: err} @@ -105,10 +103,8 @@ func (cloud *CloudMeta) GetInstanceMetadata(ctx context.Context) (InstanceMetada return InstanceMetadata{}, ctx.Err() case res := <-results: // Additional context check just in case messages in results and in ctx.Done channels were available at the same time. - select { - case <-ctx.Done(): - return InstanceMetadata{}, ctx.Err() - default: + if err := ctx.Err(); err != nil { + return InstanceMetadata{}, err } if res.err != nil { mErr = multierr.Append(mErr, res.err)