Documentation

Overview

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

URLs

For runtimevar.OpenVariable, blobvar registers for the scheme "blob". The default URL opener will open a blob.Bucket based on the environment variable "BLOBVAR_BUCKET_URL". To customize the URL opener, or for more details on the URL format, see URLOpener. See https://gocloud.dev/concepts/urls/ for background information.

As

blobvar exposes the following types for As:

- Snapshot: Not supported.
- Error: error, which can be passed to blob.ErrorAs.
Example (OpenVariableFromURL)
package main

import (
	"context"
	"log"

	"gocloud.dev/runtimevar"
)

func main() {
	// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
	// PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/runtimevar/blobvar"
	// PRAGMA: On gocloud.dev, hide lines until the next blank line.
	ctx := context.Background()

	// runtimevar.OpenVariable creates a *runtimevar.Variable from a URL.
	// The default opener opens a blob.Bucket via a URL, based on the environment
	// variable BLOBVAR_BUCKET_URL.

	v, err := runtimevar.OpenVariable(ctx, "blob://myvar.txt?decoder=string")
	if err != nil {
		log.Fatal(err)
	}
	defer v.Close()
}
Output:

Index

Examples

Constants

View Source
const Scheme = "blob"

Scheme is the URL scheme blobvar registers its URLOpener under on runtimevar.DefaultMux.

Variables

This section is empty.

Functions

func OpenVariable

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

OpenVariable 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.

Example
package main

import (
	"context"
	"fmt"
	"log"

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

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

	// Construct a *runtimevar.Variable that watches the blob.
	v, err := blobvar.OpenVariable(bucket, "cfg-variable-name", runtimevar.StringDecoder, 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.Latest(ctx)
	if err != nil {
		log.Fatal(err)
	}
	// runtimevar.Snapshot.Value is decoded to a string.
	fmt.Println(snapshot.Value.(string))

}
Output:

hello world

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.

type URLOpener

type URLOpener struct {
	// Bucket is required.
	Bucket *blob.Bucket

	// Decoder specifies the decoder to use if one is not specified in the URL.
	// Defaults to runtimevar.BytesDecoder.
	Decoder *runtimevar.Decoder

	// Options specifies the Options for OpenVariable.
	Options Options
}

URLOpener opens blob-backed URLs like "blob://myblobkey?decoder=string". It supports the following URL parameters:

- decoder: The decoder to use. Defaults to URLOpener.Decoder, or
    runtimevar.BytesDecoder if URLOpener.Decoder is nil.
    See runtimevar.DecoderByName for supported values.

func (*URLOpener) OpenVariableURL

func (o *URLOpener) OpenVariableURL(ctx context.Context, u *url.URL) (*runtimevar.Variable, error)

OpenVariableURL opens the variable at the URL's path. See the package doc for more details.

Source Files