consul

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2026 License: MIT Imports: 8 Imported by: 0

README

Consul Source · Consul 脚本来源

Go Version

基于 HashiCorp Consul KV 存储的脚本来源,支持 ModifyIndex 轮询热更新

中文 · English · 日本語


概述

Consul Source 使用 Consul API 从 Consul KV 存储读取脚本。通过轮询 Key 的 ModifyIndex 变化来检测热更新。

特性
特性 说明
底层库 github.com/hashicorp/consul/api
热更新 ModifyIndex 轮询(每 5 秒)
Key 前缀 支持 WithPrefix 命名空间隔离
认证 支持 ACL Token
接口 实现 source.ReadWatcher

安装

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

配置选项

选项 默认值 说明
WithAddress(addr) 127.0.0.1:8500 Consul Agent 地址
WithToken(token) ACL 认证 Token
WithPrefix(prefix) Key 前缀(自动去除前导 /
WithTimeout(d) 5s HTTP 超时

快速开始

package main

import (
    "context"
    "fmt"
    consulSrc "github.com/tx7do/go-scripts/source/consul"
)

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

    src, err := consulSrc.New(ctx,
        consulSrc.WithAddress("127.0.0.1:8500"),
        consulSrc.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)
}

测试

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

相关文档

License

MIT License

Documentation

Overview

Package consul provides a source.Reader implementation that reads scripts from a HashiCorp Consul KV store (or any Consul-compatible agent such as Nomad's embedded KV, etc.).

Construction:

src, err := consul.New(ctx,
    consul.WithAddress("127.0.0.1:8500"),
    consul.WithPrefix("scripts/lua/"),
)

Hot-reload detection polls the key's ModifyIndex via Get every 5 seconds and sends a signal when the index changes.

Index

Constants

This section is empty.

Variables

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

ErrNotFound is returned (wrapped) by Load when the requested key does not exist in Consul's KV store. 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 Consul. Equivalent to errors.Is(err, ErrNotFound).

Types

type Option

type Option func(*configOptions)

Option configures a Reader. Pass to New.

func WithAddress

func WithAddress(address string) Option

WithAddress sets the Consul agent address (default "127.0.0.1:8500").

func WithPrefix

func WithPrefix(prefix string) Option

WithPrefix sets a key prefix that is transparently prepended to every key before it is resolved against Consul. 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 Consul client HTTP timeout (default 5s). This only applies when the Reader constructs its own client (i.e. not in tests).

func WithToken

func WithToken(token string) Option

WithToken sets the ACL token used for authentication.

type Reader

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

Reader reads scripts from Consul's KV store.

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 a Consul-backed Reader. Address defaults to "127.0.0.1:8500"; override with WithAddress. All other settings are optional.

Authentication can be enabled via WithToken.

func (*Reader) Close

func (r *Reader) Close() error

Close releases the underlying Consul client resources, if any.

func (*Reader) Load

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

Load fetches the value from Consul's KV store and returns it as a string. Context cancellation propagates to the underlying request via QueryOptions.

A nil KVPair (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 polls the key's ModifyIndex via Get every 5 seconds and sends a signal on the channel when the index differs from the one recorded during the last Load.

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