You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
after 1ms this func will be hit.
f := func() {
fmt.Println("hit timer run")
w.Timeout()
w.timer = nil
}
and if I modify the code,
if w.timer == nil {
fmt.Println("Timer Start", w.timeout)
//w.timer = time.AfterFunc(w.timeout, f)
time.AfterFunc(w.timeout, f) // it works why ?
}
What did you see instead?
Nothing happened after 4 seconds
The text was updated successfully, but these errors were encountered:
jubaobleach
changed the title
affected/package: time
affected/package: time time.AfterFunc not working when the code is in a struct.
Apr 26, 2023
What version of Go are you using (
go version
)?I try both go version go1.17.1 darwin/amd64 and go version go1.20.3 darwin/amd64
Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Here is the code
package main
import (
"fmt"
"time"
)
type Packet struct {
seqNum uint
data []byte
}
type Window struct {
windowSize uint
base uint
nextSeqNum uint
packets map[uint]*Packet
timer *time.Timer
timeout time.Duration
}
func NewWindow(windowSize uint, timeout time.Duration) *Window {
return &Window{
windowSize: windowSize,
base: 0,
nextSeqNum: 0,
packets: make(map[uint]*Packet),
timer: nil,
timeout: timeout,
}
}
func (w *Window) SendPacket(seqNum uint, data []byte) {
if int(seqNum-w.base) < 0 || int(w.base+w.windowSize-1-seqNum) < 0 {
return
}
}
func (w *Window) Timeout() {
fmt.Println("timout hit")
for i := uint(0); i < w.windowSize; i++ {
seqNum := w.base + i
if seqNum > w.nextSeqNum {
break
}
if packet, ok := w.packets[seqNum]; ok && packet != nil {
fmt.Printf("Packet %d timed out", seqNum)
delete(w.packets, seqNum)
}
}
w.base = (w.base + 1) % 0x10000
w.nextSeqNum = (w.base + 1) % 0x10000
}
func (w *Window) ReceivePacket(seqNum uint) (int, []byte) {
if int(seqNum-w.base) < 0 || int(w.base+w.windowSize-1-seqNum) < 0 {
return 1, nil
}
}
func main() {
}
What did you expect to see?
after 1ms this func will be hit.
f := func() {
fmt.Println("hit timer run")
w.Timeout()
w.timer = nil
}
and if I modify the code,
if w.timer == nil {
fmt.Println("Timer Start", w.timeout)
//w.timer = time.AfterFunc(w.timeout, f)
time.AfterFunc(w.timeout, f) // it works why ?
}
What did you see instead?
Nothing happened after 4 seconds
The text was updated successfully, but these errors were encountered: