| 1 |
// Copyright 2018 The Go Cloud Development Kit Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
// Package driver defines interfaces to be implemented by runtimevar drivers, which |
| 16 |
// will be used by the runtimevar package to interact with the underlying services. |
| 17 |
// Application code should use package runtimevar. |
| 18 |
package driver // import "gocloud.dev/runtimevar/driver" |
| 19 |
|
| 20 |
import ( |
| 21 |
"context" |
| 22 |
"time" |
| 23 |
|
| 24 |
"gocloud.dev/gcerrors" |
| 25 |
) |
| 26 |
|
| 27 |
// DefaultWaitDuration is the default value for WaitDuration. |
| 28 |
const DefaultWaitDuration = 30 * time.Second |
| 29 |
|
| 30 |
// WaitDuration returns DefaultWaitDuration if d is <= 0, otherwise it returns d. |
| 31 |
func WaitDuration(d time.Duration) time.Duration { |
| 32 |
if d <= 0 { |
| 33 |
return DefaultWaitDuration |
| 34 |
} |
| 35 |
return d |
| 36 |
} |
| 37 |
|
| 38 |
// State represents the current state of a variable. |
| 39 |
type State interface { |
| 40 |
// Value returns the current variable value. |
| 41 |
Value() (any, error) |
| 42 |
// UpdateTime returns the update time for the variable. |
| 43 |
UpdateTime() time.Time |
| 44 |
|
| 45 |
// As converts i to driver-specific types. |
| 46 |
// See https://gocloud.dev/concepts/as/ for background information. |
| 47 |
As(any) bool |
| 48 |
} |
| 49 |
|
| 50 |
// Watcher watches for updates on a variable and returns an updated Variable object if |
| 51 |
// there are changes. A Watcher object is associated with a variable upon construction. |
| 52 |
// |
| 53 |
// An application can have more than one Watcher, one for each variable. It is typical |
| 54 |
// to only have one Watcher per variable. |
| 55 |
// |
| 56 |
// Many services store their configuration data as raw bytes; drivers for such |
| 57 |
// services should include a runtimevar.Decoder in their constructor to allow |
| 58 |
// users to decode the raw bytes into a particular format (e.g., parsing a |
| 59 |
// JSON string). |
| 60 |
// |
| 61 |
// Drivers for services that don't have raw bytes may dictate the type of the exposed |
| 62 |
// Snapshot.Value, or expose custom decoding logic. |
| 63 |
type Watcher interface { |
| 64 |
// WatchVariable returns the current State of the variable. |
| 65 |
// If the State has not changed, it returns nil. |
| 66 |
// |
| 67 |
// If WatchVariable returns a wait time > 0, the portable type uses |
| 68 |
// it as a hint to not call WatchVariable again for the wait time. |
| 69 |
// |
| 70 |
// Implementations *may* block, but must return if ctx is Done. If the |
| 71 |
// variable has changed, then implementations *must* eventually return |
| 72 |
// it. |
| 73 |
// |
| 74 |
// A polling implementation should return (State, <poll interval>) for |
| 75 |
// a new State, or (nil, <poll interval>) if State hasn't changed. |
| 76 |
// |
| 77 |
// An implementation that receives notifications from an external source |
| 78 |
// about changes to the underlying variable should: |
| 79 |
// 1. If prev != nil, subscribe to change notifications. |
| 80 |
// 2. Fetch the current State. |
| 81 |
// 3. If prev == nil or if the State has changed, return (State, 0). |
| 82 |
// A non-zero wait should be returned if State holds an error, to avoid |
| 83 |
// spinning. |
| 84 |
// 4. Block until it detects a change or ctx is Done, then fetch and return |
| 85 |
// (State, 0). |
| 86 |
// Note that the subscription in 1 must occur before 2 to avoid race conditions. |
| 87 |
WatchVariable(ctx context.Context, prev State) (state State, wait time.Duration) |
| 88 |
|
| 89 |
// Close cleans up any resources used by the Watcher object. |
| 90 |
Close() error |
| 91 |
|
| 92 |
// ErrorAs allows drivers to expose driver-specific types for returned |
| 93 |
// errors; see State.As for more details. |
| 94 |
ErrorAs(error, any) bool |
| 95 |
|
| 96 |
// ErrorCode should return a code that describes the error, which was returned by |
| 97 |
// one of the other methods in this interface. |
| 98 |
ErrorCode(error) gcerrors.ErrorCode |
| 99 |
} |