Skip to content

Commit

Permalink
core: Move canceled.Is to core.IsCanceled (#7831)
Browse files Browse the repository at this point in the history
Small refactor to use errors.Is() rather than the equality operator.
Also moves this utility function into core.
  • Loading branch information
beautifulentropy authored Nov 20, 2024
1 parent a8cdaf8 commit c263258
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 48 deletions.
16 changes: 0 additions & 16 deletions canceled/canceled.go

This file was deleted.

22 changes: 0 additions & 22 deletions canceled/canceled_test.go

This file was deleted.

14 changes: 13 additions & 1 deletion core/util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package core

import (
"context"
"crypto"
"crypto/ecdsa"
"crypto/rand"
Expand All @@ -27,9 +28,12 @@ import (
"unicode"

"github.com/go-jose/go-jose/v4"
"github.com/letsencrypt/boulder/identifier"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/letsencrypt/boulder/identifier"
)

const Unspecified = "Unspecified"
Expand Down Expand Up @@ -395,6 +399,14 @@ func IsASCII(str string) bool {
return true
}

// IsCanceled returns true if err is non-nil and is either context.Canceled, or
// has a grpc code of Canceled. This is useful because cancellations propagate
// through gRPC boundaries, and if we choose to treat in-process cancellations a
// certain way, we usually want to treat cross-process cancellations the same way.
func IsCanceled(err error) bool {
return errors.Is(err, context.Canceled) || status.Code(err) == codes.Canceled
}

func Command() string {
return path.Base(os.Args[0])
}
16 changes: 16 additions & 0 deletions core/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package core

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"math"
"math/big"
Expand All @@ -13,6 +15,8 @@ import (
"time"

"github.com/go-jose/go-jose/v4"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/timestamppb"

Expand Down Expand Up @@ -362,3 +366,15 @@ func TestHashNames(t *testing.T) {
h2 = HashNames([]string{"a"})
test.AssertByteEquals(t, h1, h2)
}

func TestIsCanceled(t *testing.T) {
if !IsCanceled(context.Canceled) {
t.Errorf("Expected context.Canceled to be canceled, but wasn't.")
}
if !IsCanceled(status.Errorf(codes.Canceled, "hi")) {
t.Errorf("Expected gRPC cancellation to be canceled, but wasn't.")
}
if IsCanceled(errors.New("hi")) {
t.Errorf("Expected random error to not be canceled, but was.")
}
}
5 changes: 2 additions & 3 deletions publisher/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
cttls "github.com/google/certificate-transparency-go/tls"
"github.com/prometheus/client_golang/prometheus"

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

sct, err := pub.singleLogSubmit(ctx, chain, req.Kind, ctLog)
if err != nil {
if canceled.Is(err) {
if core.IsCanceled(err) {
return nil, err
}
var body string
Expand Down Expand Up @@ -297,7 +296,7 @@ func (pub *Impl) singleLogSubmit(
took := time.Since(start).Seconds()
if err != nil {
status := "error"
if canceled.Is(err) {
if core.IsCanceled(err) {
status = "canceled"
}
httpStatus := ""
Expand Down
7 changes: 3 additions & 4 deletions va/caa.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/miekg/dns"

"github.com/letsencrypt/boulder/bdns"
"github.com/letsencrypt/boulder/canceled"
"github.com/letsencrypt/boulder/core"
corepb "github.com/letsencrypt/boulder/core/proto"
berrors "github.com/letsencrypt/boulder/errors"
Expand Down Expand Up @@ -234,7 +233,7 @@ func (va *ValidationAuthorityImpl) processRemoteCAAResults(
// the number of remote VAs. The CAA checks will be performed in separate
// go-routines. If the result `error` from a remote `isCAAValid` RPC is nil or a
// nil `ProblemDetails` instance it is written directly to the `results` chan.
// If the err is a cancelled error it is treated as a nil error. Otherwise the
// If the err is a canceled error it is treated as a nil error. Otherwise the
// error/problem is written to the results channel as-is.
func (va *ValidationAuthorityImpl) performRemoteCAACheck(
ctx context.Context,
Expand All @@ -248,9 +247,9 @@ func (va *ValidationAuthorityImpl) performRemoteCAACheck(
}
res, err := rva.IsCAAValid(ctx, req)
if err != nil {
if canceled.Is(err) {
if core.IsCanceled(err) {
// Handle the cancellation error.
result.Problem = probs.ServerInternal("Remote VA IsCAAValid RPC cancelled")
result.Problem = probs.ServerInternal("Remote VA IsCAAValid RPC canceled")
} else {
// Handle validation error.
va.log.Errf("Remote VA %q.IsCAAValid failed: %s", rva.Address, err)
Expand Down
3 changes: 1 addition & 2 deletions va/va.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/prometheus/client_golang/prometheus"

"github.com/letsencrypt/boulder/bdns"
"github.com/letsencrypt/boulder/canceled"
"github.com/letsencrypt/boulder/core"
berrors "github.com/letsencrypt/boulder/errors"
bgrpc "github.com/letsencrypt/boulder/grpc"
Expand Down Expand Up @@ -485,7 +484,7 @@ func (va *ValidationAuthorityImpl) performRemoteValidation(
// Failed to communicate with the remote VA.
failed = append(failed, resp.addr)

if canceled.Is(resp.err) {
if core.IsCanceled(resp.err) {
currProb = probs.ServerInternal("Remote PerformValidation RPC canceled")
} else {
va.log.Errf("Remote VA %q.PerformValidation failed: %s", resp.addr, resp.err)
Expand Down

0 comments on commit c263258

Please sign in to comment.