Skip to content

Commit

Permalink
chore: use timerpool instead of time.After
Browse files Browse the repository at this point in the history
  • Loading branch information
fufuok committed Dec 2, 2024
1 parent 0fae2cc commit bbcf7db
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
5 changes: 4 additions & 1 deletion config/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/fufuok/utils"
"github.com/fufuok/utils/myip"
"github.com/fufuok/utils/pools/timerpool"
"github.com/imroc/req/v3"

"github.com/fufuok/pkg/json"
Expand Down Expand Up @@ -182,10 +183,12 @@ func GetNodeIPFromAPIs(ipapi string, timeout ...time.Duration) string {
}()
}

timer := timerpool.New(dur)
defer timerpool.Release(timer)
select {
case ip := <-ipChan:
return ip
case <-time.After(dur):
case <-timer.C:
}
return ""
}
Expand Down
5 changes: 4 additions & 1 deletion master/ntpdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/fufuok/ntp"
"github.com/fufuok/utils/pools/timerpool"

"github.com/fufuok/pkg/common"
"github.com/fufuok/pkg/config"
Expand All @@ -21,8 +22,10 @@ var (

// WaitUntilNtpdate 等待, 直到第一次时间同步成功
func WaitUntilNtpdate(timeout time.Duration) bool {
timer := timerpool.New(timeout)
defer timerpool.Release(timer)
select {
case <-time.After(timeout):
case <-timer.C:
return false
case <-ntpFirstDoneChan:
return true
Expand Down

0 comments on commit bbcf7db

Please sign in to comment.