all: Use new Go 1.19 atomic types (#8772)
This commit is contained in:
@@ -25,7 +25,7 @@ type limiter struct {
|
||||
mu sync.Mutex
|
||||
write *rate.Limiter
|
||||
read *rate.Limiter
|
||||
limitsLAN atomicBool
|
||||
limitsLAN atomic.Bool
|
||||
deviceReadLimiters map[protocol.DeviceID]*rate.Limiter
|
||||
deviceWriteLimiters map[protocol.DeviceID]*rate.Limiter
|
||||
}
|
||||
@@ -157,7 +157,7 @@ func (lim *limiter) CommitConfiguration(from, to config.Configuration) bool {
|
||||
limited = true
|
||||
}
|
||||
|
||||
lim.limitsLAN.set(to.Options.LimitBandwidthInLan)
|
||||
lim.limitsLAN.Store(to.Options.LimitBandwidthInLan)
|
||||
|
||||
l.Infof("Overall send rate %s, receive rate %s", sendLimitStr, recvLimitStr)
|
||||
|
||||
@@ -282,13 +282,13 @@ func (w *limitedWriter) Write(buf []byte) (int, error) {
|
||||
// waiter, valid for both writers and readers
|
||||
type waiterHolder struct {
|
||||
waiter waiter
|
||||
limitsLAN *atomicBool
|
||||
limitsLAN *atomic.Bool
|
||||
isLAN bool
|
||||
}
|
||||
|
||||
// unlimited returns true if the waiter is not limiting the rate
|
||||
func (w waiterHolder) unlimited() bool {
|
||||
if w.isLAN && !w.limitsLAN.get() {
|
||||
if w.isLAN && !w.limitsLAN.Load() {
|
||||
return true
|
||||
}
|
||||
return w.waiter.Limit() == rate.Inf
|
||||
@@ -322,20 +322,6 @@ func (w waiterHolder) take(tokens int) {
|
||||
}
|
||||
}
|
||||
|
||||
type atomicBool int32
|
||||
|
||||
func (b *atomicBool) set(v bool) {
|
||||
if v {
|
||||
atomic.StoreInt32((*int32)(b), 1)
|
||||
} else {
|
||||
atomic.StoreInt32((*int32)(b), 0)
|
||||
}
|
||||
}
|
||||
|
||||
func (b *atomicBool) get() bool {
|
||||
return atomic.LoadInt32((*int32)(b)) != 0
|
||||
}
|
||||
|
||||
// totalWaiter waits for all of the waiters
|
||||
type totalWaiter []waiter
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
crand "crypto/rand"
|
||||
"io"
|
||||
"math/rand"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"github.com/syncthing/syncthing/lib/config"
|
||||
@@ -234,7 +235,7 @@ func TestLimitedWriterWrite(t *testing.T) {
|
||||
writer: cw,
|
||||
waiterHolder: waiterHolder{
|
||||
waiter: rate.NewLimiter(rate.Limit(42), limiterBurstSize),
|
||||
limitsLAN: new(atomicBool),
|
||||
limitsLAN: new(atomic.Bool),
|
||||
isLAN: false, // enables limiting
|
||||
},
|
||||
}
|
||||
@@ -263,7 +264,7 @@ func TestLimitedWriterWrite(t *testing.T) {
|
||||
writer: cw,
|
||||
waiterHolder: waiterHolder{
|
||||
waiter: rate.NewLimiter(rate.Limit(42), limiterBurstSize),
|
||||
limitsLAN: new(atomicBool),
|
||||
limitsLAN: new(atomic.Bool),
|
||||
isLAN: true, // disables limiting
|
||||
},
|
||||
}
|
||||
@@ -287,7 +288,7 @@ func TestLimitedWriterWrite(t *testing.T) {
|
||||
writer: cw,
|
||||
waiterHolder: waiterHolder{
|
||||
waiter: totalWaiter{rate.NewLimiter(rate.Inf, limiterBurstSize), rate.NewLimiter(rate.Inf, limiterBurstSize)},
|
||||
limitsLAN: new(atomicBool),
|
||||
limitsLAN: new(atomic.Bool),
|
||||
isLAN: false, // enables limiting
|
||||
},
|
||||
}
|
||||
@@ -315,7 +316,7 @@ func TestLimitedWriterWrite(t *testing.T) {
|
||||
rate.NewLimiter(rate.Limit(42), limiterBurstSize),
|
||||
rate.NewLimiter(rate.Inf, limiterBurstSize),
|
||||
},
|
||||
limitsLAN: new(atomicBool),
|
||||
limitsLAN: new(atomic.Bool),
|
||||
isLAN: false, // enables limiting
|
||||
},
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ func init() {
|
||||
|
||||
type quicListener struct {
|
||||
svcutil.ServiceWithError
|
||||
nat atomic.Value
|
||||
nat atomic.Uint64 // Holds a stun.NATType.
|
||||
|
||||
onAddressesChangedNotifier
|
||||
|
||||
@@ -56,7 +56,7 @@ func (t *quicListener) OnNATTypeChanged(natType stun.NATType) {
|
||||
if natType != stun.NATUnknown {
|
||||
l.Infof("%s detected NAT type: %s", t.uri, natType)
|
||||
}
|
||||
t.nat.Store(natType)
|
||||
t.nat.Store(uint64(natType))
|
||||
}
|
||||
|
||||
func (t *quicListener) OnExternalAddressChanged(address *stun.Host, via string) {
|
||||
@@ -205,7 +205,7 @@ func (t *quicListener) Factory() listenerFactory {
|
||||
}
|
||||
|
||||
func (t *quicListener) NATType() string {
|
||||
v := t.nat.Load().(stun.NATType)
|
||||
v := stun.NATType(t.nat.Load())
|
||||
if v == stun.NATUnknown || v == stun.NATError {
|
||||
return "unknown"
|
||||
}
|
||||
@@ -228,7 +228,7 @@ func (f *quicListenerFactory) New(uri *url.URL, cfg config.Wrapper, tlsCfg *tls.
|
||||
registry: registry,
|
||||
}
|
||||
l.ServiceWithError = svcutil.AsService(l.serve, l.String())
|
||||
l.nat.Store(stun.NATUnknown)
|
||||
l.nat.Store(uint64(stun.NATUnknown))
|
||||
return l
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user