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://github.com/eliben/gocdkx/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 ¶
package main import ( "context" "fmt" "log" "github.com/eliben/gocdkx/blob/memblob" "github.com/eliben/gocdkx/runtimevar" "github.com/eliben/gocdkx/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) 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
Example (OpenVariable) ¶
package main import ( "context" "log" "github.com/eliben/gocdkx/runtimevar" ) func main() { // 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 }
Output:
Index ¶
Examples ¶
Constants ¶
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.
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 ¶
OpenVariableURL opens the variable at the URL's path. See the package doc for more details.