Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fregie/etcdwatcher
etcdwatcher is a simple library to watch etcd changes.Usually used to monitor configuration items.
https://github.com/fregie/etcdwatcher
configuration etcd golang-package
Last synced: 29 days ago
JSON representation
etcdwatcher is a simple library to watch etcd changes.Usually used to monitor configuration items.
- Host: GitHub
- URL: https://github.com/fregie/etcdwatcher
- Owner: fregie
- Created: 2021-11-25T07:45:11.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2022-10-17T07:47:04.000Z (about 2 years ago)
- Last Synced: 2024-10-18T15:39:41.935Z (3 months ago)
- Topics: configuration, etcd, golang-package
- Language: Go
- Homepage:
- Size: 25.4 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# etcdwatcher
[![Build and test](https://github.com/fregie/etcdwatcher/actions/workflows/test.yml/badge.svg)](https://github.com/fregie/etcdwatcher/actions/workflows/test.yml)
etcdwatcher is a simple library to watch etcd changes.Usually used to monitor configuration items.
Just define the key to watch and the value type,it will update the value.## Installation
```bash
go get github.com/fregie/etcdwatcher
```## usage
```go
import "github.com/fregie/etcdwatcher"
```
watch a simple configuration:
```go
// Create a new watcher
etcdEndpoint := []string{"http://localhost:2379"}
configWatcher, err := etcdwatcher.NewWatcher(etcdEndpoint)
if err != nil {
panic()
}
// Define the key to watch
var (
int32Value *etcdwatcher.Int32 = etcdwatcher.NewInt32("/etcdwatcher/test/int32Value", 0)
stringValue *etcdwatcher.String = etcdwatcher.NewString("/etcdwatcher/test/int32String", "default")
durationValue *etcdwatcher.Duration = etcdwatcher.NewDuration("/etcdwatcher/test/durationValue", time.Second)
boolValue *etcdwatcher.Bool = etcdwatcher.NewBool("/etcdwatcher/test/boolValue", false)
)// Watch the key
err = configWatcher.WatchItems([]etcdwatcher.Item{
int32Value,
stringValue,
durationValue,
boolValue,
})
if err != nil {
panic()
}// Use
fmt.Println(int32Value.Value())
fmt.Println(stringValue.Value())
fmt.Println(durationValue.Value())
fmt.Println(boolValue.Value())
```## custom your watch item
etcdwatcher watch a item implement the Item interface.
You can implement your own Item to watch your custom item.
```go
type Item interface {
// Return the key watching
Key() string
// Parse the data when the value is changed
Parse([]byte) error
// Set the default value
SetDefault()
}
```
### example
define a `map[string]string` Item to watch
```go
type customMapItem struct {
sync.Map
key string
}func NewcustomMapItem(key string) *customMapItem {
return &customMapItem{
key: key,
}
}func (c *customMapItem) SetDefault() {
c.Range(func(key, value interface{}) bool {
c.Delete(key)
return true
})
}func (c *customMapItem) Parse(data []byte) error {
dataMap := make(map[string]string)
err := json.Unmarshal(data, &dataMap)
if err != nil {
return err
}
for k, v := range dataMap {
c.Store(k, v)
}
return nil
}func (c *customMapItem) Key() string {
return c.key
}func (c *customMapItem) SaveToEtcd(ctx context.Context, cli *clientv3.Client, dataMap map[string]string) error {
data, err := json.Marshal(dataMap)
if err != nil {
return err
}
_, err := cli.Put(ctx, m.Key(), string(data))
return err
}
```