File size: 3,509 Bytes
c8f6dca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//                           _       _
// __      _____  __ ___   ___  __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
//  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
//   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
//  Copyright © 2016 - 2025 Weaviate B.V. All rights reserved.
//
//  CONTACT: hello@weaviate.io
//

package runtime

import (
	"encoding/json"
	"fmt"
	"sync"
	"time"

	"gopkg.in/yaml.v3"
)

// DynamicType represents different types that is supported in runtime configs
type DynamicType interface {
	~int | ~float64 | ~bool | time.Duration | ~string | []string
}

// DynamicValue represents any runtime config value. It's zero value is
// fully usable.
// If you want zero value with different `default`, use `NewDynamicValue` constructor.
type DynamicValue[T DynamicType] struct {
	// val is the dynamically changing value.
	val *T
	// mu protects val
	mu sync.RWMutex

	// def represents the default value.
	def T
}

// NewDynamicValue returns an instance of DynamicValue as passed in type
// with passed in value as default.
func NewDynamicValue[T DynamicType](val T) *DynamicValue[T] {
	return &DynamicValue[T]{
		def: val,
	}
}

// Get returns a current value for the given config. It can either be dynamic value or default
// value (if unable to get dynamic value)
// Consumer of the dynamic config value should care only about this `Get()` api.
func (dv *DynamicValue[T]) Get() T {
	// Handle zero-value of `*DynamicValue[T]` without panic.
	if dv == nil {
		var zero T
		return zero
	}

	dv.mu.RLock()
	defer dv.mu.RUnlock()

	if dv.val != nil {
		return *dv.val
	}
	return dv.def
}

// Reset removes the old dynamic value.
func (dv *DynamicValue[T]) Reset() {
	if dv == nil {
		return
	}

	dv.mu.Lock()
	defer dv.mu.Unlock()

	dv.val = nil
}

// Set is used by the config manager to update the dynamic value.
func (dv *DynamicValue[T]) SetValue(val T) {
	// log this at the high level, that someone is trying to set unitilized runtime value
	if dv == nil {
		return
	}

	dv.mu.Lock()
	defer dv.mu.Unlock()

	dv.val = &val

	// NOTE: doesn't need to set any default value here
	// as `Get()` api will return default if dynamic value is not set.
}

// UnmarshalYAML implements `yaml.v3` custom decoding for `DynamicValue` type.
func (dv *DynamicValue[T]) UnmarshalYAML(node *yaml.Node) error {
	var val T
	if err := node.Decode(&val); err != nil {
		return err
	}
	dv.mu.Lock()
	defer dv.mu.Unlock()

	dv.def = val
	return nil
}

// MarshalYAML implements `yaml.v3` custom encoding for `DynamicValue` type.
func (dv *DynamicValue[T]) MarshalYAML() (any, error) {
	dv.mu.Lock()
	val := dv.def
	if dv.val != nil {
		val = *dv.val
	}
	dv.mu.Unlock()

	return val, nil
}

// UnmarshalJSON implements `json` custom decoding for `DynamicValue` type.
func (dv *DynamicValue[T]) UnmarshalJSON(data []byte) error {
	var val T
	if err := json.Unmarshal(data, &val); err != nil {
		return err
	}
	dv.mu.Lock()
	defer dv.mu.Unlock()
	dv.def = val
	return nil
}

// MarshalJSON implements `json` custom encoding for `DynamicValue` type.
func (dv *DynamicValue[T]) MarshalJSON() ([]byte, error) {
	dv.mu.Lock()
	val := dv.def
	if dv.val != nil {
		val = *dv.val
	}
	dv.mu.Unlock()

	b, err := json.Marshal(val)
	if err != nil {
		return nil, err
	}

	return b, nil
}

// String implements Stringer interface for `%v` formatting
// for any dynamic value types.
func (dv *DynamicValue[T]) String() string {
	res := dv.val
	if res == nil {
		res = &dv.def
	}
	return fmt.Sprintf("%v", *res)
}