gocloud.dev: gocloud.dev/runtimevar/blobvar Index | Examples | Files

package blobvar

import "gocloud.dev/runtimevar/blobvar"

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


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.


blobvar exposes the following types for As:

- Snapshot: Not supported.
- Error: error, which can be passed to blob.ErrorAs.


// 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

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



Package Files



const Scheme = "blob"

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

func OpenVariable Uses

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.


// 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 {

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

// We can now read the current value of the variable from v.
snapshot, err := v.Latest(ctx)
if err != nil {
// runtimevar.Snapshot.Value is decoded to a string.


hello world

type Options Uses

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 Uses

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 Uses

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.

Package blobvar imports 13 packages (graph) and is imported by 4 packages. Updated 2019-08-07. Refresh now. Tools for package owners.