Skip to content

Commit

Permalink
minor refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Oliver Eikemeier <[email protected]>
  • Loading branch information
eikemeier committed Jan 10, 2025
1 parent 2165df5 commit 6263348
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 51 deletions.
27 changes: 16 additions & 11 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,37 @@ name: Test
- main
jobs:
test:
strategy:
matrix:
go: ['1.22', '1.21']
name: Test on Go ${{ matrix.go }}
runs-on: ubuntu-latest
name: Test on Go ${{ matrix.go-version }}
permissions:
checks: write
contents: read
pull-requests: read
statuses: write
runs-on: ubuntu-24.04
strategy:
matrix:
go-version: ["1.24.0-rc.1", "1.23.4", "1.22.10", "1.21.13"]
include:
- go-version: "1.23.4"
update-coverage: true
env:
GOTOOLCHAIN: local
steps:
- name: ✔ Check out
uses: actions/checkout@v4
- name: 🐹 Set up Go ${{ matrix.go }}
- name: 🐹 Set up Go ${{ matrix.go-version }}
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go }}
check-latest: true
go-version: ${{ matrix.go-version }}
- name: 🧸 golangci-lint
uses: golangci/golangci-lint-action@v4
uses: golangci/golangci-lint-action@v6
with:
version: v1.57.2
version: v1.63.4
- name: 🔨 Test
run: go test -race -coverprofile=cover.out ./...
- name: 🧑🏻‍💻 codecov
uses: codecov/codecov-action@v4
uses: codecov/codecov-action@v5
if: ${{ matrix.update-coverage }}
with:
files: ./cover.out
token: ${{ secrets.CODECOV_TOKEN }}
38 changes: 26 additions & 12 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,7 @@ linters:
enable-all: true
disable:
# deprecated
- deadcode
- exhaustivestruct
- golint
- ifshort
- interfacer
- maligned
- nosnakecase
- scopelint
- structcheck
- varcheck
- exportloopref
# disabled
- depguard
- dupl
Expand All @@ -24,17 +15,26 @@ linters:
- varnamelen
- wrapcheck
- wsl
# Go 1.22
- copyloopvar
# Go 1.22+
- intrange
linters-settings:
govet:
enable-all: true
disable:
- fieldalignment
settings:
shadow:
strict: true
testifylint:
enable-all: true
disable:
- require-error
ireturn:
allow:
- anon
- error
- empty
- stdlib
- generic
predeclared:
ignore: "new"
Expand All @@ -44,3 +44,17 @@ issues:
linters:
- revive
text: "dot-imports"
- path: _test\.go$
linters:
- govet
text: "lostcancel"
- path: ^main\.go$
linters:
- gocheckcompilerdirectives
text: "go:debug"
- linters:
- govet
text: '^shadow: declaration of "(ctx|err|ok)" shadows declaration at line \d+$'
- linters:
- revive
text: "^redefines-builtin-id: redefinition of the built-in function new$"
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module fillmore-labs.com/lazydone

go 1.21

toolchain go1.22.2
toolchain go1.23.4
18 changes: 8 additions & 10 deletions lazy.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,20 @@ func (l *Lazy) Close() {

// Done returns the done channel.
func (l *Lazy) Done() <-chan struct{} {
done := l.done.Load()
if done == nil {
if ch := make(chan struct{}); l.done.CompareAndSwap(nil, ch) {
done = ch
} else {
done = l.done.Load()
}
if done := l.done.Load(); done != nil {
return done
}

if done := make(chan struct{}); l.done.CompareAndSwap(nil, done) {
return done
}

return done
return l.done.Load()
}

// Closed returns true if the done channel is closed.
func (l *Lazy) Closed() bool {
done := l.done.Load()
switch done {
switch done := l.done.Load(); done {
case nil:
return false

Expand Down
35 changes: 18 additions & 17 deletions pointer.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,36 +29,37 @@ type SafeLazy struct {

// Close closes the done channel. You shouldn't close the channel twice.
func (l *SafeLazy) Close() {
if ch := l.done.Swap(&closedChan); ch != nil && ch != &closedChan {
close(*ch)
if done := l.done.Swap(&closedChan); done != nil && *done != closedChan {
close(*done)
}
}

// Done returns the done channel.
func (l *SafeLazy) Done() <-chan struct{} {
done := l.done.Load()
if done == nil {
if ch := make(chan struct{}); l.done.CompareAndSwap(nil, &ch) {
done = &ch
} else {
done = l.done.Load()
}
if done := l.done.Load(); done != nil {
return *done
}

if ch := make(chan struct{}); l.done.CompareAndSwap(nil, &ch) {
return ch
}

return *done
return *l.done.Load()
}

// Closed returns true if the done channel is closed.
func (l *SafeLazy) Closed() bool {
if done := l.done.Load(); done != nil {
select {
case <-*done:
return true
default:
}
done := l.done.Load()
if done == nil {
return false
}

return false
select {
case <-*done:
return true
default:
return false
}
}

func (l *SafeLazy) String() string {
Expand Down

0 comments on commit 6263348

Please sign in to comment.