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.

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.

Code:

// 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.
// This example watches a JSON variable.
ctx := context.Background()
v, err := runtimevar.OpenVariable(ctx, "blob://myvar.json?decoder=json")
if err != nil {
    log.Fatal(err)
}

snapshot, err := v.Latest(ctx)
_, _ = snapshot, err

Index

Examples

Package Files

blobvar.go

Constants

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.

Code:

// Create a *blob.Bucket.
// Here, we use an in-memory implementation and write a sample
// configuration value.
bucket := memblob.OpenBucket(nil)
defer bucket.Close()
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.OpenVariable(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.Latest(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

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 3 packages. Updated 2019-05-16. Refresh now. Tools for package owners.