Skip to content

Commit

Permalink
Replace go.uber.org/atomic with sync/atomic
Browse files Browse the repository at this point in the history
go 1.19 introduced atomic types
  • Loading branch information
serprex committed Jan 10, 2024
1 parent 5ca9a4d commit 0961d80
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 24 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ require (
github.com/robfig/cron v1.2.0
github.com/stretchr/testify v1.8.4
go.temporal.io/api v1.26.1-0.20240103185939-608bdd111e4b
go.uber.org/atomic v1.9.0
golang.org/x/sys v0.15.0
golang.org/x/time v0.3.0
google.golang.org/grpc v1.60.1
Expand All @@ -25,6 +24,7 @@ require (
github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
go.uber.org/atomic v1.4.0 // indirect
golang.org/x/exp v0.0.0-20231127185646-65229373498e
golang.org/x/net v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
Expand Down
3 changes: 1 addition & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,8 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
go.temporal.io/api v1.26.1-0.20240103185939-608bdd111e4b h1:Fi5NWG08z7pfxBolgjchVp4PnmWrGIHjqboDlXve5Sg=
go.temporal.io/api v1.26.1-0.20240103185939-608bdd111e4b/go.mod h1:mix7Bpl8mFEfYud66rjYInxNpP43sqXvr1UiHv+mur0=
go.uber.org/atomic v1.4.0 h1:cxzIVoETapQEqDhQu3QfnvXAV4AlzcvUCxkVUFw3+EU=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down
9 changes: 4 additions & 5 deletions internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import (
enumspb "go.temporal.io/api/enums/v1"
"go.temporal.io/api/operatorservice/v1"
"go.temporal.io/api/workflowservice/v1"
uberatomic "go.uber.org/atomic"
"google.golang.org/grpc"

"go.temporal.io/sdk/converter"
Expand Down Expand Up @@ -532,7 +531,7 @@ type (
// other gRPC errors. If not present during service client creation, it will
// be created as false. This is set to true when server capabilities are
// fetched.
excludeInternalFromRetry *uberatomic.Bool
excludeInternalFromRetry *atomic.Bool
}

// StartWorkflowOptions configuration parameters for starting a workflow execution.
Expand Down Expand Up @@ -746,7 +745,7 @@ func newClient(options ClientOptions, existing *WorkflowClient) (Client, error)
var connection *grpc.ClientConn
var err error
if existing == nil {
options.ConnectionOptions.excludeInternalFromRetry = uberatomic.NewBool(false)
options.ConnectionOptions.excludeInternalFromRetry = &atomic.Bool{}
connection, err = dial(newDialParameters(&options, options.ConnectionOptions.excludeInternalFromRetry))
if err != nil {
return nil, err
Expand Down Expand Up @@ -780,7 +779,7 @@ func newClient(options ClientOptions, existing *WorkflowClient) (Client, error)
return client, nil
}

func newDialParameters(options *ClientOptions, excludeInternalFromRetry *uberatomic.Bool) dialParameters {
func newDialParameters(options *ClientOptions, excludeInternalFromRetry *atomic.Bool) dialParameters {
return dialParameters{
UserConnectionOptions: options.ConnectionOptions,
HostPort: options.HostPort,
Expand Down Expand Up @@ -818,7 +817,7 @@ func NewServiceClient(workflowServiceClient workflowservice.WorkflowServiceClien
}

if options.ConnectionOptions.excludeInternalFromRetry == nil {
options.ConnectionOptions.excludeInternalFromRetry = uberatomic.NewBool(false)
options.ConnectionOptions.excludeInternalFromRetry = &atomic.Bool{}
}

// Collect set of applicable worker interceptors
Expand Down
6 changes: 3 additions & 3 deletions internal/common/retry/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ package retry
import (
"context"
"math"
"sync/atomic"
"time"

"google.golang.org/grpc/status"
grpc_retry "github.com/grpc-ecosystem/go-grpc-middleware/retry"
"github.com/grpc-ecosystem/go-grpc-middleware/util/backoffutils"
uberatomic "go.uber.org/atomic"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

const (
Expand Down Expand Up @@ -127,7 +127,7 @@ var (
// NewRetryOptionsInterceptor creates a new gRPC interceptor that populates retry options for each call based on values
// provided in the context. The atomic bool is checked each call to determine whether internals are included in retry.
// If not present or false, internals are assumed to be included.
func NewRetryOptionsInterceptor(excludeInternal *uberatomic.Bool) grpc.UnaryClientInterceptor {
func NewRetryOptionsInterceptor(excludeInternal *atomic.Bool) grpc.UnaryClientInterceptor {
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
if rc, ok := ctx.Value(ConfigKey).(*GrpcRetryConfig); ok {
if _, ok := ctx.Deadline(); !ok {
Expand Down
4 changes: 2 additions & 2 deletions internal/grpc_dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ package internal

import (
"context"
"sync/atomic"
"time"

grpc_retry "github.com/grpc-ecosystem/go-grpc-middleware/retry"
"go.temporal.io/api/serviceerror"
"go.temporal.io/sdk/internal/common/metrics"
"go.temporal.io/sdk/internal/common/retry"
uberatomic "go.uber.org/atomic"
"google.golang.org/grpc"
"google.golang.org/grpc/backoff"
"google.golang.org/grpc/credentials"
Expand Down Expand Up @@ -148,7 +148,7 @@ func requiredInterceptors(
metricsHandler metrics.Handler,
headersProvider HeadersProvider,
controller TrafficController,
excludeInternalFromRetry *uberatomic.Bool,
excludeInternalFromRetry *atomic.Bool,
) []grpc.UnaryClientInterceptor {
interceptors := []grpc.UnaryClientInterceptor{
errorInterceptor,
Expand Down
2 changes: 1 addition & 1 deletion internal/internal_workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ import (
"runtime"
"strings"
"sync"
"sync/atomic"
"time"
"unicode"

"golang.org/x/exp/slices"

commonpb "go.temporal.io/api/common/v1"
enumspb "go.temporal.io/api/enums/v1"
"go.uber.org/atomic"

"go.temporal.io/sdk/converter"
"go.temporal.io/sdk/internal/common/metrics"
Expand Down
3 changes: 1 addition & 2 deletions internal/internal_workflow_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
"time"

"github.com/pborman/uuid"
uberatomic "go.uber.org/atomic"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
Expand Down Expand Up @@ -91,7 +90,7 @@ type (
contextPropagators []ContextPropagator
workerInterceptors []WorkerInterceptor
interceptor ClientOutboundInterceptor
excludeInternalFromRetry *uberatomic.Bool
excludeInternalFromRetry *atomic.Bool
capabilities *workflowservice.GetSystemInfoResponse_Capabilities
capabilitiesLock sync.RWMutex
eagerDispatcher *eagerWorkflowDispatcher
Expand Down
3 changes: 1 addition & 2 deletions internal/internal_workflow_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (

updatepb "go.temporal.io/api/update/v1"
workflowpb "go.temporal.io/api/workflow/v1"
uberatomic "go.uber.org/atomic"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/connectivity"
Expand Down Expand Up @@ -174,7 +173,7 @@ func (s *historyEventIteratorSuite) SetupTest() {
s.wfClient = &WorkflowClient{
workflowService: s.workflowServiceClient,
namespace: DefaultNamespace,
excludeInternalFromRetry: uberatomic.NewBool(false),
excludeInternalFromRetry: &atomic.Bool{},
}
}

Expand Down
11 changes: 5 additions & 6 deletions internal/internal_workflow_testsuite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import (
commonpb "go.temporal.io/api/common/v1"
enumspb "go.temporal.io/api/enums/v1"
"go.temporal.io/api/serviceerror"
uberatomic "go.uber.org/atomic"
"google.golang.org/protobuf/proto"

"go.temporal.io/sdk/converter"
Expand Down Expand Up @@ -2331,8 +2330,8 @@ func (s *WorkflowTestSuiteUnitTest) Test_LocalActivity() {
}

func (s *WorkflowTestSuiteUnitTest) Test_WorkflowLocalActivityWithMockAndListeners() {
var localActivityFnCanceled uberatomic.Bool
var startedCount, completedCount, canceledCount uberatomic.Int32
var localActivityFnCanceled atomic.Bool
var startedCount, completedCount, canceledCount atomic.Int32
env := s.NewTestWorkflowEnvironment()

localActivityFn := func(_ context.Context, _ string) (string, error) {
Expand Down Expand Up @@ -2376,7 +2375,7 @@ func (s *WorkflowTestSuiteUnitTest) Test_WorkflowLocalActivityWithMockAndListene
env.RegisterWorkflow(workflowFn)
env.OnActivity(localActivityFn, mock.Anything, "local_activity").Return("hello mock", nil).Once()
env.SetOnLocalActivityStartedListener(func(activityInfo *ActivityInfo, ctx context.Context, args []interface{}) {
startedCount.Inc()
startedCount.Add(1)
})

env.SetOnLocalActivityCompletedListener(func(activityInfo *ActivityInfo, result converter.EncodedValue, err error) {
Expand All @@ -2385,11 +2384,11 @@ func (s *WorkflowTestSuiteUnitTest) Test_WorkflowLocalActivityWithMockAndListene
err = result.Get(&resultValue)
s.NoError(err)
s.Equal("hello mock", resultValue)
completedCount.Inc()
completedCount.Add(1)
})

env.SetOnLocalActivityCanceledListener(func(activityInfo *ActivityInfo) {
canceledCount.Inc()
canceledCount.Add(1)
})

env.ExecuteWorkflow(workflowFn)
Expand Down

0 comments on commit 0961d80

Please sign in to comment.