Documentation ¶
Overview ¶
Package runtimeconfigurator provides a runtimevar driver implementation to read configurations from Cloud Runtime Configurator service and ability to detect changes and get updates.
User constructs a Client that provides the gRPC connection, then use the client to construct any number of runtimevar.Variable objects using NewConfig method.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var Set = wire.NewSet( Dial, NewClient, )
Set is a Wire provider set that provides *Client using a default connection to the Runtime Configurator API given a GCP token source.
Functions ¶
func Dial ¶
func Dial(ctx context.Context, ts gcp.TokenSource) (pb.RuntimeConfigManagerClient, func(), error)
Dial opens a gRPC connection to the Runtime Configurator API.
The second return value is a function that can be called to clean up the connection opened by Dial.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
A Client constructs runtime variables using the Runtime Configurator API.
func NewClient ¶
func NewClient(stub pb.RuntimeConfigManagerClient) *Client
NewClient returns a new client that makes calls to the given gRPC stub.
Example ¶
package main import ( "context" "log" "github.com/Lioric/go-cloud/gcp" "github.com/Lioric/go-cloud/runtimevar/runtimeconfigurator" ) func main() { ctx := context.Background() creds, err := gcp.DefaultCredentials(ctx) if err != nil { log.Fatal(err) } stub, cleanup, err := runtimeconfigurator.Dial(ctx, creds.TokenSource) if err != nil { log.Fatal(err) } defer cleanup() client := runtimeconfigurator.NewClient(stub) // Now use the client. _ = client }
Output:
func (*Client) NewVariable ¶
func (c *Client) NewVariable(ctx context.Context, name ResourceName, decoder *runtimevar.Decoder, opts *WatchOptions) (*runtimevar.Variable, error)
NewVariable constructs a runtimevar.Variable object with this package as the driver implementation. Provide a decoder to unmarshal updated configurations into similar objects during the Watch call. If WaitTime is not set the poller will check for updates to the variable every 30 seconds.
Example ¶
package main import ( "context" "log" "github.com/Lioric/go-cloud/runtimevar" "github.com/Lioric/go-cloud/runtimevar/runtimeconfigurator" ) func main() { // Assume client was created at server startup. ctx := context.Background() client := openClient() // MyAppConfig is an example unmarshaled type for configuration stored in key // "projects/projectID/configs/configName/variables/appConfig". Runtime // Configurator stores individual variables as strings or binary data, so a // decoder automatically parses the data. type MyAppConfig struct { MsgOfTheDay string `json:"msg_of_the_day"` } decoder := runtimevar.NewDecoder(&MyAppConfig{}, runtimevar.JSONDecode) // Fill these in with the values from the Cloud Console. name := runtimeconfigurator.ResourceName{ ProjectID: "projectID", Config: "configName", Variable: "appConfig", } // Create a variable object to watch for changes. v, err := client.NewVariable(ctx, name, decoder, nil) if err != nil { log.Fatal(err) } defer v.Close() // Read the current value. Calling Watch() again will block until the value // changes. snapshot, err := v.Watch(ctx) if err != nil { log.Fatal(err) } // Value will always be of the decoder's type. cfg := snapshot.Value.(*MyAppConfig) log.Println(cfg.MsgOfTheDay) } func openClient() *runtimeconfigurator.Client { return nil }
Output:
type ResourceName ¶
ResourceName identifies the full configuration variable path used by the service.
func (ResourceName) String ¶
func (r ResourceName) String() string
String returns the full configuration variable path.
type WatchOptions ¶
type WatchOptions struct { // WaitTime controls the frequency of RPC calls and checking for updates by the Watch method. // A Watcher keeps track of the last time it made an RPC, when Watch is called, it waits for // configured WaitTime from the last RPC before making another RPC. The smaller the value, the // higher the frequency of making RPCs, which also means faster rate of hitting the API quota. // // If this option is not set or set to 0, it uses defaultWait value. WaitTime time.Duration }
WatchOptions provide optional configurations to the Watcher.