Introduction
gtimer
is a concurrent safe and high-performance timer, similar to Java
's Timer
. gtimer
uses a Priority Queue to implement its core functionality.
Use Cases:
Any scenario involving scheduled tasks, scenarios with a large number of scheduled/delayed tasks, business scenarios that require timeout control/frequency control, or scenarios where the accuracy of the scheduled time is not strictly critical.
How to Use:
import "github.com/gogf/gf/v2/os/gtimer"
API Documentation:
https://pkg.go.dev/github.com/gogf/gf/v2/os/gtimer
Brief Explanation:
- The
New
method is used to create a custom task timer object and can acceptTimerOptions
parameters upon creation, including:Interval
to specify the minimumtick
time interval for the timer.Quick
to specify whether the timer should execute once upon start (default isfalse
).
- The
Add
method is used to add scheduled tasks, where:- The
interval
parameter specifies the execution time interval of the method. - The
job
parameter is the task method that needs to be executed.
- The
- The
AddEntry
method adds scheduled tasks, supporting more parameter controls. - The
AddSingleton
method is used to add singleton scheduled tasks, meaning only one task can be running at the same time. - The
AddOnce
method is used to add a task that runs only once and is automatically destroyed after running once. - The
AddTimes
method is used to add a task that runs a specified number of times and is automatically destroyed after runningtimes
times. - The
Search
method is used to search for scheduled tasks by name (returns the*Entry
object pointer of the task). - The
Start
method is used to start the timer (the timer is automatically started when created usingNew
). - The
Stop
method is used to stop the timer. - The
Close
method is used to close the timer.
Default Timer
In most scenarios, the default timer can be used. When using the default timer of gtimer
, the default detection interval is 100ms
, so the theoretical time interval error range is 0~100ms
. You can modify the parameters of the default timer using the following two methods:
- Use startup parameters
gf.gtimer.interval=50
: Change the default time scale to50ms
- Use environment variables
GF_GTIMER_INTERVAL=50
It should be noted that the shorter the default detection interval of the timer, the greater the CPU usage.
Precautions🔥
- Since modern computers use software-implemented timers, all timers have inaccuracies. They will not be completely precise and may delay or even execute ahead of time, but they will not fail to execute. This is particularly noticeable in systems with large time intervals or high concurrency/high load. See the reference link: https://github.com/golang/go/issues/14410
- Since inaccuracies are inevitable, any timer implementation (not just framework timers, but also standard library timers) will not use system time. Instead, it uses a fixed
tick
interval. Do not use system time to judge intervals in the logic of timer tasks, as this judgment is meaningless. - Assuming no inaccuracies, the time interval does not consider the execution time of the task. For example, if a job takes
3
minutes to complete and is scheduled to run every5
minutes, there will be only2
minutes of idle time between each task. - It should be noted that the execution time of singleton mode scheduled tasks will affect the start time of the task's next execution. For example, if a task runs every
1
second and takes1
second to execute, then after starting at second 1, the next task will start executing at second 3 since a running check in between found the current task still running and thus exited waiting for the next execution check.
Difference Between Timer and gcron
Please refer to the section Cron Job
Documents
📄️ Timer - Usage
Using the timer component in the GoFrame framework, including basic usage, singleton tasks, delayed tasks, and scheduled operations through SetTimeout and SetInterval methods. Detailed explanation of the implementation and execution results of these scheduled tasks, and demonstration of how to use the Exit method to exit timed tasks.
📄️ Timer - Performance
Detailed information on timer performance testing using the GoFrame framework in a Linux environment. By comparing Benchmark_Add and Benchmark_StartStop, we can more clearly understand the efficiency and resource allocation of the timer under different operations. The test results demonstrate the efficient performance metrics of the Go language when executing timer operations, providing better references for developers using GoFrame.