From 5253a99bce7d7580d3d5a0a4b21a1952ca7e6648 Mon Sep 17 00:00:00 2001 From: 0x1a8510f2 Date: Mon, 12 Sep 2022 15:33:10 +0100 Subject: [PATCH] Switch to stdlib atomic No need for `go.uber.org/atomic` anymore since 1.19 adds new atomic types --- federationapi/queue/destinationqueue.go | 6 +++--- federationapi/statistics/statistics.go | 8 ++++---- go.mod | 2 +- internal/sqlutil/writer_exclusive.go | 5 ++--- setup/base/base.go | 6 +++--- setup/process/process.go | 4 ++-- syncapi/streams/stream_pdu.go | 2 +- 7 files changed, 16 insertions(+), 17 deletions(-) diff --git a/federationapi/queue/destinationqueue.go b/federationapi/queue/destinationqueue.go index 0d937ffaf..3b7c68c22 100644 --- a/federationapi/queue/destinationqueue.go +++ b/federationapi/queue/destinationqueue.go @@ -19,6 +19,7 @@ import ( "encoding/json" "fmt" "sync" + "sync/atomic" "time" fedapi "github.com/matrix-org/dendrite/federationapi/api" @@ -30,7 +31,6 @@ import ( "github.com/matrix-org/gomatrix" "github.com/matrix-org/gomatrixserverlib" "github.com/sirupsen/logrus" - "go.uber.org/atomic" ) const ( @@ -162,7 +162,7 @@ func (oq *destinationQueue) sendEDU(event *gomatrixserverlib.EDU, receipt *share // requests to retry. func (oq *destinationQueue) wakeQueueIfNeeded() { // If we are backing off then interrupt the backoff. - if oq.backingOff.CAS(true, false) { + if oq.backingOff.CompareAndSwap(true, false) { oq.interruptBackoff <- true } // If we aren't running then wake up the queue. @@ -242,7 +242,7 @@ func (oq *destinationQueue) getPendingFromDatabase() { func (oq *destinationQueue) backgroundSend() { // Check if a worker is already running, and if it isn't, then // mark it as started. - if !oq.running.CAS(false, true) { + if !oq.running.CompareAndSwap(false, true) { return } destinationQueueRunning.Inc() diff --git a/federationapi/statistics/statistics.go b/federationapi/statistics/statistics.go index b8e16a259..39463505d 100644 --- a/federationapi/statistics/statistics.go +++ b/federationapi/statistics/statistics.go @@ -3,11 +3,11 @@ package statistics import ( "math" "sync" + "sync/atomic" "time" "github.com/matrix-org/gomatrixserverlib" "github.com/sirupsen/logrus" - "go.uber.org/atomic" "github.com/matrix-org/dendrite/federationapi/storage" ) @@ -95,7 +95,7 @@ func (s *ServerStatistics) cancel() { // we will unblacklist it. func (s *ServerStatistics) Success() { s.cancel() - s.successCounter.Inc() + s.successCounter.Add(1) s.backoffCount.Store(0) if s.statistics.DB != nil { if err := s.statistics.DB.RemoveServerFromBlacklist(s.serverName); err != nil { @@ -114,8 +114,8 @@ func (s *ServerStatistics) Failure() (time.Time, bool) { // a new backoff period. Increase the failure counter and // start a goroutine which will wait out the backoff and // unset the backoffStarted flag when done. - if s.backoffStarted.CAS(false, true) { - if s.backoffCount.Inc() >= s.statistics.FailuresUntilBlacklist { + if s.backoffStarted.CompareAndSwap(false, true) { + if s.backoffCount.Add(1) >= s.statistics.FailuresUntilBlacklist { s.blacklisted.Store(true) if s.statistics.DB != nil { if err := s.statistics.DB.AddServerToBlacklist(s.serverName); err != nil { diff --git a/go.mod b/go.mod index 3eddc7e80..9b5abc11b 100644 --- a/go.mod +++ b/go.mod @@ -42,7 +42,6 @@ require ( github.com/uber/jaeger-client-go v2.30.0+incompatible github.com/uber/jaeger-lib v2.4.1+incompatible github.com/yggdrasil-network/yggdrasil-go v0.4.5-0.20220901155642-4f2abece817c - go.uber.org/atomic v1.9.0 golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 golang.org/x/image v0.0.0-20220413100746-70e8d0d3baa9 golang.org/x/mobile v0.0.0-20220722155234-aaac322e2105 @@ -123,6 +122,7 @@ require ( github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.0 // indirect go.etcd.io/bbolt v1.3.5 // indirect + go.uber.org/atomic v1.9.0 // indirect golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect golang.org/x/sys v0.0.0-20220906135438-9e1f76180b77 // indirect golang.org/x/text v0.3.8-0.20211004125949-5bd84dd9b33b // indirect diff --git a/internal/sqlutil/writer_exclusive.go b/internal/sqlutil/writer_exclusive.go index 91dd77e4d..d12c26c62 100644 --- a/internal/sqlutil/writer_exclusive.go +++ b/internal/sqlutil/writer_exclusive.go @@ -3,8 +3,7 @@ package sqlutil import ( "database/sql" "errors" - - "go.uber.org/atomic" + "sync/atomic" ) // ExclusiveWriter implements sqlutil.Writer. @@ -57,7 +56,7 @@ func (w *ExclusiveWriter) Do(db *sql.DB, txn *sql.Tx, f func(txn *sql.Tx) error) // opened using the database object from the task and then this will // be passed as a parameter to the task function. func (w *ExclusiveWriter) run() { - if !w.running.CAS(false, true) { + if !w.running.CompareAndSwap(false, true) { return } if tracingEnabled { diff --git a/setup/base/base.go b/setup/base/base.go index 3070bed5f..801c8da49 100644 --- a/setup/base/base.go +++ b/setup/base/base.go @@ -26,6 +26,7 @@ import ( "os" "os/signal" "sync" + "sync/atomic" "syscall" "time" @@ -33,7 +34,6 @@ import ( sentryhttp "github.com/getsentry/sentry-go/http" "github.com/matrix-org/gomatrixserverlib" "github.com/prometheus/client_golang/prometheus/promhttp" - "go.uber.org/atomic" "golang.org/x/net/http2" "golang.org/x/net/http2/h2c" @@ -498,7 +498,7 @@ func (b *BaseDendrite) SetupAndServeHTTP( logrus.Infof("Starting internal %s listener on %s", b.componentName, internalServ.Addr) b.ProcessContext.ComponentStarted() internalServ.RegisterOnShutdown(func() { - if internalShutdown.CAS(false, true) { + if internalShutdown.CompareAndSwap(false, true) { b.ProcessContext.ComponentFinished() logrus.Infof("Stopped internal HTTP listener") } @@ -526,7 +526,7 @@ func (b *BaseDendrite) SetupAndServeHTTP( logrus.Infof("Starting external %s listener on %s", b.componentName, externalServ.Addr) b.ProcessContext.ComponentStarted() externalServ.RegisterOnShutdown(func() { - if externalShutdown.CAS(false, true) { + if externalShutdown.CompareAndSwap(false, true) { b.ProcessContext.ComponentFinished() logrus.Infof("Stopped external HTTP listener") } diff --git a/setup/process/process.go b/setup/process/process.go index 01eb26e22..0e666504c 100644 --- a/setup/process/process.go +++ b/setup/process/process.go @@ -4,10 +4,10 @@ import ( "context" "fmt" "sync" + "sync/atomic" "github.com/getsentry/sentry-go" "github.com/sirupsen/logrus" - "go.uber.org/atomic" ) type ProcessContext struct { @@ -51,7 +51,7 @@ func (b *ProcessContext) WaitForComponentsToFinish() { } func (b *ProcessContext) Degraded() { - if b.degraded.CAS(false, true) { + if b.degraded.CompareAndSwap(false, true) { logrus.Warn("Dendrite is running in a degraded state") sentry.CaptureException(fmt.Errorf("Process is running in a degraded state")) } diff --git a/syncapi/streams/stream_pdu.go b/syncapi/streams/stream_pdu.go index ffcf64df6..8656747f4 100644 --- a/syncapi/streams/stream_pdu.go +++ b/syncapi/streams/stream_pdu.go @@ -6,6 +6,7 @@ import ( "fmt" "sort" "sync" + "sync/atomic" "time" "github.com/matrix-org/dendrite/internal/caching" @@ -18,7 +19,6 @@ import ( "github.com/matrix-org/gomatrixserverlib" "github.com/sirupsen/logrus" "github.com/tidwall/gjson" - "go.uber.org/atomic" "github.com/matrix-org/dendrite/syncapi/notifier" )