redconf

package module
Version: v0.0.0-...-c90807a Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2016 License: Apache-2.0 Imports: 10 Imported by: 0

README

RedConf

Sync config from redis or others storages while the key's value changed

Usage
  • The following struct is the config what we want sync with storage while the values changed
type ServerConfig struct {
	Host     string
	Port     int
	AllowIPs []string
}

type LogConfig struct {
	Path    string
	Maxsize int
}

type AppConfig struct {
	Server ServerConfig
	Log    LogConfig
}
  • We need create storage for tell redconf where the config values stored, and create monitor to notify the redconf while the values changed

	opts = redconf.Options{
		"address":  "localhost:6379",
		"password": "",
		"db":       0,
		"idle":     10,
		"channel":  "ONCHANGED",
	}

	if monitor, err = redconf.CreateMonitor("redis", opts); err != nil {
		fmt.Println(err)
		return
	}

	if storage, err = redconf.CreateStorage("redis", opts); err != nil {
		fmt.Println(err)
		return
	}
  • Create RedConf instance and watch the config
	if redConf, err = redconf.New(namespace, storage, monitor); err != nil {
		return
	}

	appConf := AppConfig{}

	if err = redConf.Watch(&appConf); err != nil {
		fmt.Println(err)
		return
	}
  • Initial redis key-value
$> redis-cli
127.0.0.1:6379> SET GOGAP:AppConfig:Server:AllowIPs 127.0.0.1,202.10.5.123
OK

  • Run example code
$> go run example/*.go
  • Open new terminal session and change the config in redis
$> redis-cli
127.0.0.1:6379>SET GOGAP:AppConfig:Server:AllowIPs 127.0.0.1,202.10.5.125
OK
127.0.0.1:6379> PUBLISH ONCHANGED GOGAP:AppConfig:Server:AllowIPs
(integer) 1

Then you will see the change from your terminal

  • if you want subscribe the value change event, you could do as following:
func onValueChangedSubscriber(event redconf.OnValueChangedEvent) {
	var err error
	var data []byte
	if data, err = json.MarshalIndent(&event, "", "    "); err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(string(data))
}


redConf.Subscribe(onValueChangedSubscriber)

Documentation

Index

Constants

View Source
const (
	DefaultSubscribeChannel = "REDCONF:ONCHANGED"
)

Variables

This section is empty.

Functions

func RegisterMonitor

func RegisterMonitor(dirverName string, newFunc NewMonitorFunc) (err error)

func RegisterStorage

func RegisterStorage(dirverName string, newFunc NewStorageFunc) (err error)

Types

type Field

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

func (*Field) Kind

func (p *Field) Kind() reflect.Kind

func (*Field) Level

func (p *Field) Level() int

func (*Field) Name

func (p *Field) Name() string

func (*Field) Parents

func (p *Field) Parents() []string

func (*Field) String

func (p *Field) String() string

func (*Field) Type

func (p *Field) Type() reflect.Type

func (*Field) Value

func (p *Field) Value() (currentVal interface{})

type KeyContentChangedCallback

type KeyContentChangedCallback func(namespace, key string)

type Monitor

type Monitor interface {
	Watch(namespace string, callback KeyContentChangedCallback, onError OnWatchingError) (err error)
}

func CreateMonitor

func CreateMonitor(driverName string, opts Options) (monitor Monitor, err error)

func NewRedisMonitor

func NewRedisMonitor(opts Options) (monitor Monitor, err error)

type NewMonitorFunc

type NewMonitorFunc func(opts Options) (monitor Monitor, err error)

type NewStorageFunc

type NewStorageFunc func(opts Options) (storage Storage, err error)

type OnValueChangedEvent

type OnValueChangedEvent struct {
	Namespace   string
	Key         string
	BeforeValue interface{}
	AfterValue  interface{}
	UpdateTime  time.Time
}

type OnValueChangedSubscriber

type OnValueChangedSubscriber func(event OnValueChangedEvent)

type OnWatchingError

type OnWatchingError func(namespace string, err error)

type Options

type Options map[string]interface{}

func (Options) Get

func (p Options) Get(name string, v interface{}) (exist bool)

func (Options) ToObject

func (p Options) ToObject(v interface{}) (err error)

type RedConf

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

func New

func New(namespace string, storage Storage, monitor Monitor) (redConf *RedConf, err error)

func (*RedConf) Keys

func (p *RedConf) Keys() []string

func (*RedConf) Namespace

func (p *RedConf) Namespace() string

func (*RedConf) Subscribe

func (p *RedConf) Subscribe(subscribers ...OnValueChangedSubscriber)

func (*RedConf) Watch

func (p *RedConf) Watch(vals ...interface{}) (err error)

func (*RedConf) WatchWithConfig

func (p *RedConf) WatchWithConfig(configs ...*WatchingConfig) (err error)

type RedisMonitor

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

func (*RedisMonitor) Watch

func (p *RedisMonitor) Watch(namespace string, callback KeyContentChangedCallback, onError OnWatchingError) (err error)

type RedisStorage

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

func (*RedisStorage) Get

func (p *RedisStorage) Get(namespace, key string) (ret interface{}, err error)

func (*RedisStorage) Set

func (p *RedisStorage) Set(namespace, key string, val interface{}) (err error)

type Storage

type Storage interface {
	Set(namespace, key string, val interface{}) (err error)
	Get(namespace, key string) (ret interface{}, err error)
}

func CreateStorage

func CreateStorage(driverName string, opts Options) (storage Storage, err error)

func NewRedisStorage

func NewRedisStorage(opts Options) (storage Storage, err error)

type WatchingConfig

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

func NewWatchingConfig

func NewWatchingConfig(v interface{}, name ...string) (wConf *WatchingConfig, err error)

func (*WatchingConfig) Fields

func (p *WatchingConfig) Fields() []Field

func (*WatchingConfig) Name

func (p *WatchingConfig) Name() string

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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