package blobvar
import "gocloud.dev/runtimevar/blobvar"
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¶
Code:play
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 ¶
- func NewVariable(bucket *blob.Bucket, key string, decoder *runtimevar.Decoder, opts *Options) (*runtimevar.Variable, error)
- type Options
Examples ¶
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.
Source Files ¶
blobvar.go
- Version
- v0.11.0
- Published
- Feb 28, 2019
- Platform
- js/wasm
- Imports
- 7 packages
- Last checked
- 27 minutes ago –
Tools for package owners.