All skills
gogf avatar

/goframe-v2

@911705d
by GoFramegogf/skills81 stars
3

GoFrame v2 development skill. Use only when the target Go project uses or is explicitly adopting GoFrame v2: the nearest go.mod requires github.com/gogf/gf/v2, existing Go files import github.com/gogf/gf/v2 or any github.com/gogf/gf/v2/... component package, or the user asks to scaffold, migrate, or build with GoFrame. Trigger for GoFrame-backed Go work such as APIs/controllers/services, middleware, routing/config, ORM/DAO/DO/entity/database operations, gf CLI/codegen, HTTP/gRPC services, and microservice conventions. Do not trigger for generic Go projects without GoFrame evidence, frontend-only work, shell scripts, or unrelated infrastructure tasks.

Use this Skill: https://skilld.dev/gh/gogf/skills/goframe-v2

This session only. Nothing lands on disk.

references组件列表系统相关定时器-gtimer定时器-gtimer.md

基本介绍

gtimer 是一个并发安全的高性能定时器,类似于 Java 的 Timer。 gtimer 底层采用了 优先级队列( PriorityQueue)实现。

使用场景:

任何定时任务场景,大批量定时任务/延迟任务的场景,超时控制/频率控制的业务场景,对于定时时间准确度要求不高的业务场景。

使用方式:

import "github.com/gogf/gf/v2/os/gtimer"

接口文档:

https://pkg.go.dev/github.com/gogf/gf/v2/os/gtimer

简要说明:

  1. New 方法用于创建自定义的任务定时器对象,并可在创建时TimerOptions传入参数,其中:
    • Interval 指定定时器的最小 tick 时间间隔。
    • Quick 指定定时器是否启动时就执行一次(默认为 false)。
  2. Add 方法用于添加定时任务,其中:
    • interval 参数用于指定方法的执行的时间间隔。
    • job 参数为需要执行的任务方法。
  3. AddEntry 方法添加定时任务,支持更多参数的控制。
  4. AddSingleton 方法用于添加 单例 定时任务,即 同时只能有一个该任务正在运行。
  5. AddOnce 方法用于添加只运行一次的定时任务,当运行一次数后该定时任务自动销毁。
  6. AddTimes 方法用于添加运行指定次数的定时任务,当运行 times 次数后该定时任务自动销毁。
  7. Search 方法用于根据名称进行定时任务搜索(返回定时任务 *Entry 对象指针)。
  8. Start 方法用于启动定时器(使用 New 创建定时器时会自动启动)。
  9. Stop 方法用于停止定时器。
  10. Close 方法用于关闭定时器。

默认定时器

大部分的场景下使用默认的定时器即可。使用 gtimer 的默认定时器时,默认的定时检测间隔时间为 100ms,因此理论的时间间隔误差范围为 0~100ms。可以使用以下两种方式修改默认的定时器参数:

  1. 使用启动参数
    • gf.gtimer.interval=50: 修改默认的时间刻度为 50毫秒
  2. 使用环境变量
    • GF_GTIMER_INTERVAL=50 :::warning 需要注意,定时器默认检测间隔时间越短,对 CPU 的使用量会越大。 :::

注意事项🔥

  1. 由于现代的计算机都是使用软件实现的定时器, 任何的定时器都是存在误差的,不会完全精准,可能延迟,也甚至会提前,但是不会不执行。并且在定时间隔比较大,或者并发量大,负载较高的系统中尤其明显。参考链接: https://github.com/golang/go/issues/14410
  2. 由于误差是不可避免的,任何定时器的实现(不仅是框架的定时器,标准库的定时器亦是如此) 都不会使用系统时间,而是使用一种固定的 tick 间隔。不要在定时器任务逻辑中使用系统时间来判断间隔做逻辑运算,这个时间的判断没有任何意义。
  3. 在不考虑误差的前提下,定时间隔不会考虑任务的执行时间。例如,如果一项工作需要 3 分钟才能执行完成,并且计划每隔 5 分钟运行一次,那么每次任务之间只有 2 分钟的空闲时间。
  4. 需要注意的是 单例模式 运行的定时任务,任务的执行时间会影响该任务下一次执行的 开始时间。例如:一个每间隔 1 秒执行的任务,运行耗时为 1 秒,那么在 第1秒 开始运行后,下一次任务将会在 第3秒 开始执行。因为中间有一次运行检查时发现有当前任务正在进行,因此退出等待下一次执行检查。

定时器与 gcron 区别

具体请查看章节 定时任务-gcron与gtimer

相关文档

Source: SKILL.md on GitHub

1 alert16d5 checks · Risk MEDIUM
  • Gen Agent Trust Hub16d

    The skill provides a comprehensive development guide and code examples for the GoFrame v2 framework. While highly informative for Go developers, it contains several examples with hardcoded placeholder secrets, tokens, and specific local file paths to sensitive configuration files (like Kubernetes credentials). These elements are provided for demonstration purposes but represent insecure practices if used in production without modification.

  • Socket16d

    5 alerts: gptSecurity, gptAnomaly

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer6mo

    227/949 files flagged

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 911705d. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 4 months ago.

Steadyupdated 4 months ago
  • Go
  • Database
  • goframe
  • orm
  • http
  • grpc
  • microservices
  • crud
  • scaffolding
  • middleware

README badge

README badge for gogf/skills

Provides instruction and conventions for developing with GoFrame v2, a modular Go framework for building HTTP and gRPC services, microservices, and database operations using its ORM, CLI tooling, and project scaffolding. Use this skill when the target project has GoFrame v2 as a dependency or when the user requests scaffolding, migrations, or service development with GoFrame conventions.

Generated from the current SKILL.md.

When should I use this skill?
Use this skill only when your Go project uses GoFrame v2, indicated by github.com/gogf/gf/v2 in go.mod or imports. Do not use for generic Go projects, frontend work, or infrastructure tasks unrelated to GoFrame.
Should I manually set created_at and updated_at fields?
No. GoFrame automatically writes created_at on insert and updated_at on insert/update/save. Manually setting these fields is redundant and violates project conventions.
What should I use for database operations instead of g.Map?
Always use DO objects from internal/model/do/. DO struct fields are interface{}, unset fields remain nil and are automatically ignored by the ORM.
How does soft delete work in GoFrame?
Call Delete() on the DAO; GoFrame automatically converts it to UPDATE SET deleted_at = NOW(). Queries automatically filter out soft-deleted rows without requiring manual WhereNull conditions.
Where should I implement business logic?
Implement business logic directly in the service/ directory. Do not use the logic/ directory unless explicitly requested.

Generated from the current SKILL.md. These answers refresh after source changes.