Skip to content

Commit

Permalink
fix: use atomic package for ids
Browse files Browse the repository at this point in the history
closes #622

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 committed Oct 7, 2024
1 parent 4382fdf commit 6f9a708
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 48 deletions.
13 changes: 3 additions & 10 deletions filepicker/filepicker.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,18 @@ import (
"sort"
"strconv"
"strings"
"sync"
"sync/atomic"

"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/dustin/go-humanize"
)

var (
lastID int
idMtx sync.Mutex
)
var lastID uint64

// Return the next ID we should use on the Model.
func nextID() int {
idMtx.Lock()
defer idMtx.Unlock()
lastID++
return lastID
return int(atomic.AddUint64(&lastID, 1))

Check failure on line 21 in filepicker/filepicker.go

View workflow job for this annotation

GitHub Actions / lint

G115: integer overflow conversion uint64 -> int (gosec)
}

// New returns a new filepicker model with default styling and key bindings.
Expand Down
13 changes: 3 additions & 10 deletions progress/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"math"
"strings"
"sync"
"sync/atomic"
"time"

tea "github.com/charmbracelet/bubbletea"
Expand All @@ -17,17 +17,10 @@ import (

// Internal ID management. Used during animating to assure that frame messages
// can only be received by progress components that sent them.
var (
lastID int
idMtx sync.Mutex
)
var lastID uint64

// Return the next ID we should use on the model.
func nextID() int {
idMtx.Lock()
defer idMtx.Unlock()
lastID++
return lastID
return int(atomic.AddUint64(&lastID, 1))

Check failure on line 23 in progress/progress.go

View workflow job for this annotation

GitHub Actions / lint

G115: integer overflow conversion uint64 -> int (gosec)
}

const (
Expand Down
13 changes: 3 additions & 10 deletions spinner/spinner.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package spinner

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

tea "github.com/charmbracelet/bubbletea"
Expand All @@ -10,17 +10,10 @@ import (

// Internal ID management. Used during animating to ensure that frame messages
// are received only by spinner components that sent them.
var (
lastID int
idMtx sync.Mutex
)
var lastID uint64

// Return the next ID we should use on the Model.
func nextID() int {
idMtx.Lock()
defer idMtx.Unlock()
lastID++
return lastID
return int(atomic.AddUint64(&lastID, 1))

Check failure on line 16 in spinner/spinner.go

View workflow job for this annotation

GitHub Actions / lint

G115: integer overflow conversion uint64 -> int (gosec)
}

// Spinner is a set of frames used in animating the spinner.
Expand Down
12 changes: 3 additions & 9 deletions stopwatch/stopwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,16 @@
package stopwatch

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

tea "github.com/charmbracelet/bubbletea"
)

var (
lastID int
idMtx sync.Mutex
)
var lastID uint64

func nextID() int {
idMtx.Lock()
defer idMtx.Unlock()
lastID++
return lastID
return int(atomic.AddUint64(&lastID, 1))

Check failure on line 14 in stopwatch/stopwatch.go

View workflow job for this annotation

GitHub Actions / lint

G115: integer overflow conversion uint64 -> int (gosec)
}

// TickMsg is a message that is sent on every timer tick.
Expand Down
12 changes: 3 additions & 9 deletions timer/timer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,16 @@
package timer

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

tea "github.com/charmbracelet/bubbletea"
)

var (
lastID int
idMtx sync.Mutex
)
var lastID uint64

func nextID() int {
idMtx.Lock()
defer idMtx.Unlock()
lastID++
return lastID
return int(atomic.AddUint64(&lastID, 1))

Check failure on line 14 in timer/timer.go

View workflow job for this annotation

GitHub Actions / lint

G115: integer overflow conversion uint64 -> int (gosec)
}

// Authors note with regard to start and stop commands:
Expand Down

0 comments on commit 6f9a708

Please sign in to comment.