props

package module
v0.0.0-...-34fd78c Latest Latest
Warning

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

Go to latest
Published: May 11, 2023 License: Apache-2.0 Imports: 11 Imported by: 0

README

Overview

props is a thread-safe, multi-source cache for Java properties in Go.

It wraps around the properties library and provides a read-only concurrent cache that is continusously kept in sync with configured properties sources.

Features

  1. Supports multiple sources:
    1. S3
    2. DynamoDB
    3. Local File
  2. Supports a combination of any of the above with custom precedence order.
  3. Caching and syncing with remote sources.
  4. Go-routine/concurrent/thread-safe global props.

You can also bring your own source by implementing the Poller interface and use it during cache initialization:

type Poller interface {
	Poll(context.Context) (*properties.Properties, error)
}

Feel free to send in a PR for some useful sources!

Example Usage

Configure Sources

source1 := props.NewFileSource("path_to_local_props")

source2 := props.NewS3Source(*cfg, "props_bucket", "props_file_path")

source3 := props.NewDynamoDBGetterSource(*cfg, props.DynamoDBGetterArgs{
  Table:     "PropsTable",
  KeyCol:    "Key",
  ValCol:    "Value",
  WatchKeys: []string{"prop_key_1", "prop_key_2"},
})

Initialize and Start Cache

cache := props.Cache{
  Store:           props.GetProperties(),
  Source:          props.NewCompositeSource(source1, source2, source3),
  RefreshInterval: time.Minute,
  ExpireAfter:     5 * time.Minute,
}

cache.Start(context.Background().Done())

Read props from anywhere, anytime

valFromLocal := props.GetBool("prop_in_local", false)
valFromS3 := props.GetString("prop_in_s3", "")
valFromDb := props.GetInt("prop_key_1", 42)

log.Println(valFromLocal, valFromS3, valFromDb)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get(key string) (string, bool)

func GetBool

func GetBool(key string, def bool) bool

func GetInt

func GetInt(key string, def int) int

func GetString

func GetString(key, def string) string

func Replace

func Replace(props *properties.Properties)

Types

type Cache

type Cache struct {
	Store           *Properties
	Source          Poller
	RefreshInterval time.Duration
	ExpireAfter     time.Duration
}

func NewAsyncPollerSource

func NewAsyncPollerSource(p *Properties, src Poller, period time.Duration) *Cache

func (*Cache) Poll

func (c *Cache) Poll(ctx context.Context) (*properties.Properties, error)

func (*Cache) Start

func (c *Cache) Start(stopCh <-chan struct{}) error

type Composite

type Composite struct {
	Sources []Poller
}

func NewCompositeSource

func NewCompositeSource(sources ...Poller) *Composite

func (Composite) Poll

type DynamoDBGetter

type DynamoDBGetter struct {
	*dynamodb.Client
	// contains filtered or unexported fields
}

func NewDynamoDBGetterSource

func NewDynamoDBGetterSource(cfg aws.Config, args DynamoDBGetterArgs) *DynamoDBGetter

func (DynamoDBGetter) Poll

type DynamoDBGetterArgs

type DynamoDBGetterArgs struct {
	Table     string
	KeyCol    string
	ValCol    string
	WatchKeys []string
}

type File

type File struct {
	Path string
}

func NewFileSource

func NewFileSource(filename string) *File

func (File) Poll

func (f File) Poll(ctx context.Context) (*properties.Properties, error)

type Poller

type Poller interface {
	Poll(context.Context) (*properties.Properties, error)
}

type Properties

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

func GetProperties

func GetProperties() *Properties

func New

func New() *Properties

func (*Properties) Get

func (p *Properties) Get(key string) (string, bool)

func (*Properties) GetBool

func (p *Properties) GetBool(key string, def bool) bool

func (*Properties) GetInt

func (p *Properties) GetInt(key string, def int) int

func (*Properties) GetString

func (p *Properties) GetString(key, def string) string

func (*Properties) Properties

func (p *Properties) Properties() *properties.Properties

func (*Properties) Replace

func (p *Properties) Replace(props *properties.Properties)

type S3

type S3 struct {
	*s3.Client
	// contains filtered or unexported fields
}

func NewS3Source

func NewS3Source(cfg aws.Config, bucket, key string) *S3

func (S3) Poll

func (s S3) Poll(ctx context.Context) (*properties.Properties, error)

Directories

Path Synopsis
pkg

Jump to

Keyboard shortcuts

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