etcd

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2026 License: MIT Imports: 8 Imported by: 0

README

etcd Source · etcd 脚本来源

Go Version

基于 etcd KV 存储的脚本来源,支持原生 Watch 热更新

中文 · English · 日本語


概述

etcd Source 使用 etcd clientv3 从 etcd 集群读取脚本。利用 etcd 原生的 Watch API 实现高效的热更新检测,无需轮询。

特性
特性 说明
底层库 go.etcd.io/etcd/client/v3
热更新 etcd 原生 Watch(PUT / DELETE 事件触发)
Key 前缀 支持 WithPrefix 命名空间隔离
认证 支持用户名/密码
接口 实现 source.ReadWatcher

安装

go get github.com/tx7do/go-scripts/source/etcd

配置选项

选项 默认值 说明
WithEndpoints(addr...) localhost:2379 etcd 服务器地址
WithUsername(user) 认证用户名
WithPassword(pass) 认证密码
WithPrefix(prefix) Key 前缀(自动去除前导 /
WithTimeout(d) 5s 连接超时

快速开始

package main

import (
    "context"
    "fmt"
    etcdSrc "github.com/tx7do/go-scripts/source/etcd"
)

func main() {
    ctx := context.Background()

    src, err := etcdSrc.New(ctx,
        etcdSrc.WithEndpoints("localhost:2379"),
        etcdSrc.WithPrefix("scripts/lua/"),
    )
    if err != nil { panic(err) }
    defer src.Close()

    // 加载脚本
    code, err := src.Load(ctx, "hello.lua")
    if err != nil { panic(err) }
    fmt.Println(code)

    // 热更新监听
    ch, _ := src.Watch(ctx, "hello.lua")
    go func() {
        for range ch {
            fmt.Println("脚本已变更,请重新加载")
        }
    }()
}

测试

cd source/etcd && go test -v ./...

相关文档

License

MIT License

Documentation

Overview

Package etcd provides a source.Reader implementation that reads scripts from an etcd cluster. It leverages etcd's native Watch API for efficient hot-reload detection without polling.

Construction:

src, err := etcd.New(ctx,
    etcd.WithEndpoints("localhost:2379"),
    etcd.WithPrefix("scripts/lua/"),
)

Hot-reload uses etcd's built-in Watch mechanism: a watch is established on the key and a signal is sent whenever a PUT or DELETE event occurs.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("etcd source: key not found")

ErrNotFound is returned (wrapped) by Load when the requested key does not exist in etcd. Detect with errors.Is(err, ErrNotFound) or the convenience helper IsNotFound.

Functions

func IsNotFound

func IsNotFound(err error) bool

IsNotFound reports whether err represents a "key not found" response from etcd. Equivalent to errors.Is(err, ErrNotFound).

Types

type Option

type Option func(*configOptions)

Option configures a Reader. Pass to New.

func WithEndpoints

func WithEndpoints(endpoints ...string) Option

WithEndpoints sets the etcd server endpoints (default ["localhost:2379"]).

func WithPassword

func WithPassword(password string) Option

WithPassword sets the etcd password for authentication.

func WithPrefix

func WithPrefix(prefix string) Option

WithPrefix sets a key prefix that is transparently prepended to every key before it is resolved against etcd. Useful when all scripts share a common namespace (e.g. WithPrefix("scripts/lua/")).

Leading slashes are stripped; no other normalization is applied.

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout sets the etcd client timeout (default 5s).

func WithUsername

func WithUsername(username string) Option

WithUsername sets the etcd username for authentication.

type Reader

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

Reader reads scripts from etcd.

All exported methods are safe for concurrent use. Reader implements the source.ReadWatcher interface.

func New

func New(_ context.Context, opts ...Option) (*Reader, error)

New creates an etcd-backed Reader. At least one endpoint must be supplied via WithEndpoints (default is "localhost:2379"). All other settings are optional.

Authentication can be enabled via WithUsername / WithPassword.

func (*Reader) Close

func (r *Reader) Close() error

Close releases the underlying etcd client.

func (*Reader) Load

func (r *Reader) Load(ctx context.Context, key string) (string, error)

Load fetches the value from etcd and returns it as a string. Context cancellation propagates to the underlying request.

An empty response (key not found) is reported as a wrapped ErrNotFound. Other errors are wrapped with the key for easier debugging.

func (*Reader) Watch

func (r *Reader) Watch(ctx context.Context, key string) (<-chan struct{}, error)

Watch returns a channel that signals when the value identified by `key` changes. It uses etcd's native Watch API for efficient event-driven notification without polling.

The returned channel is closed when the context is cancelled. Callers should re-Load the script after receiving from the channel.

Jump to

Keyboard shortcuts

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