mesh

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2026 License: MIT Imports: 23 Imported by: 0

Documentation

Overview

Package mesh provides the embeddable GopherMesh engine used to load config, expose HTTP/TCP routes, cold-start local backends, and run the dashboard.

The recommended integration order is:

  1. Start with CLI + config.json when you only need routing and process orchestration.
  2. Use this package when you need to embed GopherMesh into a custom Go launcher.

This package currently implements the single-node gateway/runtime model. It should not be described as a full distributed service mesh.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SaveConfig

func SaveConfig(path string, cfg Config) error

SaveConfig 将配置序列化并安全写入磁盘

Types

type BackendConfig

type BackendConfig struct {
	Name         string   `json:"name,omitempty"`
	Cmd          string   `json:"cmd"`                     // 执行的二进制目标,为空或"internal"表示内部路由
	Args         []string `json:"args,omitempty"`          // 启动参数
	InternalHost string   `json:"internal_host,omitempty"` // 目标主机IP/域名
	InternalPort string   `json:"internal_port"`           // 目标进程实际监听的本地端口
}

BackendConfig 定义路由下单个实际承载请求的后端。

type Config

type Config struct {
	ConfigPath     string                 `json:"-"`
	DashboardHost  string                 `json:"dashboard_host"`
	DashboardPort  string                 `json:"dashboard_port"`
	TrustedOrigins []string               `json:"trusted_origins"`
	ServiceName    string                 `json:"service_name,omitempty"`
	InternalPort   string                 `json:"internal_port,omitempty"`
	Routes         map[string]RouteConfig `json:"routes,omitempty"`
}

Config 代表 GopherMesh 全局配置(支持无头静默运行)

func DefaultConfig

func DefaultConfig() Config

DefaultConfig 生成包含防环路机制与默认放行白名单的配置

func LoadConfig

func LoadConfig(path string) (Config, error)

LoadConfig 从指定路径加载配置,若文件不存在则初始化默认配置并落盘

func (Config) Normalize

func (c Config) Normalize() (Config, error)

Normalize 校验用户配置的合法性,并安全补全缺失的默认值

type Engine

type Engine struct {
	// contains filtered or unexported fields
}

Engine 是 MeshEngine 接口的具体实现

func (*Engine) GetConfigJSON

func (e *Engine) GetConfigJSON() []byte

GetConfigJSON 返回当前 Routes 的 JSON 字符串

func (*Engine) GetLogs

func (e *Engine) GetLogs(port string) []string

func (*Engine) GetStatus

func (e *Engine) GetStatus() map[string]dashboard.RouteStatus

GetStatus 实现 dashboard.MeshState 接口,收集底层进程快照

func (*Engine) KillProcess

func (e *Engine) KillProcess(port string) error

KillProcess 实现 dashboard.MeshState 接口,支持手动kill底层进程

func (*Engine) ReloadConfig

func (e *Engine) ReloadConfig(rawJSON []byte) error

ReloadConfig 接收前端传来的新 JSON,验证、落盘、并执行平滑重载

func (*Engine) Role

func (e *Engine) Role() Role

Role 返回当前实例运行的身份

func (*Engine) Run

func (e *Engine) Run(ctx context.Context) error

Run 启动引擎并阻塞,直到ctx被取消

func (*Engine) Shutdown

func (e *Engine) Shutdown(ctx context.Context) error

Shutdown 触发安全退出,释放端口

type LogBuffer

type LogBuffer struct {
	// contains filtered or unexported fields
}

LogBuffer 是一个并发安全的行级环形缓冲区,实现了 io.Writer 端口

func NewLogBuffer

func NewLogBuffer(maxLines int) *LogBuffer

NewLogBuffer 创造指定最大行数的日志截获器

func (*LogBuffer) Lines

func (b *LogBuffer) Lines() []string

Lines 返回当前缓存的所有完整日志行快照

func (*LogBuffer) Write

func (b *LogBuffer) Write(p []byte) (n int, err error)

Write 拦截操作系统的标准输出流,按换行符进行切分缓存

type MeshEngine

type MeshEngine interface {
	// Role 返回当前实例运行的身份
	Role() Role

	// Run 启动代理或注册工作节点,并阻塞直到ctx被取消
	Run(ctx context.Context) error

	// Shutdown 触发安全退出,释放端口并优雅中介所有托管的子进程
	Shutdown(ctx context.Context) error
}

MeshEngine 定义了GopherMesh核心引擎的生命周期契约

func NewEngine

func NewEngine(cfg Config) (MeshEngine, error)

NewEngine 负责初始化并探测节点角色

type ProcessInfo

type ProcessInfo struct {
	Cmd       *exec.Cmd
	StartTime time.Time
}

type Role

type Role string

Role 定义节点的运行角色

const (
	RoleMaster Role = "master" // 主控节点:负责监听、劫持流量、按需静默拉起子进程
	RoleWorker Role = "worker" // 工作节点:被拉起的业务进程(当作为SDK引入时使用)
)

type RouteConfig

type RouteConfig struct {
	Name        string          `json:"name"`
	Protocol    string          `json:"protocol,omitempty"`     // 默认为 http,可配置为 tcp
	LoadBalance string          `json:"load_balance,omitempty"` // 支持 round_robin / least_conn / ip_hash
	Backends    []BackendConfig `json:"backends,omitempty"`
}

RouteConfig 定义单个对外暴露端口的路由规则。

Jump to

Keyboard shortcuts

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