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组件列表系统相关进程管理-gproc进程管理-信号监听.md

基本介绍

gproc 组件提供了统一的信号监听和回调处理功能,目的是解决在程序中多个不同组件冗余的信号处理逻辑,以及接收退出信号后无法平滑析构的痛点。在没有统一退出信号监听的组件下,当多个组件通过 goroutine 异步监听信号,主 goroutine 接收到退出信号往往会直接退出,或者等待一段无法预测的时间退出,造成程序其实无法平滑退出,可能引起一些不可预料的问题。 gproc 通过统一的信号注册和回调处理,使得各个组件能够有效地接收到退出信号并做相应析构处理,使得程序的信号处理逻辑更加严谨。

相关方法:

// AddSigHandler adds custom signal handler for custom one or more signals.
func AddSigHandler(handler SigHandler, signals ...os.Signal)

// AddSigHandlerShutdown adds custom signal handler for shutdown signals:
// syscall.SIGINT,
// syscall.SIGQUIT,
// syscall.SIGKILL,
// syscall.SIGTERM,
// syscall.SIGABRT.
func AddSigHandlerShutdown(handler ...SigHandler)

// Listen blocks and does signal listening and handling.
func Listen()

简要介绍:

  • AddSigHanlder 方法用于添加对指定信号的监听和对应回调函数注册。
  • AddSigHandlerShutdown 方法用于添加对常见进程退出信号的监听和对应回调函数注册,可以注册多个 SigHandler。
  • Listen 方法用于阻塞监听信号并自动执行回调函数调用。

我们来看两个示例。

示例1,使用标准库信号监听

使用标准库信号监听机制的常见代码逻辑如下:

package main

import (
    "fmt"
    "os"
    "os/signal"
    "syscall"
    "time"
)

func signalHandlerForMQ() {
    var (
        sig          os.Signal
        receivedChan = make(chan os.Signal)
    )
    signal.Notify(
        receivedChan,
        syscall.SIGINT,
        syscall.SIGQUIT,
        syscall.SIGKILL,
        syscall.SIGTERM,
        syscall.SIGABRT,
    )
    for {
        sig = <-receivedChan
        fmt.Println("MQ is shutting down due to signal:", sig.String())
        time.Sleep(time.Second)
        fmt.Println("MQ is shut down smoothly")
        return
    }
}

func main() {
    fmt.Println("Process start, pid:", os.Getpid())
    go signalHandlerForMQ()

    var (
        sig          os.Signal
        receivedChan = make(chan os.Signal)
    )
    signal.Notify(
        receivedChan,
        syscall.SIGINT,
        syscall.SIGQUIT,
        syscall.SIGKILL,
        syscall.SIGTERM,
        syscall.SIGABRT,
    )
    for {
        sig = <-receivedChan
        fmt.Println("MainProcess is shutting down due to signal:", sig.String())
        return
    }
}

我们通过 go run 命令来执行一下,随后通过 Ctrl+C 快捷键退出( Mac 用户通过 Command+C)。

$ go run signal_handler.go
Process start, pid: 21928
^CMainProcess is shutting down due to signal: interrupt
MQ is shutting down due to signal: interrupt

可以看到,好可惜,那个 MQ 的 goroutine 还没完全退出进程即被强行关闭。

示例2,使用 gproc 信号监听

使用 gproc 组件改进后的信号监听机制如下:

package main

import (
    "fmt"
    "github.com/gogf/gf/v2/os/gproc"
    "os"
    "time"
)

func signalHandlerForMQ(sig os.Signal) {
    fmt.Println("MQ is shutting down due to signal:", sig.String())
    time.Sleep(time.Second)
    fmt.Println("MQ is shut down smoothly")
}

func signalHandlerForMain(sig os.Signal) {
    fmt.Println("MainProcess is shutting down due to signal:", sig.String())
}

func main() {
    fmt.Println("Process start, pid:", os.Getpid())
    gproc.AddSigHandlerShutdown(
        signalHandlerForMQ,
        signalHandlerForMain,
    )
    gproc.Listen()
}

我们通过 go run 命令来执行一下,随后通过 Ctrl+C 快捷键退出( Mac 用户通过 Command+C)。

$ go run signal_handler_gproc.go
Process start, pid: 22876
^CMQ is shutting down due to signal: interrupt
MainProcess is shutting down due to signal: interrupt
MQ is shut down smoothly

看到差别了吧!所有的信号监听函数都正常结束后,随后进程平滑退出。

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.