Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace go.uber.org/atomic with sync/atomic #910

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions benchmark/internal_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ package benchmark
import (
"fmt"
"os"
"sync/atomic"

"github.com/uber/tchannel-go"
"github.com/uber/tchannel-go/hyperbahn"
"github.com/uber/tchannel-go/raw"
"github.com/uber/tchannel-go/thrift"
gen "github.com/uber/tchannel-go/thrift/gen-go/test"

"go.uber.org/atomic"
"golang.org/x/net/context"
)

Expand Down Expand Up @@ -118,7 +118,7 @@ func (rawHandler) OnError(ctx context.Context, err error) {
}

func (h rawHandler) Handle(ctx context.Context, args *raw.Args) (*raw.Res, error) {
h.calls.Inc()
h.calls.Add(1)
return &raw.Res{
Arg2: args.Arg2,
Arg3: args.Arg3,
Expand All @@ -130,6 +130,6 @@ type handler struct {
}

func (h handler) Echo(ctx thrift.Context, arg1 string) (string, error) {
h.calls.Inc()
h.calls.Add(1)
return arg1, nil
}
5 changes: 2 additions & 3 deletions benchmark/internal_tcp_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ package benchmark
import (
"log"
"net"
"sync/atomic"

"github.com/uber/tchannel-go"

"go.uber.org/atomic"
)

// internalTCPServer represents a TCP server responds to TChannel
Expand Down Expand Up @@ -98,7 +97,7 @@ func (s *internalTCPServer) writeResponses(conn net.Conn, ids chan uint32) {
continue
}

s.rawCalls.Inc()
s.rawCalls.Add(1)
if _, err := frames.writeCallRes(id, conn); err != nil {
log.Printf("writeCallRes failed: %v", err)
return
Expand Down
4 changes: 2 additions & 2 deletions benchmark/real_relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ package benchmark
import (
"errors"
"os"
"sync/atomic"

"github.com/uber/tchannel-go"
"github.com/uber/tchannel-go/relay"
"github.com/uber/tchannel-go/relay/relaytest"
"go.uber.org/atomic"
)

type fixedHosts struct {
Expand All @@ -46,7 +46,7 @@ func (fh *fixedHosts) Get(cf relay.CallFrame, _ *relay.Conn) (string, error) {
cf.Arg2Append(kv.Key, kv.Val)
}

pickI := int(fh.pickI.Inc()-1) % len(peers)
pickI := int(fh.pickI.Add(1)-1) % len(peers)
return peers[pickI], nil
}

Expand Down
5 changes: 2 additions & 3 deletions benchmark/tcp_raw_relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ import (
"io"
"log"
"net"

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

type tcpRelay struct {
Expand Down Expand Up @@ -87,7 +86,7 @@ func (r *tcpRelay) handleIncoming(src net.Conn) {
}

func (r *tcpRelay) nextDestination() string {
i := int(r.destI.Inc()-1) % len(r.dests)
i := int(r.destI.Add(1)-1) % len(r.dests)
return r.dests[i]
}

Expand Down
4 changes: 2 additions & 2 deletions channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ import (
"runtime"
"strings"
"sync"
"sync/atomic"
"time"

"github.com/uber/tchannel-go/tnet"

"github.com/opentracing/opentracing-go"
"go.uber.org/atomic"
"golang.org/x/net/context"
)

Expand Down Expand Up @@ -264,7 +264,7 @@ func NewChannel(serviceName string, opts *ChannelOptions) (*Channel, error) {
timeTicker = time.NewTicker
}

chID := _nextChID.Inc()
chID := _nextChID.Add(1)
logger = logger.WithFields(
LogField{"serviceName", serviceName},
LogField{"process", processName},
Expand Down
6 changes: 3 additions & 3 deletions close_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package tchannel_test
import (
"math/rand"
"sync"
"sync/atomic"
"testing"
"time"

Expand All @@ -33,7 +34,6 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/atomic"
"golang.org/x/net/context"
)

Expand Down Expand Up @@ -601,10 +601,10 @@ func TestCloseSendError(t *testing.T) {
opts := testutils.NewOpts().DisableLogVerification()
serverCh := testutils.NewServer(t, opts)
testutils.RegisterEcho(serverCh, func() {
if counter.Inc() > 10 {
if counter.Add(1) > 10 {
// Close the server in a goroutine to possibly trigger more race conditions.
go func() {
closed.Inc()
closed.Add(1)
serverCh.Close()
}()
}
Expand Down
14 changes: 7 additions & 7 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ import (
"net"
"strings"
"sync"
"sync/atomic"
"syscall"
"time"

"github.com/uber/tchannel-go/tos"

"go.uber.org/atomic"
"golang.org/x/net/context"
"golang.org/x/net/ipv4"
"golang.org/x/net/ipv6"
Expand Down Expand Up @@ -316,7 +316,7 @@ func (ch *Channel) setConnectionTosPriority(tosPriority tos.ToS, c net.Conn) err
func (ch *Channel) newConnection(baseCtx context.Context, conn net.Conn, initialID uint32, outboundHP string, remotePeer PeerInfo, remotePeerAddress peerAddressComponents, events connectionEvents) *Connection {
opts := ch.connectionOptions.withDefaults()

connID := _nextConnID.Inc()
connID := _nextConnID.Add(1)
connDirection := inbound
log := ch.log.WithFields(LogFields{
{"connID", connID},
Expand Down Expand Up @@ -357,10 +357,10 @@ func (ch *Channel) newConnection(baseCtx context.Context, conn net.Conn, initial
events: events,
commonStatsTags: ch.commonStatsTags,
healthCheckHistory: newHealthHistory(),
lastActivityRead: *atomic.NewInt64(timeNow),
lastActivityWrite: *atomic.NewInt64(timeNow),
baseContext: ch.connContext(baseCtx, conn),
}
c.lastActivityRead.Store(timeNow)
c.lastActivityWrite.Store(timeNow)

if tosPriority := opts.TosPriority; tosPriority > 0 {
if err := ch.setConnectionTosPriority(tosPriority, conn); err != nil {
Expand Down Expand Up @@ -523,7 +523,7 @@ func (c *Connection) RemotePeerInfo() PeerInfo {

// NextMessageID reserves the next available message id for this connection
func (c *Connection) NextMessageID() uint32 {
return c.nextMessageID.Inc()
return c.nextMessageID.Add(1)
}

// SendSystemError sends an error frame for the given system error.
Expand Down Expand Up @@ -618,7 +618,7 @@ func (c *Connection) connectionError(site string, err error) error {
c.close(closeLogFields...)

// On any connection error, notify the exchanges of this error.
if c.stoppedExchanges.CAS(false, true) {
if c.stoppedExchanges.CompareAndSwap(false, true) {
c.outbound.stopExchanges(err)
c.inbound.stopExchanges(err)
}
Expand All @@ -639,7 +639,7 @@ func (c *Connection) protocolError(id uint32, err error) error {
)

// On any connection error, notify the exchanges of this error.
if c.stoppedExchanges.CAS(false, true) {
if c.stoppedExchanges.CompareAndSwap(false, true) {
c.outbound.stopExchanges(sysErr)
c.inbound.stopExchanges(sysErr)
}
Expand Down
6 changes: 3 additions & 3 deletions examples/bench/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ import (
"net/http"
_ "net/http/pprof"
"runtime"
"sync/atomic"
"time"

"github.com/uber/tchannel-go"
"github.com/uber/tchannel-go/raw"

"go.uber.org/atomic"
"golang.org/x/net/context"
)

Expand Down Expand Up @@ -83,14 +83,14 @@ func worker(ch *tchannel.Channel) {
log.Fatalf("set failed: %v", err)
continue
}
counter.Inc()
counter.Add(1)

for i := 0; i < *getToSetRatio; i++ {
_, err := getRequest(ch, "key")
if err != nil {
log.Fatalf("get failed: %v", err)
}
counter.Inc()
counter.Add(1)
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions frame_pool_b_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ package tchannel_test
import (
"math/rand"
"sync"
"sync/atomic"
"testing"

. "github.com/uber/tchannel-go"

"go.uber.org/atomic"
)

func benchmarkUsing(b *testing.B, pool FramePool) {
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ require (
github.com/stretchr/testify v1.5.1
github.com/uber-go/tally v3.3.15+incompatible
github.com/uber/jaeger-client-go v2.22.1+incompatible
go.uber.org/atomic v1.6.0
go.uber.org/multierr v1.2.0
golang.org/x/net v0.14.0
golang.org/x/sys v0.11.0
Expand All @@ -29,6 +28,6 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.3.0 // indirect
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
golang.org/x/tools v0.1.12 // indirect
go.uber.org/atomic v1.11.0 // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
)
17 changes: 2 additions & 15 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,14 @@ github.com/uber/jaeger-client-go v2.22.1+incompatible h1:NHcubEkVbahf9t3p75TOCR8
github.com/uber/jaeger-client-go v2.22.1+incompatible/go.mod h1:WVhlPFC8FDjOFMMWRy2pZqQJSXxYSwNYOkTr/Z6d3Kk=
github.com/uber/jaeger-lib v2.4.1+incompatible h1:td4jdvLcExb4cBISKIpHuGoVXh+dVKhn2Um6rjCsSsg=
github.com/uber/jaeger-lib v2.4.1+incompatible/go.mod h1:ComeNDZlWwrWnDv8aPp0Ba6+uUTzImX/AauajbLI56U=
go.uber.org/atomic v1.6.0 h1:Ezj3JGmsOnG1MoRWQkPBsKLe9DwWD9QeXzTRzzldNVk=
go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
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/multierr v1.2.0 h1:6I+W7f5VwC5SV9dNrZ3qXrDB9mD0dyGOi/ZJmYw03T4=
go.uber.org/multierr v1.2.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de h1:5hukYrvBGR8/eNkX5mdUezrA6JiaEZDtJb9Ei+1LlBs=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14=
golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
4 changes: 2 additions & 2 deletions handlers_with_skip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package tchannel_test

import (
"fmt"
"sync/atomic"
"testing"
"time"

Expand All @@ -30,7 +31,6 @@ import (
. "github.com/uber/tchannel-go"
"github.com/uber/tchannel-go/raw"
"github.com/uber/tchannel-go/testutils"
"go.uber.org/atomic"
"golang.org/x/net/context"
)

Expand Down Expand Up @@ -90,5 +90,5 @@ func TestUserHandlerWithSkipInvalidInput(t *testing.T) {
type recordHandler struct{ c atomic.Uint32 }

func (r *recordHandler) Handle(ctx context.Context, call *InboundCall) {
r.c.Inc()
r.c.Add(1)
}
8 changes: 4 additions & 4 deletions mex.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import (
"errors"
"fmt"
"sync"
"sync/atomic"

"github.com/uber/tchannel-go/typed"

"go.uber.org/atomic"
"golang.org/x/net/context"
)

Expand Down Expand Up @@ -255,11 +255,11 @@ func (mex *messageExchange) onCtxErr(err error) {
func (mex *messageExchange) shutdown() {
// The reader and writer side can both hit errors and try to shutdown the mex,
// so we ensure that it's only shut down once.
if !mex.shutdownAtomic.CAS(false, true) {
if !mex.shutdownAtomic.CompareAndSwap(false, true) {
return
}

if mex.errChNotified.CAS(false, true) {
if mex.errChNotified.CompareAndSwap(false, true) {
mex.errCh.Notify(errMexShutdown)
}

Expand Down Expand Up @@ -529,7 +529,7 @@ func (mexset *messageExchangeSet) stopExchanges(err error) {
// on sendChRefs that there's no references to sendCh is violated since
// readers/writers could still have a reference to sendCh even though
// we shutdown the exchange and called Done on sendChRefs.
if mex.errChNotified.CAS(false, true) {
if mex.errChNotified.CompareAndSwap(false, true) {
mex.errCh.Notify(err)
}
}
Expand Down
4 changes: 2 additions & 2 deletions peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ import (
"errors"
"strings"
"sync"
"sync/atomic"
"time"

"github.com/uber/tchannel-go/trand"

"go.uber.org/atomic"
"golang.org/x/net/context"
)

Expand Down Expand Up @@ -210,7 +210,7 @@ func (l *PeerList) choosePeer(prevSelected map[string]struct{}, avoidHost bool)
}

l.peerHeap.pushPeer(ps)
ps.chosenCount.Inc()
ps.chosenCount.Add(1)
return ps.Peer
}

Expand Down
2 changes: 1 addition & 1 deletion peer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"fmt"
"sort"
"sync"
"sync/atomic"
"testing"
"time"

Expand All @@ -35,7 +36,6 @@ import (

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

func fakePeer(t *testing.T, ch *Channel, hostPort string) *Peer {
Expand Down
Loading