Skip to content

Commit c263258

Browse files
core: Move canceled.Is to core.IsCanceled (#7831)
Small refactor to use errors.Is() rather than the equality operator. Also moves this utility function into core.
1 parent a8cdaf8 commit c263258

File tree

7 files changed

+35
-48
lines changed

7 files changed

+35
-48
lines changed

canceled/canceled.go

Lines changed: 0 additions & 16 deletions
This file was deleted.

canceled/canceled_test.go

Lines changed: 0 additions & 22 deletions
This file was deleted.

core/util.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package core
22

33
import (
4+
"context"
45
"crypto"
56
"crypto/ecdsa"
67
"crypto/rand"
@@ -27,9 +28,12 @@ import (
2728
"unicode"
2829

2930
"github.com/go-jose/go-jose/v4"
30-
"github.com/letsencrypt/boulder/identifier"
31+
"google.golang.org/grpc/codes"
32+
"google.golang.org/grpc/status"
3133
"google.golang.org/protobuf/types/known/durationpb"
3234
"google.golang.org/protobuf/types/known/timestamppb"
35+
36+
"github.com/letsencrypt/boulder/identifier"
3337
)
3438

3539
const Unspecified = "Unspecified"
@@ -395,6 +399,14 @@ func IsASCII(str string) bool {
395399
return true
396400
}
397401

402+
// IsCanceled returns true if err is non-nil and is either context.Canceled, or
403+
// has a grpc code of Canceled. This is useful because cancellations propagate
404+
// through gRPC boundaries, and if we choose to treat in-process cancellations a
405+
// certain way, we usually want to treat cross-process cancellations the same way.
406+
func IsCanceled(err error) bool {
407+
return errors.Is(err, context.Canceled) || status.Code(err) == codes.Canceled
408+
}
409+
398410
func Command() string {
399411
return path.Base(os.Args[0])
400412
}

core/util_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package core
22

33
import (
44
"bytes"
5+
"context"
56
"encoding/json"
7+
"errors"
68
"fmt"
79
"math"
810
"math/big"
@@ -13,6 +15,8 @@ import (
1315
"time"
1416

1517
"github.com/go-jose/go-jose/v4"
18+
"google.golang.org/grpc/codes"
19+
"google.golang.org/grpc/status"
1620
"google.golang.org/protobuf/types/known/durationpb"
1721
"google.golang.org/protobuf/types/known/timestamppb"
1822

@@ -362,3 +366,15 @@ func TestHashNames(t *testing.T) {
362366
h2 = HashNames([]string{"a"})
363367
test.AssertByteEquals(t, h1, h2)
364368
}
369+
370+
func TestIsCanceled(t *testing.T) {
371+
if !IsCanceled(context.Canceled) {
372+
t.Errorf("Expected context.Canceled to be canceled, but wasn't.")
373+
}
374+
if !IsCanceled(status.Errorf(codes.Canceled, "hi")) {
375+
t.Errorf("Expected gRPC cancellation to be canceled, but wasn't.")
376+
}
377+
if IsCanceled(errors.New("hi")) {
378+
t.Errorf("Expected random error to not be canceled, but was.")
379+
}
380+
}

publisher/publisher.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
cttls "github.com/google/certificate-transparency-go/tls"
2626
"github.com/prometheus/client_golang/prometheus"
2727

28-
"github.com/letsencrypt/boulder/canceled"
2928
"github.com/letsencrypt/boulder/core"
3029
"github.com/letsencrypt/boulder/issuance"
3130
blog "github.com/letsencrypt/boulder/log"
@@ -261,7 +260,7 @@ func (pub *Impl) SubmitToSingleCTWithResult(ctx context.Context, req *pubpb.Requ
261260

262261
sct, err := pub.singleLogSubmit(ctx, chain, req.Kind, ctLog)
263262
if err != nil {
264-
if canceled.Is(err) {
263+
if core.IsCanceled(err) {
265264
return nil, err
266265
}
267266
var body string
@@ -297,7 +296,7 @@ func (pub *Impl) singleLogSubmit(
297296
took := time.Since(start).Seconds()
298297
if err != nil {
299298
status := "error"
300-
if canceled.Is(err) {
299+
if core.IsCanceled(err) {
301300
status = "canceled"
302301
}
303302
httpStatus := ""

va/caa.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/miekg/dns"
1414

1515
"github.com/letsencrypt/boulder/bdns"
16-
"github.com/letsencrypt/boulder/canceled"
1716
"github.com/letsencrypt/boulder/core"
1817
corepb "github.com/letsencrypt/boulder/core/proto"
1918
berrors "github.com/letsencrypt/boulder/errors"
@@ -234,7 +233,7 @@ func (va *ValidationAuthorityImpl) processRemoteCAAResults(
234233
// the number of remote VAs. The CAA checks will be performed in separate
235234
// go-routines. If the result `error` from a remote `isCAAValid` RPC is nil or a
236235
// nil `ProblemDetails` instance it is written directly to the `results` chan.
237-
// If the err is a cancelled error it is treated as a nil error. Otherwise the
236+
// If the err is a canceled error it is treated as a nil error. Otherwise the
238237
// error/problem is written to the results channel as-is.
239238
func (va *ValidationAuthorityImpl) performRemoteCAACheck(
240239
ctx context.Context,
@@ -248,9 +247,9 @@ func (va *ValidationAuthorityImpl) performRemoteCAACheck(
248247
}
249248
res, err := rva.IsCAAValid(ctx, req)
250249
if err != nil {
251-
if canceled.Is(err) {
250+
if core.IsCanceled(err) {
252251
// Handle the cancellation error.
253-
result.Problem = probs.ServerInternal("Remote VA IsCAAValid RPC cancelled")
252+
result.Problem = probs.ServerInternal("Remote VA IsCAAValid RPC canceled")
254253
} else {
255254
// Handle validation error.
256255
va.log.Errf("Remote VA %q.IsCAAValid failed: %s", rva.Address, err)

va/va.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"github.com/prometheus/client_golang/prometheus"
2121

2222
"github.com/letsencrypt/boulder/bdns"
23-
"github.com/letsencrypt/boulder/canceled"
2423
"github.com/letsencrypt/boulder/core"
2524
berrors "github.com/letsencrypt/boulder/errors"
2625
bgrpc "github.com/letsencrypt/boulder/grpc"
@@ -485,7 +484,7 @@ func (va *ValidationAuthorityImpl) performRemoteValidation(
485484
// Failed to communicate with the remote VA.
486485
failed = append(failed, resp.addr)
487486

488-
if canceled.Is(resp.err) {
487+
if core.IsCanceled(resp.err) {
489488
currProb = probs.ServerInternal("Remote PerformValidation RPC canceled")
490489
} else {
491490
va.log.Errf("Remote VA %q.PerformValidation failed: %s", resp.addr, resp.err)

0 commit comments

Comments
 (0)