sngo

module
v0.0.0-...-6374b6f Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2025 License: MIT

README

SNGO - 强类型 Skynet 框架

Go Version License Build Status

SNGO 是一个用 Go 语言实现的现代化、强类型的 Actor 模型框架,提供与经典 Skynet 框架相同的 API 兼容性,同时具备更好的类型安全性、性能优化和分布式系统支持。

🚀 核心特性

✅ 完整的 Actor 系统
  • 强类型消息传递:编译期类型检查,运行时安全
  • 高效路由机制:直接投递、广播、一致性哈希
  • Handle 管理:全局唯一标识符和自动垃圾回收
  • 服务注册发现:动态服务管理和负载均衡
✅ 高性能网络层
  • TCP/UDP 支持:高并发网络服务器
  • 连接池管理:自动连接管理和资源优化
  • 二进制协议:高效消息编解码
  • 零拷贝优化:最小化内存分配和拷贝
✅ 灵活配置系统
  • 多格式支持:YAML、JSON 配置文件
  • 热重载:运行时动态配置更新
  • 环境变量:覆盖和扩展配置
  • 类型验证:强类型配置结构和验证
✅ 企业级 Bootstrap 框架
  • 依赖注入:现代化 IoC 容器
  • 生命周期管理:服务编排和健康监控
  • 事件系统:生命周期事件和扩展机制
  • 优雅关闭:资源清理和状态保存
✅ 分布式集群支持
  • 节点管理:自动节点发现和故障检测
  • 远程调用:跨节点 Actor 通信
  • 服务注册:分布式服务发现
  • 负载均衡:智能请求分发

📋 项目状态

当前版本: v1.0.0
开发进度: 100% 完成 (8/8 阶段)

  • 阶段 1-3: 核心 Actor 系统
  • 阶段 4-5: 网络层和服务发现
  • 阶段 6: 配置系统
  • 阶段 7: Bootstrap 和生命周期管理
  • 阶段 8: 集群支持

🚀 快速开始

安装
go mod init your-project
go get github.com/najoast/sngo
基本使用
package main

import (
    "context"
    "fmt"
    "log"
    
    "github.com/najoast/sngo/bootstrap"
    "github.com/najoast/sngo/core"
)

// 定义消息类型
type EchoRequest struct {
    Message string `json:"message"`
}

type EchoResponse struct {
    Reply string `json:"reply"`
}

// 实现 Actor
type EchoActor struct {
    core.BaseActor
}

func (a *EchoActor) Handle(ctx context.Context, message interface{}) (interface{}, error) {
    switch msg := message.(type) {
    case *EchoRequest:
        return &EchoResponse{
            Reply: "Echo: " + msg.Message,
        }, nil
    default:
        return nil, fmt.Errorf("unknown message type: %T", message)
    }
}

func main() {
    // 创建应用
    app := bootstrap.NewApplication()
    
    // 获取 Actor 系统
    container := app.Container()
    actorSystem, _ := container.Resolve("actor_system")
    system := actorSystem.(core.ActorSystem)
    
    // 启动应用
    ctx := context.Background()
    if err := app.Run(ctx); err != nil {
        log.Fatal(err)
    }
    
    // 创建 Actor
    actor := &EchoActor{}
    handle, err := system.CreateActor(actor)
    if err != nil {
        log.Fatal(err)
    }
    
    // 发送消息
    request := &EchoRequest{Message: "Hello SNGO!"}
    response, err := system.Call(ctx, handle, request)
    if err != nil {
        log.Fatal(err)
    }
    
    // 打印结果
    if reply, ok := response.(*EchoResponse); ok {
        fmt.Println(reply.Reply) // 输出: Echo: Hello SNGO!
    }
}
集群应用
package main

import (
    "context"
    "log"
    
    "github.com/najoast/sngo/cluster"
)

func main() {
    // 创建集群应用
    app, err := cluster.NewClusterApp("config.yaml")
    if err != nil {
        log.Fatal(err)
    }
    
    // 注册服务
    remoteService := app.GetRemoteService()
    handler := &MyServiceHandler{}
    remoteService.Register("my-service", handler)
    
    // 启动应用
    ctx := context.Background()
    if err := app.Start(ctx); err != nil {
        log.Fatal(err)
    }
}

type MyServiceHandler struct{}

func (h *MyServiceHandler) Handle(ctx context.Context, request interface{}) (interface{}, error) {
    return "处理结果: " + fmt.Sprint(request), nil
}

