Skip to content

Commit

Permalink
Drop go.uber.org/atomic in favor of sync/atomic (#3443)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanvanburen authored Nov 1, 2024
1 parent 5c44d3e commit c26d758
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 16 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ require (
github.com/tetratelabs/wazero v1.8.1
go.lsp.dev/jsonrpc2 v0.10.0
go.lsp.dev/protocol v0.12.0
go.uber.org/atomic v1.11.0
go.uber.org/zap v1.27.0
go.uber.org/zap/exp v0.3.0
golang.org/x/crypto v0.28.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,6 @@ go.opentelemetry.io/otel/trace v1.31.0 h1:ffjsj1aRouKewfr85U2aGagJ46+MvodynlQ1HY
go.opentelemetry.io/otel/trace v1.31.0/go.mod h1:TXZkRk7SM2ZQLtR6eoAWQFIHPvzQ06FJAsO1tJg480A=
go.opentelemetry.io/proto/otlp v1.0.0 h1:T0TX0tmXU8a3CbNXzEKGeU5mIVOdf0oykP+u2lIVU/I=
go.opentelemetry.io/proto/otlp v1.0.0/go.mod h1:Sy6pihPLfYHkr3NkUbEhGHFhINUSI/v80hjKIs5JXpM=
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.uber.org/mock v0.5.0 h1:KAMbZvZPyBPWgD14IrIQ38QCyjwpvVVV6K/bHl1IwQU=
Expand Down
2 changes: 1 addition & 1 deletion private/buf/bufcurl/verbose_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import (
"runtime"
"sort"
"strings"
"sync/atomic"

"connectrpc.com/connect"
"github.com/bufbuild/buf/private/pkg/verbose"
"go.uber.org/atomic"
)

type skipUploadFinishedMessageKey struct{}
Expand Down
9 changes: 4 additions & 5 deletions private/pkg/storage/limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ package storage

import (
"context"

"go.uber.org/atomic"
"sync/atomic"
)

// LimitWriteBucket returns a [WriteBucket] that writes to [writeBucket]
Expand All @@ -42,7 +41,7 @@ type limitedWriteBucket struct {
func newLimitedWriteBucket(bucket WriteBucket, limit int64) *limitedWriteBucket {
return &limitedWriteBucket{
WriteBucket: bucket,
currentSize: atomic.NewInt64(0),
currentSize: &atomic.Int64{},
limit: limit,
}
}
Expand Down Expand Up @@ -78,15 +77,15 @@ func (o *limitedWriteObjectCloser) Write(p []byte) (int, error) {
writeSize := int64(len(p))
newBucketSize := o.bucketSize.Add(writeSize)
if newBucketSize > o.limit {
o.bucketSize.Sub(writeSize)
o.bucketSize.Add(-writeSize)
return 0, &errWriteLimitReached{
Limit: o.limit,
ExceedingBy: newBucketSize - o.limit,
}
}
writtenSize, err := o.WriteObjectCloser.Write(p)
if int64(writtenSize) < writeSize {
o.bucketSize.Sub(writeSize - int64(writtenSize))
o.bucketSize.Add(-(writeSize - int64(writtenSize)))
}
return writtenSize, err
}
30 changes: 27 additions & 3 deletions private/pkg/storage/storageos/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ import (
"io/fs"
"os"
"path/filepath"
"sync/atomic"

"github.com/bufbuild/buf/private/pkg/filepathext"
"github.com/bufbuild/buf/private/pkg/normalpath"
"github.com/bufbuild/buf/private/pkg/storage"
"github.com/bufbuild/buf/private/pkg/storage/storageutil"
"go.uber.org/atomic"
"github.com/bufbuild/buf/private/pkg/syserror"
)

// errNotDir is the error returned if a path is not a directory.
Expand Down Expand Up @@ -359,7 +360,7 @@ type writeObjectCloser struct {
path string
// writeErr contains the first non-nil error caught by a call to Write.
// This is returned in Close for atomic writes to prevent writing an incomplete file.
writeErr atomic.Error
writeErr onceError
}

func newWriteObjectCloser(
Expand All @@ -375,7 +376,7 @@ func newWriteObjectCloser(
func (w *writeObjectCloser) Write(p []byte) (int, error) {
n, err := w.file.Write(p)
if err != nil {
w.writeErr.CompareAndSwap(nil, err)
w.writeErr.Store(err)
}
return n, toStorageError(err)
}
Expand Down Expand Up @@ -404,6 +405,29 @@ func (w *writeObjectCloser) Close() error {
return err
}

// onceError is an object that will only store an error once.
type onceError struct {
err atomic.Value
}

// Store stores the err.
func (e *onceError) Store(err error) {
e.err.CompareAndSwap(nil, err)
}

// Load loads the stored error.
func (e *onceError) Load() error {
atomicValue := e.err.Load()
if atomicValue == nil {
return nil
}
err, ok := atomicValue.(error)
if !ok {
return syserror.Newf("expected error but got %T", atomicValue)
}
return err
}

// newErrNotDir returns a new Error for a path not being a directory.
func newErrNotDir(path string) *normalpath.Error {
return normalpath.NewError(path, errNotDir)
Expand Down
2 changes: 1 addition & 1 deletion private/pkg/storage/storagetesting/storagetesting.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"sort"
"strconv"
"sync"
"sync/atomic"
"testing"

"github.com/bufbuild/buf/private/pkg/slicesext"
Expand All @@ -38,7 +39,6 @@ import (
"github.com/bufbuild/buf/private/pkg/tmp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/atomic"
)

const (
Expand Down
6 changes: 3 additions & 3 deletions private/pkg/thread/thread_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ package thread

import (
"context"
"sync/atomic"
"testing"

"github.com/stretchr/testify/assert"
"go.uber.org/atomic"
)

func TestParallelizeWithImmediateCancellation(t *testing.T) {
Expand All @@ -35,7 +35,7 @@ func TestParallelizeWithImmediateCancellation(t *testing.T) {
)
for i := 0; i < jobsToExecute; i++ {
jobs = append(jobs, func(_ context.Context) error {
executed.Inc()
executed.Add(1)
return nil
})
}
Expand All @@ -49,7 +49,7 @@ func TestParallelizeWithImmediateCancellation(t *testing.T) {
var jobs []func(context.Context) error
for i := 0; i < 10; i++ {
jobs = append(jobs, func(_ context.Context) error {
executed.Inc()
executed.Add(1)
return nil
})
}
Expand Down

0 comments on commit c26d758

Please sign in to comment.