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

[Bug]: Correct usage of Ticker and Timer #22222

Closed
1 task done
czs007 opened this issue Feb 16, 2023 · 3 comments
Closed
1 task done

[Bug]: Correct usage of Ticker and Timer #22222

czs007 opened this issue Feb 16, 2023 · 3 comments
Assignees
Labels
kind/bug Issues or changes related a bug kind/improvement Changes related to something improve, likes ut and code refactor stale indicates no udpates for 30 days triage/accepted Indicates an issue or PR is ready to be actively worked on.
Milestone

Comments

@czs007
Copy link
Collaborator

czs007 commented Feb 16, 2023

Is there an existing issue for this?

  • I have searched the existing issues

Environment

- Milvus version: master/2.x
- Deployment mode(standalone or cluster):
- MQ type(rocksmq, pulsar or kafka):    
- SDK version(e.g. pymilvus v2.0.0rc2):
- OS(Ubuntu or CentOS): 
- CPU/Memory: 
- GPU: 
- Others:

Current Behavior

Timer Vesus Ticker

Timers — These are used for one-off tasks. It represents a single event in the future.
You tell the timer how long you want to wait, and it provides a channel that will be notified at that time. You can call Stop prevents the Timer from firing.
Stop returns true if the call stops the timer, false if the timer has already expired or been stopped.

Tickers — Tickers are exceptionally helpful when you need to perform an action repeatedly at given time intervals.
We can use tickers, in combination with goroutines to run these tasks in the background of our applications.

For repeated- tasks, use Ticker

Although Timer can also help to achieve this (Reset, for example), there are many details that need to be paid attention to, especially in the case of Stop (You can refer to this discussion golang/go#27169 ).

When using Ticker, remember to stop it to release associated resources.

For one-off task, use Timer or time.After

As for time.After, it waits for the duration to elapse and then sends the current time on the returned channel. It is equivalent to NewTimer(d).C.
The underlying Timer is not recovered by the garbage collector until the timer fires. If efficiency is a concern, use NewTimer instead and call Timer.Stop if the timer is no longer needed.

Avoid using time.After when you have a For loop

    	for {
		select {
		 ...
		case <-time.After(time.Minute * 3):
		}
	    }

In this use case, it may cause a large number of Timer objects to be created, although these large objects will be GCed later.

Do not use time.Tick

Tick is a convenience wrapper for NewTicker providing access to the ticking channel only.
While Tick is useful for clients that have no need to shut down the Ticker, be aware that without a way to shut it down the underlying Ticker cannot be recovered by the garbage collector; it "leaks".

In milvus, there is no such usage scenario, so the use of time.Tick is prohibited

Expected Behavior

No response

Steps To Reproduce

No response

Milvus Log

No response

Anything else?

No response

@czs007 czs007 added kind/bug Issues or changes related a bug kind/improvement Changes related to something improve, likes ut and code refactor needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Feb 16, 2023
@yanliang567
Copy link
Contributor

/assign @czs007
/unassign

@sre-ci-robot sre-ci-robot assigned czs007 and unassigned yanliang567 Feb 16, 2023
@yanliang567 yanliang567 added triage/accepted Indicates an issue or PR is ready to be actively worked on. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Feb 16, 2023
@yanliang567 yanliang567 added this to the 2.3 milestone Feb 16, 2023
@congqixia
Copy link
Contributor

Good point! We need a linter for these cases

@stale
Copy link

stale bot commented Mar 18, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Rotten issues close after 30d of inactivity. Reopen the issue with /reopen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Issues or changes related a bug kind/improvement Changes related to something improve, likes ut and code refactor stale indicates no udpates for 30 days triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

No branches or pull requests

4 participants