📖 文档

核心文档
阶段性文档

🎯 示例项目

基础示例
高级示例

🏗️ 项目结构

sngo/
├── core/           # 核心 Actor 系统
├── network/        # 网络通信层
├── config/         # 配置管理系统
├── bootstrap/      # 应用框架
├── cluster/        # 集群支持
├── examples/       # 示例项目
├── docs/           # 技术文档
└── test/           # 集成测试

🔧 配置示例

# config.yaml
application:
  name: "my-sngo-app"
  version: "1.0.0"
  debug: true

actor:
  pool_size: 1000
  message_queue_size: 1000
  gc_interval: 30s

network:
  tcp:
    address: "0.0.0.0:8080"
    max_connections: 10000
    read_timeout: 30s
    write_timeout: 30s
  
cluster:
  cluster_name: "my-cluster"
  bind_port: 7946
  seed_nodes:
    - "node1.example.com:7946"
    - "node2.example.com:7946"
  heartbeat_interval: 1s

🚦 性能特性

并发性能
  • Actor 吞吐量: 100万+ 消息/秒
  • 网络连接: 支持 10万+ 并发连接
  • 内存使用: 单 Actor ~2KB 内存占用
  • 启动时间: < 100ms 冷启动
扩展性
  • 水平扩展: 支持 100+ 节点集群
  • 服务数量: 支持 10万+ 服务实例
  • 消息路由: 微秒级消息路由延迟

🔒 生产就绪特性

可靠性
  • 容错机制: Actor 监督和自动重启
  • 优雅关闭: 完整的资源清理流程
  • 健康检查: 实时服务健康监控
  • 错误隔离: 防止级联失败
可观测性
  • 结构化日志: JSON 格式的详细日志
  • 性能指标: 内置性能监控
  • 分布式追踪: 请求链路追踪
  • 运行时监控: 实时系统状态
安全性
  • 类型安全: 编译期类型检查
  • 资源限制: 内存和 CPU 使用限制
  • 访问控制: 基于角色的权限管理
  • 加密通信: TLS 加密支持(规划中)

🤝 贡献

我们欢迎社区贡献!请查看 贡献指南

贡献方式
  • 🐛 报告 Bug
  • 💡 提交功能请求
  • 📖 改进文档
  • 🔧 提交代码
开发环境
# 克隆项目
git clone https://github.com/najoast/sngo.git
cd sngo

# 安装依赖
go mod download

# 运行测试
go test ./...

# 运行示例
cd examples/hello_world
go run main.go

📊 基准测试

# Actor 性能测试
cd core
go test -bench=. -benchmem

# 网络性能测试  
cd network
go test -bench=. -benchmem

# 集群性能测试
cd cluster  
go test -bench=. -benchmem

🛣️ 路线图

v1.1.0 (规划中)
  • 🔐 TLS 加密通信
  • 📊 Prometheus 监控集成
  • 🔍 OpenTelemetry 追踪支持
  • 📱 管理界面
v1.2.0 (规划中)
  • 🌐 HTTP/gRPC 网关
  • 🗄️ 持久化存储支持
  • 🔄 服务网格集成
  • 🚀 云原生部署支持

📜 许可证

本项目采用 MIT 许可证

🙏 致谢

感谢 Skynet 项目提供的设计灵感。


开始使用 SNGO 构建你的下一个高性能分布式应用!

如有问题或建议,请提交 Issue 或联系维护者。

Directories

Path Synopsis
Package bootstrap provides application implementation
Package bootstrap provides application implementation
example command
Example demonstrating the SNGO bootstrap system
Example demonstrating the SNGO bootstrap system
Package cluster provides distributed cluster support for SNGO framework
Package cluster provides distributed cluster support for SNGO framework
Package config provides error definitions for configuration management
Package config provides error definitions for configuration management
Package core implements the fundamental Actor system for SNGO.
Package core implements the fundamental Actor system for SNGO.
client/test command
cluster_example command
echo_client command
Package main provides a simple echo client example using SNGO network layer
Package main provides a simple echo client example using SNGO network layer
echo_server command
Package main provides a simple echo server example using SNGO network layer
Package main provides a simple echo server example using SNGO network layer
login/client command
login/server command
simpledb/test command
Package network provides connection management implementation
Package network provides connection management implementation
Package protocol provides protocol support for SNGO framework
Package protocol provides protocol support for SNGO framework

Jump to

Keyboard shortcuts

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