🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

github.com/haesungseo/goconsulkvcache

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/haesungseo/goconsulkvcache

v0.0.0-20220713021523-1a9e3576e13a
Source
Go
Version published
Created
Source

goConsulKVCache

golang Consul KV cache library

import in your project

import (
    "github.com/HaesungSeo/goConsulKVCache"
)

example code

simple

package main

import (
    "flag"
    "fmt"
    "reflect"
    "time"

    goConsulKVCache "github.com/HaesungSeo/goConsulKVCache"
)

func isMapSame(src, dst map[string]string) bool {
    return reflect.DeepEqual(src, dst)
}

func diff(src, dst map[string]string) map[string]string {
    differ := make(map[string]string)
    for k, val1 := range src {
        val2, ok := dst[k]
        if !ok {
            differ[k] = fmt.Sprintf("[%s] removed", k)
        }
        if val1 != val2 {
            differ[k] = fmt.Sprintf("[%s] [%s] -->[%s]", k, val1, val2)
        }
    }
    for k, val2 := range dst {
        _, ok := src[k]
        if !ok {
            differ[k] = fmt.Sprintf("[%s] New [%s]", k, val2)
        }
        // maybe already compared
    }
    return differ
}

// Watch example
func main() {
    addr := flag.String("s", ":8080", "consul server address")
    keyType := flag.String("t", "keyprefix", "consul watch type, one of key keyprefix")
    key := flag.String("k", "/", "consul kv key")

    flag.Parse()

    cfg, err := goConsulKVCache.NewCache(*addr, *keyType, *key)
    if err != nil {
        fmt.Printf("ERROR: %s\n", err)
        return
    }

    old := cfg.KVCopy()
    fmt.Printf("%s\n", old)
    for {
        select {
        case <-time.After(1 * time.Second):
            cur := cfg.KVCopy()
            if !isMapSame(cur, old) {
                diff := diff(old, cur)
                for _, v := range diff {
                    fmt.Printf("%s\n", v)
                }
            }
            old = cur
        }
    }
}

with two terminal, Run binary simple at first terminal,
then Run consul cli at the other one. watch the result

first terminal

./simple -k foo -t key
map[]
[foo] New [hello]
[foo] [hello] -->[world]
[foo] [world] -->[and goodbye]
[foo] [and goodbye] -->[]
[foo] New [again]

second terminal

$ consul kv put foo hello
Success! Data written to: foo
$ consul kv put foo world
Success! Data written to: foo
$ consul kv put foo "and goodbye"
Success! Data written to: foo
$ consul kv delete foo
Success! Deleted key: foo
$ consul kv put foo again
Success! Data written to: foo
$

FAQs

Package last updated on 13 Jul 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts