blobvar

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2019 License: Apache-2.0 Imports: 7 Imported by: 2

Documentation

Overview

Package blobvar provides a runtimevar implementation with variables read from a blob.Bucket. Use NewVariable to construct a *runtimevar.Variable.

As

blobvar exposes the following types for As:

  • Snapshot: Not supported.
  • Error: error, which can be passed to blob.ErrorAs.
Example
package main

import (
	"context"
	"fmt"
	"log"

	"gocloud.dev/blob/memblob"
	"gocloud.dev/runtimevar"
	"gocloud.dev/runtimevar/blobvar"
)

// MyConfig is a sample configuration struct.
type MyConfig struct {
	Server string
	Port   int
}

func main() {
	// Create a *blob.Bucket.
	// Here, we use an in-memory implementation and write a sample
	// configuration value.
	bucket := memblob.OpenBucket(nil)
	ctx := context.Background()
	err := bucket.WriteAll(ctx, "cfg-variable-name", []byte(`{"Server": "foo.com", "Port": 80}`), nil)
	if err != nil {
		log.Fatal(err)
	}

	// Create a decoder for decoding JSON strings into MyConfig.
	decoder := runtimevar.NewDecoder(MyConfig{}, runtimevar.JSONDecode)

	// Construct a *runtimevar.Variable that watches the blob.
	v, err := blobvar.NewVariable(bucket, "cfg-variable-name", decoder, nil)
	if err != nil {
		log.Fatal(err)
	}
	defer v.Close()

	// We can now read the current value of the variable from v.
	snapshot, err := v.Watch(ctx)
	if err != nil {
		log.Fatal(err)
	}
	// runtimevar.Snapshot.Value is decoded to type MyConfig.
	cfg := snapshot.Value.(MyConfig)
	fmt.Printf("%s running on port %d", cfg.Server, cfg.Port)

}
Output:

foo.com running on port 80

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewVariable

func NewVariable(bucket *blob.Bucket, key string, decoder *runtimevar.Decoder, opts *Options) (*runtimevar.Variable, error)

NewVariable constructs a *runtimevar.Variable backed by the referenced blob. Reads of the blob return raw bytes; provide a decoder to decode the raw bytes into the appropriate type for runtimevar.Snapshot.Value. See the runtimevar package documentation for examples of decoders.

Types

type Options

type Options struct {
	// WaitDuration controls the rate at which the blob is polled.
	// Defaults to 30 seconds.
	WaitDuration time.Duration
}

Options sets options.

Jump to

Keyboard shortcuts

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