Skip to content

Commit

Permalink
refactor: replace custom atomic types with stdlib atomic
Browse files Browse the repository at this point in the history
Go 1.19 added new atomic types. Replace custom atomic implementation with native types.
  • Loading branch information
kruskall committed Jun 19, 2024
1 parent 4ccdd09 commit cb83cc1
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ package handlers

import (
"context"
"sync/atomic"
"testing"

"github.com/elastic/elastic-agent-libs/atomic"

"github.com/elastic/elastic-agent-client/v7/pkg/client"
"github.com/elastic/elastic-agent-client/v7/pkg/proto"
"github.com/elastic/elastic-agent/internal/pkg/agent/application/coordinator"
Expand Down Expand Up @@ -43,15 +42,15 @@ func makeComponentState(name string, proxiedActions []string) runtime.ComponentC

type MockActionCoordinator struct {
st coordinator.State
performedActions atomic.Int
performedActions atomic.Int64
}

func (c *MockActionCoordinator) State() coordinator.State {
return c.st
}

func (c *MockActionCoordinator) PerformAction(ctx context.Context, comp component.Component, unit component.Unit, name string, params map[string]interface{}) (map[string]interface{}, error) {
c.performedActions.Inc()
c.performedActions.Add(1)
return nil, nil
}

Expand Down Expand Up @@ -118,7 +117,7 @@ func TestActionUnenrollHandler(t *testing.T) {
name string
st coordinator.State
wantErr error // Handler error
wantPerformedActions int
wantPerformedActions int64
tamperProtectionFn func() bool
}{
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ package http

import (
"context"
"sync/atomic"
"time"

"github.com/elastic/elastic-agent-libs/atomic"
)

type downloadProgressReporter struct {
Expand All @@ -17,7 +16,7 @@ type downloadProgressReporter struct {
warnTimeout time.Duration
length float64

downloaded atomic.Int
downloaded atomic.Int64
started time.Time

progressObservers []progressObserver
Expand All @@ -42,7 +41,7 @@ func newDownloadProgressReporter(sourceURI string, timeout time.Duration, length

func (dp *downloadProgressReporter) Write(b []byte) (int, error) {
n := len(b)
dp.downloaded.Add(n)
dp.downloaded.Add(int64(n))
return n, nil
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/component/runtime/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"path"
"strings"
"sync"
"sync/atomic"
"time"

"github.com/gofrs/uuid"
Expand All @@ -28,7 +29,6 @@ import (

"github.com/elastic/elastic-agent-client/v7/pkg/client"
"github.com/elastic/elastic-agent-client/v7/pkg/proto"
"github.com/elastic/elastic-agent-libs/atomic"

"github.com/elastic/elastic-agent/internal/pkg/agent/application/info"
"github.com/elastic/elastic-agent/internal/pkg/agent/configuration"
Expand Down Expand Up @@ -193,7 +193,7 @@ func NewManager(
errCh: make(chan error),
monitor: monitor,
grpcConfig: grpcConfig,
serverReady: atomic.NewBool(false),
serverReady: &atomic.Bool{},
doneChan: make(chan struct{}),
}
return m, nil
Expand Down
5 changes: 2 additions & 3 deletions pkg/component/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"context"
"errors"
"sync"
"sync/atomic"

"github.com/elastic/elastic-agent-client/v7/pkg/client"
"github.com/elastic/elastic-agent-client/v7/pkg/proto"
"github.com/elastic/elastic-agent-libs/atomic"
"github.com/elastic/elastic-agent/internal/pkg/runner"
"github.com/elastic/elastic-agent/pkg/component"
"github.com/elastic/elastic-agent/pkg/core/logger"
Expand Down Expand Up @@ -188,11 +188,10 @@ func (s *componentRuntimeState) start() error {
}

func (s *componentRuntimeState) stop(teardown bool, signed *component.Signed) error {
if s.shuttingDown.Load() {
if !s.shuttingDown.CompareAndSwap(false, true) {
// already stopping
return nil
}
s.shuttingDown.Store(true)
if teardown {
return s.runtime.Teardown(signed)
}
Expand Down

0 comments on commit cb83cc1

Please sign in to comment.