Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ConfigDN Client for Go

The ConfigDN Go client downloads configuration and feature-flag values from a ConfigDN server and keeps them in a local in-memory cache. Values can be read locally with no network request, or through Get, which refreshes the cache when it is stale.

ConfigDN can be used through the hosted service or with a self-hosted server.

Requirements

  • Go 1.20 or newer
  • A ConfigDN authorization key

Installation

go get github.com/dBuidl/ConfigDN-client-go

Quick Start

package main

import (
	"fmt"
	"log"

	"github.com/dBuidl/ConfigDN-client-go"
)

func main() {
	client, err := configdn.NewConfigDN("YOUR_CONFIGDN_AUTH_KEY")
	if err != nil {
		log.Fatal(err)
	}

	// Get refreshes the cache if it has not been loaded or is stale.
	enabled := client.Get("new-checkout")
	title := client.Get("checkout-title")

	fmt.Printf("enabled=%v title=%v\n", enabled, title)
}

Get returns the stored value as any. A missing key returns nil. The client does not know the expected type of a setting, so applications should type-assert values at their boundary:

if enabled, ok := client.Get("new-checkout").(bool); ok && enabled {
	// Use the new checkout flow.
}

Configuration

NewConfigDN uses https://cdn.configdn.com/ and a 60-second refresh interval. Use NewCustomConfigDN for a self-hosted server or different interval:

client, err := configdn.NewCustomConfigDN(
	"YOUR_CONFIGDN_AUTH_KEY",
	"https://your-configdn-server.example/",
	30,
)
if err != nil {
	log.Fatal(err)
}

The endpoint must use HTTP or HTTPS. The endpoint's trailing slash is optional and is normalized by the client. refreshInterval is measured in seconds and must be positive.

Reading Values

Method Behavior
Get(key) Refreshes when needed, then reads from the local cache.
GetLocal(key) Reads only from the local cache and never makes a request.
RefreshConfig(true) Forces a refresh and returns ErrRefreshConfig on failure.
RefreshConfig(false) Forces a refresh but retains the current cache when it fails.
ChangeRefreshInterval(seconds) Changes the interval used by future Get calls.

The client has no background refresh goroutine. Call RefreshConfig from a scheduled job if the application needs proactive refreshes. Failed non-fatal refreshes leave the last successfully downloaded configuration in place.

Refresh Errors

Configuration download failures include invalid endpoints, transport errors, non-2xx HTTP responses, invalid JSON, and unsuccessful ConfigDN responses. Use errors.Is when checking sentinel errors:

if err := client.RefreshConfig(true); err != nil {
	if errors.Is(err, configdn.ErrRefreshConfig) {
		// The cache could not be refreshed.
	}
}

Invalid constructor settings return ErrInvalidEndpoint or ErrInvalidRefreshInterval.

API Response

The client expects the standard ConfigDN response shape:

{
  "s": true,
  "d": {
    "new-checkout": {"v": true},
    "checkout-title": {"v": "Checkout"}
  }
}

Values are decoded using Go's standard JSON rules. JSON numbers are returned as float64 when read through any.

Development

Run the formatter and test suite from the repository root:

gofmt -w *.go
go test ./...
go vet ./...

License

This project is available under the MIT License.

About

The ConfigDN Go client downloads configuration and feature-flag values from a ConfigDN server and keeps them in a local in-memory cache. Values can be read locally with no network request, or through Get, which refreshes the cache when it is stale.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages