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

Why is the time wheel timer call blocking? #3789

Closed
me-cs opened this issue Dec 13, 2023 · 2 comments
Closed

Why is the time wheel timer call blocking? #3789

me-cs opened this issue Dec 13, 2023 · 2 comments

Comments

@me-cs
Copy link
Contributor

me-cs commented Dec 13, 2023

func TestTw(t *testing.T) {
	tw, err := collection.NewTimingWheel(time.Millisecond*100, 10, func(key, v any) {
		time.Sleep(time.Second)
		fmt.Println(time.Now().Format(time.DateTime), "hello key", key, v)
	})
	if err != nil {
		fmt.Print(err)
	}
	for i := 0; i < 10; i++ {
		tw.SetTimer(i, i, time.Second)
	}
	time.Sleep(time.Second * 10)
}

Its output looks like this,Is that how it's designed?

2023-12-13 17:20:22 hello key 0 0
2023-12-13 17:20:23 hello key 1 1
2023-12-13 17:20:24 hello key 2 2
2023-12-13 17:20:25 hello key 3 3
2023-12-13 17:20:26 hello key 4 4
2023-12-13 17:20:27 hello key 5 5
2023-12-13 17:20:28 hello key 6 6
2023-12-13 17:20:29 hello key 7 7
--- PASS: TestTw (10.01s)
PASS

I'm using gozero's own time wheel for timed tasks, but when I set a number of timed tasks to the same moment, they seem to execute serially rather than in parallel? I looked at the underlying implementation
image
image
It seems like the task is executed using a go func? Why is this still happening? Am I misunderstanding? I hope someone can solve my problem.

I'll use the go func again myself

func TestTw(t *testing.T) {
	tw, err := collection.NewTimingWheel(time.Millisecond*100, 10, func(key, v any) {
		go func() {
			time.Sleep(time.Second)
			fmt.Println(time.Now().Format(time.DateTime), "hello key", key, v)
		}()

	})
	if err != nil {
		fmt.Print(err)
	}
	for i := 0; i < 10; i++ {
		tw.SetTimer(i, i, time.Second)
	}
	time.Sleep(time.Second * 10)
}

It came out the way I wanted it to.

=== RUN   TestTw
2023-12-13 17:28:40 hello key 3 3
2023-12-13 17:28:40 hello key 6 6
2023-12-13 17:28:40 hello key 9 9
2023-12-13 17:28:40 hello key 1 1
2023-12-13 17:28:40 hello key 8 8
2023-12-13 17:28:40 hello key 2 2
2023-12-13 17:28:40 hello key 4 4
2023-12-13 17:28:40 hello key 5 5
2023-12-13 17:28:40 hello key 7 7
2023-12-13 17:28:40 hello key 0 0
--- PASS: TestTw (10.00s)
PASS

@chenquan
Copy link
Member

threading.RunSafe is executed serially.

@me-cs
Copy link
Contributor Author

me-cs commented Dec 25, 2023

Thanks, I see, I misread it and thought it was all executing threading.GoSafe

@me-cs me-cs closed this as completed Dec 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants