conf_reload

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2023 License: MIT Imports: 10 Imported by: 0

README

conf-reload

Conf-Reload是一个Go库,用于常驻进程模式下配置文件信息获取,并支持配置热加载

工作流程概述:

  • 读取配置文件信息并基于Broker.unmarshaller进行解析

  • Broker会单独启动一个Goroutinefsnotify进行通信

  • fsnotify有文件创建或写事件进来时,Broker通知Engine重载配置文件

Features

  • 支持toml yaml json配置文件获取
  • 支持key多层级获取
  • 支持热更新,配置文件更新后,配置会重载
  • 基于mapstructure库实现,配置信息获取支持弱类型转化
  • 基于fsnotify库实现了IO多路复用的事件通知机制,性能较好
  • 配置结构获取提供多种类型转化api

Stability and Compatibility

Status: 目前处于功能拓展中,不过基础功能已经完善,不会在改变api结构

☝️ Important Note: v1.0.0之前为beta版本. v1.0.0版本为当前stable版本.

Quick Start

go get -u github.com/enpsl/conf-reload

_example

_example目录是测试用例,可copy到当前项目下测试运行

配置引擎加载,加载一次即可

f = "_example/example.toml"
conf_reload.LoadEngine(f, conf_relod.WithLevelSplit("."), conf_relod.WithLogLevel(0))

LoadEngine的一些option选项说明: WithLevelSplit(string)配置信息分隔符设置,默认是.

WithWeaklyTypedInput(bool) 调用DecodeToStruct时,会启用弱类型转化

WithLogger(Logger) 外部日志接入,需实现Logger,不传入会默认使用项目自带终端输出方式记录日志

WithCapacity(int) LRU缓存容量设置,低于当前设置级别的日志记录不会在终端输出,可按照下面展示的级别进行设置

WithWatched(int) 是否开启Broker Watch检测,某些场景如命令行模式,不需要热加载,可关闭此选项即可停止文件监听

WithLogLevel(int)日志级别设置,低于当前设置级别的日志记录不会在终端输出,可按照下面展示的级别进行设置

配置信息读取,可更改文件内容观察文件变化情况

var http = &Http{}
for {
    err := conf_reload.DecodeToStruct("server.http", http)
    if err != nil {
        panic(err)
    }
    fmt.Println(http)
    time.Sleep(2 * time.Second)
}

输出示例:

conf-reload-test (main) ✗ go run _example/example.go
conf-reload@v1.0.0: pid=16749 2023/02/19 12:37:42.571884 DEBUG: map[server:map[config:map[connection:false depends:[tcp ip] publish:2023-02-19 timeout:10s] http:map[host:0.0.0.0 port:8080]]]
conf-reload@v1.0.0: pid=16749 2023/02/19 12:37:42.571917 DEBUG: map[config:map[connection:false depends:[tcp ip] publish:2023-02-19 timeout:10s] http:map[host:0.0.0.0 port:8080]]
&{0.0.0.0 8080}
&{0.0.0.0 8080}
&{0.0.0.0 8080}
conf-reload@v1.0.0: pid=16749 2023/02/19 12:37:46.693127 DEBUG: modified file:/Users/kuailexingqiu/go/src/conf-reload-test/_example/example.toml, /Users/kuailexingqiu/go/src/conf-reload-test/_example/example.toml
conf-reload@v1.0.0: pid=16749 2023/02/19 12:37:46.693437 DEBUG: map[server:map[config:map[connection:false depends:[tcp ip] publish:2023-02-19 timeout:10s] http:map[host:0.0.0.0 port:8081]]]
conf-reload@v1.0.0: pid=16749 2023/02/19 12:37:48.572885 DEBUG: map[config:map[connection:false depends:[tcp ip] publish:2023-02-19 timeout:10s] http:map[host:0.0.0.0 port:8081]]
&{0.0.0.0 8081}
&{0.0.0.0 8081}

如果想了解更多api,See godoc

License

Copyright (c) 2023-present enpsl. conf-reload is free and open-source software licensed under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeToStruct

func DecodeToStruct(key string, out interface{}) error

DecodeToStruct The external exposure api is used for decoding, which can decode the value of the key map to the out variable

func Get

func Get(key string) interface{}

Get external exposure api to get any type value.

func GetBool

func GetBool(key string) bool

GetBool external exposure api to get bool type value.

func GetDuration

func GetDuration(key string) time.Duration

GetDuration external exposure api to get time.Duration type value.

func GetFloat64

func GetFloat64(key string) float64

GetFloat64 external exposure api to get float64 type value.

func GetInt

func GetInt(key string) int

GetInt external exposure api to get int type value.

func GetInt64

func GetInt64(key string) int64

GetInt64 external exposure api to get int64 type value.

func GetSlice

func GetSlice(key string) []interface{}

GetSlice external exposure api to get []interface{} type value.

func GetString

func GetString(key string) string

GetString external exposure api to get string type value.

func GetStringMap

func GetStringMap(key string) map[string]interface{}

GetStringMap external exposure api to get map[string]interface{} type value.

func GetStringMapString

func GetStringMapString(key string) map[string]string

GetStringMapString external exposure api to get map[string]string type value.

func GetStringMapStringSlice

func GetStringMapStringSlice(key string) map[string][]string

GetStringMapStringSlice external exposure api to get map[string][]string type value.

func GetStringSlice

func GetStringSlice(key string) []string

GetStringSlice external exposure api to get []string type value.

func GetTime

func GetTime(key string) time.Time

GetTime external exposure api to get time.Time type value.

func LoadEngine

func LoadEngine(path string, opts ...Option)

Types

type Engine

type Engine struct {
	RawData          []byte                 // config file original data
	LevelSplit       string                 // key get split
	WeaklyTypedInput bool                   // whether to startweak type conversion
	Logger           *log.Logger            // logger instance
	LocalStorage     *base.LRUCache         // fast cache
	Configure        map[string]interface{} // original config
	Broker           base.Broker            // broker
	Capacity         int                    // LRU Cache cap
	Watched          bool                   // watched switch
	// contains filtered or unexported fields
}

conf-reload Engine,Used to coordinate and manage broker

func NewEngine

func NewEngine() *Engine

NewEngine Engine init

func (*Engine) DecodeToStruct

func (e *Engine) DecodeToStruct(key string, i interface{}) error

DecodeToStruct Depends on the work of broker decode If Map corresponding to key is nil, will return an error

func (*Engine) Get

func (e *Engine) Get(key string) interface{}

Get the value corresponding to the key from LocalStorage. If it is not available, it will be found in the broker

func (*Engine) GetBool

func (e *Engine) GetBool(key string) bool

Engine.GetInt returns the value associated with the key as bool type.

func (*Engine) GetDuration

func (e *Engine) GetDuration(key string) time.Duration

Engine.GetDuration returns the value associated with the key as time.Duration type.

func (*Engine) GetFloat64

func (e *Engine) GetFloat64(key string) float64

Engine.GetFloat64 returns the value associated with the key as float64 type.

func (*Engine) GetInt

func (e *Engine) GetInt(key string) int

Engine.GetInt returns the value associated with the key as int type.

func (*Engine) GetInt64

func (e *Engine) GetInt64(key string) int64

Engine.GetInt64 returns the value associated with the key as int64 type.

func (*Engine) GetSlice

func (e *Engine) GetSlice(key string) []interface{}

Engine.GetSlice returns the value associated with the key as []interface{} type.

func (*Engine) GetString

func (e *Engine) GetString(key string) string

Engine.GetInt returns the value associated with the key as string type.

func (*Engine) GetStringMap

func (e *Engine) GetStringMap(key string) map[string]interface{}

Engine.GetStringMap returns the value associated with the key as map[string]interface{} type.

func (*Engine) GetStringMapString

func (e *Engine) GetStringMapString(key string) map[string]string

Engine.GetStringMapString returns the value associated with the key as map[string]string type.

func (*Engine) GetStringMapStringSlice

func (e *Engine) GetStringMapStringSlice(key string) map[string][]string

Engine.GetStringMapStringSlice returns the value associated with the key as map[string][]string type.

func (*Engine) GetStringSlice

func (e *Engine) GetStringSlice(key string) []string

Engine.GetStringSlice returns the value associated with the key as []string type.

func (*Engine) GetTime

func (e *Engine) GetTime(key string) time.Time

Engine.GetTime returns the value associated with the key as time.Time type.

func (*Engine) Load

func (e *Engine) Load(path string, opts ...Option) error

Load the configuration file information and initialize the broker. The broker will start an additional process to receive the file change chan notification

type Logger

type Logger interface {
	// Debug logs a message at Debug level.
	Debug(args ...interface{})

	// Info logs a message at Info level.
	Info(args ...interface{})

	// Warn logs a message at Warning level.
	Warn(args ...interface{})

	// Error logs a message at Error level.
	Error(args ...interface{})

	// Fatal logs a message at Fatal level
	// and process will exit with status set to 1.
	Fatal(args ...interface{})
}

type Option

type Option func(*Engine)

func WithCapacity

func WithCapacity(capacity int) Option

WithCapacity LRU cap options

func WithLevelSplit

func WithLevelSplit(split string) Option

WithLevelSplit config file separator options

func WithLogLevel

func WithLogLevel(level int32) Option

WithLogLevel Log level options

func WithLogger

func WithLogger(logger Logger) Option

WithLogger Logger options, The logger must be implement Logger

func WithWatched added in v1.0.0

func WithWatched(watched bool) Option

WithWatched Broker watch swich options

func WithWeaklyTypedInput

func WithWeaklyTypedInput(weaklyTypedInput bool) Option

WithWeaklyTypedInput options Whether to start weak type conversion See details https://github.com/mitchellh/mapstructure/blob/main/mapstructure_examples_test.go

Directories

Path Synopsis
internal
app
fs
log

Jump to

Keyboard shortcuts

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