config4live

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2022 License: Apache-2.0 Imports: 9 Imported by: 0

README

Build Status codecov

Config4live-go

Centralized live configuration library for Go. for microservice or distributed system. Inspired from https://github.com/cfg4j/cfg4j

Features

  • gRPC connection
    • Wrapped by grpc protocol (fast and high performance RPC framework) for requesting configuration to config server.
  • Hystrix
    • Bundled with hystrix for circuit breaker. avoid cascading failures
  • In-Memory cache
    • Avoid too many requests to config server
    • go-cache cache
  • HTTP connection
    • will implement later.

gRPC proto file format

syntax = "proto3";

option java_multiple_files = true;
option java_package = "com.sayurbox.config4live";
option java_outer_classname = "LiveConfigurationProto";
option objc_class_prefix = "HLW";

package config4live;

service LiveConfiguration {
  // Find config by name
  rpc FindConfig (ConfigRequest) returns (ConfigResponse) {}
}

message ConfigRequest {
  string name = 1;
}

message ConfigResponse {
  string id = 1;
  string name = 2;
  string value = 3;
  string description = 4;
  enum Format {
      text = 0;
      number = 1;
      bool = 2;
      json = 3;
    }
  Format format = 5;
  string owner = 6;
}

Installation

go get github.com/sayurbox/config4live-go

Example

Create source (grpc url is required, hystrx config is optional) instance and provider instance

import (
	"github.com/sayurbox/config4live-go"
	grpc "github.com/sayurbox/config4live-go/grpc"
)

type Person struct {
  Name    string
	Id      int
	Address Address
}

type Address struct {
	Postal      int
	Coordinates []float64
}

type List []string

func main() {
  source := grpc.NewGrpcSource(
		grpc.WithURL("localhost:50051"),
		grpc.WithHystrixTimeout(1000),
		grpc.WithHystrixErrorPercentThreshold(25),
		grpc.WithHystrixSleepWindow(500),
		grpc.WithHystrixRequestVolumeThreshold(10),
		grpc.WithHystrixMaxConcurrentRequests(10),
		grpc.WithHystrixCommandName("find-config-key"))
  provider := config4live.NewProvider(
      config4live.WithSource(source),
      config4live.WithCache(true),
      config4live.WithExpiration(5*time.Second))

  // find configuration with default value
  value := provider.BindString("test-name", "default_name")
  value := provider.BindBool("test-bool", true)
  value := provider.BindInt64("test-int", 123)
  value := provider.BindFloat64("test-float", 1.23)

  // struct type
  person := Person{
		Id:   2,
		Name: "Smith",
		Address: Address{
			Postal:      67890,
			Coordinates: []float64{0.88930, 0.32188},
		},
	}
  value := provider.BindAny("test-struct", person).(Person)

  // slice type
  list := List{}
  value := provider.BindAny("test-list", list).(List)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	ID          string
	Name        string
	Value       string
	Description string
	Owner       string
}

Config configuration property

type GetConfigResponse

type GetConfigResponse struct {
	Output *Config
	Error  error
}

GetConfigResponse response wrapper Config and error

type HystrixParams

type HystrixParams struct {
	Name                   string
	Timeout                int
	SleepWindow            int
	RequestVolumeThreshold int
	ErrorPercentThreshold  int
	MaxConcurrentRequests  int
}

HystrixParams global configuration for hystrix

func (*HystrixParams) HystrixConfig

func (h *HystrixParams) HystrixConfig() hystrix.CommandConfig

HystrixConfig create command config

type Option

type Option func(p *providerImpl)

Option the provider options

func WithCache

func WithCache(useCache bool) Option

WithCache use inmemory cache

func WithExpiration

func WithExpiration(ttl time.Duration) Option

WithExpiration cache expiration

func WithSource

func WithSource(source Source) Option

WithSource set provider source

type Provider

type Provider interface {
	BindString(key string, defaultValue string) string
	BindBool(key string, defaultValue bool) bool
	BindInt64(key string, defaultValue int64) int64
	BindFloat64(key string, defaultValue float64) float64
	BindAny(key string, defaultValue any) any
}

Provider wrapper to read configuration value

func NewProvider

func NewProvider(opts ...Option) Provider

NewProvider create new configuration provider

type Source

type Source interface {
	Get(key string) (*Config, error)
}

Source base implementation get config by key

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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