cosgo

package module
v1.8.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 30, 2026 License: Apache-2.0 Imports: 19 Imported by: 22

README

cosgo

⚠️ 本项目由 AI 接管维护,不建议碳基生物阅读代码。

若必须阅读,请做好心理准备: 注释可能比代码还长,this 命名约定,少数位置存在"明知不标准但刻意保留"的历史选择。 同时也请理解: 代码风格一致性 > 追新,机器审美下一些 Go 1.22+ 特性(range over intslices.Contains 等)未被引入是刻意的。


cosgo 是一个 Go 应用脚手架 + 通用工具底座。它负责:

  • 应用生命周期:模块化启动/关闭、信号处理、配置、PID 文件、pprof、热重载
  • 运行时底座:路由注册、数据序列化、会话、存储、反射元数据、并发控制
  • 工具集:随机数、UUID、时间、切片、字节操作、加密、zset 排行榜

子模块索引

模块 职责 文档
registry/ 路由注册 + 基数树匹配 + 反射服务/方法注册 README
schema/ 结构体元数据 + 字段访问缓存 + 并发安全解析 README
binder/ 序列化(JSON/XML/YAML/Form/Protobuf/Msgpack/Bytes) -
session/ 会话管理,内存 / Redis 后端 README
storage/ 桶式对象存储 + O(1) 索引回收 README
scc/ goroutine 生命周期 + 超时 + 守护协程 -
values/ 通用值容器 *Message 错误载体 -
zset/ 跳表 + 字典双维护的有序集合 README
random/ crypto/rand 随机字符串 + math/rand 概率工具 -
request/ HTTP 请求辅助 + OAuth1 签名 -
utils/ 地址/字节/加密/时间轮 -
times/ 时间格式 + 周期计算 -
await/safety/slice/uuid/redis/ 同名领域的零依赖工具 -

根包文件

cosgo.go    模块注册/启动入口
config.go   基于 viper 的配置层
events.go   启停事件广播(Begin/Loaded/Started/Closing/Stopped/Reload)
signal.go   SIGINT/SIGHUP/SIGQUIT/SIGTERM/SIGUSR1 处理
module.go   Module 接口定义
init.go     应用初始化骨架
options.go  全局选项表
pidfile.go  原子写入的 PID 文件
pprof.go    独立 mux 的 pprof 服务器(避免污染 DefaultServeMux)
reload.go   SIGUSR1 触发的配置热重载

快速开始

package main

import (
    "github.com/hwcer/cosgo"
    "github.com/hwcer/logger"
)

type MyModule struct{}

func (m *MyModule) Id() string      { return "mymodule" }
func (m *MyModule) Init() error     { logger.Info("init");  return nil }
func (m *MyModule) Start() error    { logger.Info("start"); return nil }
func (m *MyModule) Close() error    { logger.Info("close"); return nil }

func main() {
    cosgo.Use(&MyModule{})
    cosgo.Start(true) // true = 阻塞等信号
}

Module 接口

type Module interface {
    Id() string
    Init() error     // 应用启动,顺序调用
    Start() error    // 所有 Init 完成后,顺序调用
    Close() error    // 收到退出信号,逆序调用
}

生命周期事件

cosgo.On(cosgo.EventTypStarted, func() error {
    logger.Info("app ready")
    return nil
})

事件列表:

  • EventTypBegin — 启动入口
  • EventTypLoaded — 所有 Init 完成
  • EventTypStarted — 所有 Start 完成(服务就绪)
  • EventTypClosing — 收到退出信号
  • EventTypStopped — 所有 Close 完成
  • EventTypReloadSIGUSR1 触发热重载

配置

cosgo.Config.SetDefault("server.port", 8080)
port := cosgo.Config.GetInt("server.port")

优先级(高→低):运行时 Set > CLI flags > 环境变量 > 配置文件 > 默认值。

日志级别:未显式设置时,debug=trueLevelDebug(最详细),debug=falseLevelInfo(生产降噪)。

pprof

只在 config.pprof 配置了地址时才启动,并且走独立 mux(不污染 http.DefaultServeMux),防止业务服务意外暴露 /debug/pprof

pprof: "127.0.0.1:6060"  # 通常仅监听本地

信号

