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

chore: use sync/atomic types #3723

Merged
merged 4 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 das/daser.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type DASer struct {

cancel context.CancelFunc
subscriberDone chan struct{}
running int32
cristaloleg marked this conversation as resolved.
Show resolved Hide resolved
running atomic.Int32
}

type (
Expand Down Expand Up @@ -85,7 +85,7 @@ func NewDASer(

// Start initiates subscription for new ExtendedHeaders and spawns a sampling routine.
func (d *DASer) Start(ctx context.Context) error {
if !atomic.CompareAndSwapInt32(&d.running, 0, 1) {
if !d.running.CompareAndSwap(0, 1) {
return errors.New("da: DASer already started")
}

Expand Down Expand Up @@ -124,7 +124,7 @@ func (d *DASer) Start(ctx context.Context) error {

// Stop stops sampling.
func (d *DASer) Stop(ctx context.Context) error {
if !atomic.CompareAndSwapInt32(&d.running, 1, 0) {
if !d.running.CompareAndSwap(1, 0) {
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion das/daser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func TestDASer_stopsAfter_BEFP(t *testing.T) {
require.NoError(t, res)
}
// wait for manager to finish catchup
require.True(t, daser.running == 0)
require.True(t, daser.running.Load() == 0)
}

func TestDASerSampleTimeout(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions das/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type metrics struct {
getHeaderTime metric.Float64Histogram
newHead metric.Int64Counter

lastSampledTS uint64
lastSampledTS atomic.Uint64

clientReg metric.Registration
}
Expand Down Expand Up @@ -113,7 +113,7 @@ func (d *DASer) InitMetrics() error {
observer.ObserveInt64(networkHead, int64(stats.NetworkHead))
observer.ObserveInt64(sampledChainHead, int64(stats.SampledChainHead))

if ts := atomic.LoadUint64(&d.sampler.metrics.lastSampledTS); ts != 0 {
if ts := d.sampler.metrics.lastSampledTS.Load(); ts != 0 {
observer.ObserveInt64(lastSampledTS, int64(ts))
}

Expand Down Expand Up @@ -171,7 +171,7 @@ func (m *metrics) observeSample(
attribute.String(jobTypeLabel, string(jobType)),
))

atomic.StoreUint64(&m.lastSampledTS, uint64(time.Now().UTC().Unix()))
m.lastSampledTS.Store(uint64(time.Now().UTC().Unix()))
}

// observeGetHeader records the time it took to get a header from the header store.
Expand Down
4 changes: 2 additions & 2 deletions share/eds/retriever.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ type retrievalSession struct {
// https://github.com/celestiaorg/rsmt2d/issues/135
squareQuadrants []*quadrant
squareCellsLks [][]sync.Mutex
squareCellsCount uint32
squareCellsCount atomic.Uint32
squareSig chan struct{}
squareDn chan struct{}
squareLk sync.RWMutex
Expand Down Expand Up @@ -319,7 +319,7 @@ func (rs *retrievalSession) doRequest(ctx context.Context, q *quadrant) {
// but it is totally fine for the happy case and for now.
// The earlier we correctly know that we have the full square - the earlier
// we cancel ongoing requests - the less data is being wastedly transferred.
if atomic.AddUint32(&rs.squareCellsCount, 1) >= uint32(size*size) {
if rs.squareCellsCount.Add(1) >= uint32(size*size) {
select {
case rs.squareSig <- struct{}{}:
default:
Expand Down
6 changes: 3 additions & 3 deletions share/ipld/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,15 @@ func GetProof(
// when fully done.
type chanGroup struct {
jobs chan job
counter int64
counter atomic.Int64
}

func (w *chanGroup) add(count int64) {
atomic.AddInt64(&w.counter, count)
w.counter.Add(count)
}

func (w *chanGroup) done() {
numRemaining := atomic.AddInt64(&w.counter, -1)
numRemaining := w.counter.Add(-1)

// Close channel if this job was the last one
if numRemaining == 0 {
Expand Down
Loading