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

fix: make timer operations work with golang 1.23 #667

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ The following emojis are used to highlight certain changes:

### Fixed

- Handle timers appropriately for go 1.23 while still being compatible with previous versions.

### Security

## [v0.22.0]
Expand Down
2 changes: 1 addition & 1 deletion gateway/backend_car_traversal.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func carToLinearBlockGetter(ctx context.Context, reader io.Reader, timeout time.
var ok bool
select {
case blkRead, ok = <-blkCh:
if !t.Stop() {
if cap(t.C) == 1 && !t.Stop() {
<-t.C
}
t.Reset(timeout)
Expand Down
32 changes: 16 additions & 16 deletions mfs/repub.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,10 @@
//
// Note: If a publish fails, we retry repeatedly every TimeoutRetry.
func (rp *Republisher) Run(lastPublished cid.Cid) {
quick := time.NewTimer(0)
if !quick.Stop() {
<-quick.C
}
longer := time.NewTimer(0)
if !longer.Stop() {
<-longer.C
}
quick := time.NewTimer(time.Hour)
quick.Stop()
longer := time.NewTimer(time.Hour)
longer.Stop()

var toPublish cid.Cid
for rp.ctx.Err() == nil {
Expand Down Expand Up @@ -156,15 +152,19 @@
// idiom as these timers may not be running.

quick.Stop()
select {
case <-quick.C:
default:
}

longer.Stop()
select {
case <-longer.C:
default:

// If using go < 1.23, clear timer channel after Stop.
if cap(quick.C) == 1 {
select {
case <-quick.C:

Check warning on line 160 in mfs/repub.go

View check run for this annotation

Codecov / codecov/patch

mfs/repub.go#L160

Added line #L160 was not covered by tests
default:
}

select {
case <-longer.C:

Check warning on line 165 in mfs/repub.go

View check run for this annotation

Codecov / codecov/patch

mfs/repub.go#L165

Added line #L165 was not covered by tests
default:
}
}

// 2. If we have a value to publish, publish it now.
Expand Down
3 changes: 2 additions & 1 deletion provider/reprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,9 @@ func (s *reprovider) run() {
}()
}

// TODO: When go 1.23+ is required, replace this with t.Stop().
func stopAndEmptyTimer(t *time.Timer) {
if !t.Stop() {
if !t.Stop() && cap(t.C) == 1 {
<-t.C
}
}
Expand Down
Loading