信号 行为
SIGINT / SIGQUIT / SIGTERM 触发优雅关闭
SIGHUP 关闭控制台输出
SIGUSR1 (0xa) 配置热重载,发 EventTypReload

SIGKILL 无法捕获(OS 直接杀进程),程序不感知。

信号处理顺序(优雅关闭)

收到 SIGINT/SIGTERM
  → 发 EventTypClosing
  → 逆序调用各 Module.Close()
  → scc.Cancel() 取消所有注册的协程
  → 等待 goroutine 清理完毕
  → 发 EventTypStopped
  → 进程退出

贡献

欢迎 Issue 和 PR。代码风格遵循仓库已有约定(this receiver、特定 godoc 风格),请不要为了迎合现代 Go linter 而大改。

许可证

MIT

Documentation

Index

Constants

View Source
const (
	AppName                     string = "name"
	AppPprof                    string = "pprof"
	AppDebug                    string = "debug"
	AppBinDir                   string = "bin"           //程序目录
	AppWorkerDir                string = "dir"           //工作目录
	AppConfigNamePidFile        string = "pid"           //pid file dir
	AppConfigNameLogsPath       string = "logs.path"     //logs dir
	AppConfigNameLogsLevel      string = "logs.level"    //logs level
	AppConfigNameConfigFileExt  string = "ConfigFileExt" //config file default ext name,只能在程序中设置
	AppConfigNameConfigFileName string = "config"        //config file

)
View Source
const LoggerFileName = "file"

Variables

View Source
var Config = &config{Viper: viper.New()}
View Source
var GCSummaryTime time.Duration = time.Second * 300

GCSummaryTime 报告性能摘要时间间隔

View Source
var Options = &struct {
	Banner  func()
	Process func() bool //设置启动进程,返回false时不会继续向下执行
}{
	Banner: defaultBanner,
}
View Source
var SignalReload syscall.Signal = 0xa

SignalReload 重新加载系统信号 kill -10 pid

View Source
var Version string = "unknown"

Functions

func Abs

func Abs(dir ...string) string

Abs 获取以工作路径为起点的绝对路径

func Close

func Close() bool

Close 外部关闭程序

func Debug

func Debug() bool

func Dir

func Dir() string

Dir 程序工作目录

func Exist added in v1.2.0

func Exist(name string) bool

func Name

func Name() string

Name 项目内部获取appName

func On

func On(e EventType, f EventFunc)

func Range added in v0.0.5

func Range(f func(Module) bool)

func SIGHUP added in v0.0.2

func SIGHUP()

SIGHUP 关闭控制台

func SetBanner

func SetBanner(f func())

SetBanner 设置默认启动界面,启动完所有MOD后执行

func SetProcess

func SetProcess(f func() bool)

func Start

func Start(waitForSystemExit bool, mods ...Module)

func Use

func Use(mods ...Module)

func WaitForSystemExit

func WaitForSystemExit()

Types

type EventFunc

type EventFunc func() error

type EventType

type EventType int32
const (
	EventTypBegin   EventType = iota //开始启动
	EventTypLoaded                   //(Init)加载完成
	EventTypStarted                  //启动完成
	EventTypClosing                  //开始关闭
	EventTypStopped                  //停止之后
	EventTypReload                   //reload
)

type Module

type Module interface {
	// Id 返回模块的唯一标识
	Id() string
	// Init 模块初始化,在应用启动时调用
	// 此阶段主要进行模块的初始化工作,如配置加载、资源分配等
	Init() error
	// Start 模块启动,在所有模块初始化完成后调用
	// 此阶段主要进行模块的业务逻辑启动,如启动服务、监听端口等
	Start() error
	// Close 模块关闭,在应用关闭时调用
	// 此阶段主要进行模块的资源释放、连接关闭等清理工作
	Close() error
}

Module 模块接口,所有需要被cosgo管理的模块都需要实现此接口

type Reload added in v0.0.2

type Reload interface {
	// Reload 模块重载,在应用收到重载信号时调用
	// 此阶段主要进行模块的配置重新加载、状态刷新等操作
	Reload() error
}

Reload 模块重载接口,实现此接口的模块支持运行时重载

Directories

Path Synopsis
test/simple command
Package session 提供会话管理功能,支持内存和Redis存储
Package session 提供会话管理功能,支持内存和Redis存储
ZSet 有序集合
ZSet 有序集合

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL