Switch to stdlib atomic

No need for `go.uber.org/atomic` anymore since 1.19 adds new atomic
types
This commit is contained in:
0x1a8510f2 2022-09-12 15:33:10 +01:00
parent 1755a2e88d
commit 5253a99bce
No known key found for this signature in database
GPG key ID: 1C692E355D76775D
7 changed files with 16 additions and 17 deletions

View file

@ -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()

View file

@ -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 {

2
go.mod
View file

@ -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

View file

@ -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 {

View file

@ -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")
}

View file

@ -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"))
}

View file

@ -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"
)