rcc

package module
v0.0.0-...-a3ed905 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2022 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Example (Rcc)

Example_RCC example code to show how to use by default rcc client

/*
@Time : 2021-06-11
@Author : xiemalin
*/

package main

import (
	"context"
	"fmt"
)

// DemoSet demo set
type DemoSet struct {
	data map[string]string
}

// Update callback function while regitered in Watch method.
func (d *DemoSet) Update(ce *ChangeEvent) {

	// 建议defer捕获协程panic
	defer func() {
		if r := recover(); r != nil {
			fmt.Println("watch update callback panic")
		}
	}()

	for key, change := range ce.Changes {
		if change.ChangeType == ADD || change.ChangeType == MODIFY {
			d.data[key] = change.NewValue
		}
	}
}

// Example_RCC example code to show how to use by default rcc client
func main() {

	conf, err := NewConf(TestConfFile)
	if err != nil {
		fmt.Printf("new conf fail, got :%v", err)
		return
	}

	err = StartWithConf(context.Background(), conf)
	if err != nil {
		fmt.Printf("start default cient fail, got :%v", err)
		return
	}

	a1 := GetValue("a1", "222")
	fmt.Println("get value", a1)

	keys := GetAllKeys()
	for _, key := range keys {
		fmt.Println("print out key", key)
	}

	d := DemoSet{data: map[string]string{}}
	d.data["a1"] = "a1"
	d.data["a2"] = "a2"

	// 监听变更
	Watch(d.Update)

	if err := Stop(); err != nil {
		fmt.Printf("Stop should return nil, got :%v", err)
		return
	}
}
Output:

Index

Examples

Constants

View Source
const (
	DefaultCallbackIntervalInt = 300 // 默认300s触发一次更新拉取
	DefaultRequestTimeoutInt   = 5   // 默认5秒钟接口调用超时时间
	DefaultFilePath            = "/tmp/rcc"
)

Variables

This section is empty.

Functions

func Bind

func Bind(s interface{}) error

Bind

func GetAllKeys

func GetAllKeys() []string

GetAllKeys return all config keys in given namespace

func GetValue

func GetValue(key, defaultValue string) string

GetValue from default namespace

func StartWithConf

func StartWithConf(ctx context.Context, conf *Conf) error

StartWithConf run rcc-client with Conf

func StartWithConfFile

func StartWithConfFile(ctx context.Context, name string) error

StartWithConfFile run rcc-client with conf file

func Stop

func Stop() error

Stop sync config

func Watch

func Watch(callback func(ce *ChangeEvent))

Watch

Types

type ApiResponse

type ApiResponse struct {
	Status int         `json:"status"`
	Msg    string      `json:"msg"`
	Data   interface{} `json:"data"`
}

type Cache

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

配置kv内存结构

type Change

type Change struct {
	OldValue   string
	NewValue   string
	ChangeType ChangeType
}

Change represent a single key change

type ChangeEvent

type ChangeEvent struct {
	Changes map[string]*Change
}

ChangeEvent change event

type ChangeType

type ChangeType int

ChangeType for a key

const (
	// ADD a new value
	ADD ChangeType = iota
	// MODIFY a old value
	MODIFY
	// DELETE ...
	DELETE
)

func (ChangeType) String

func (c ChangeType) String() string

String

type Client

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

Client for rcc

Example

ExampleNewConf the example to show use client to connect server

client, err := NewClientWithConfFile(TestConfFile)
if err != nil {
	return
}

err = client.Start()
if err != nil {
	return
}
defer client.Stop()

keys := client.GetAllKeys()
for _, v := range keys {
	client.GetValue(v, "DefaultValue")
}

s := struct {
	Key string `rcc:"mykey"`
}{}
client.Bind(&s)
Output:

func NewClient

func NewClient(ctx context.Context, conf *Conf) (*Client, error)

NewClient create client from conf

func NewClientWithConf

func NewClientWithConf(conf *Conf) (*Client, error)

NewClientWithConf create client from conf

func NewClientWithConfFile

func NewClientWithConfFile(confFile string) (*Client, error)

NewClientWithConfFile create client from conf file

func (*Client) Bind

func (c *Client) Bind(s interface{}) error

Bind

func (*Client) GetAllKeys

func (c *Client) GetAllKeys() []string

GetAllKeys return all config keys in given namespace

func (*Client) GetValue

func (c *Client) GetValue(key, defaultValue string) string

GetValue

func (*Client) Start

func (c *Client) Start() error

Start sync config

func (*Client) Stop

func (c *Client) Stop() error

Stop sync config

func (*Client) Watch

func (c *Client) Watch(callback func(ce *ChangeEvent))

Watch

Example

ExampleClient_Watch he example to show use Watch function

client, err := NewClientWithConfFile(TestConfFile)
if err != nil {
	return
}

err = client.Start()
if err != nil {
	return
}
defer client.Stop()

// define update callback for Watch function
callback := func(ce *ChangeEvent) {
	// 建议defer捕获协程panic
	defer func() {
		if r := recover(); r != nil {
			fmt.Println("watch update callback panic")
		}
	}()

	for key, change := range ce.Changes {
		if change.ChangeType == ADD || change.ChangeType == MODIFY {
			fmt.Println("changed item", key, change.NewValue, change.OldValue)
		}
	}
}
client.Watch(callback)
Output:

func (*Client) WatchUpdate

func (c *Client) WatchUpdate() <-chan *ChangeEvent

WatchUpdate get all updates

type Conf

type Conf struct {
	ProjectName string `toml:"projectName"`
	EnvName     string `toml:"envName"`
	ServerUrl   string `toml:"serverUrl"`
	ApiPassword string `toml:"apiPassword"`
	VersionName string `toml:"versionName"`

	EnableCallback      bool   `toml:"enableCallback"`
	CallbackIntervalInt int64  `toml:"callbackInterval"`
	RequestTimeoutInt   int64  `toml:"requestTimeout"`
	EnableCache         bool   `toml:"enableCache"`
	CacheDir            string `json:"cacheDir,omitempty"`

	Token string
	EnvId int64

	CallbackInterval time.Duration
	RequestTimeout   time.Duration
}

Conf

func NewConf

func NewConf(name string) (*Conf, error)

NewConf create Conf from file

type IPoller

type IPoller interface {
	// contains filtered or unexported methods
}

Poller fetch conf updates

type IRequester

type IRequester interface {
	Get(url string, respData interface{}) error
	Post(url string, data interface{}, respData interface{}) error
	Delete(url string, respData interface{}) error
}

IRequester

type Item

type Item struct {
	Key   string `json:"key"`
	Value string `json:"value"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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