de34f259ba
fixing test failed issue change back error type change registry.ErrNotFound back to selector.ErrNotFound change back error type change registry.ErrNotFound back to selector.ErrNotFound remove the single node tunnel test Fix read yaml config from memory package main import ( "fmt" "github.com/micro/go-micro/config" "github.com/micro/go-micro/config/source/memory" ) var configData = []byte(` --- a: 1234 `) func main() { memorySource := memory.NewSource( memory.WithYAML(configData), ) // Create new config conf := config.NewConfig() // Load file source conf.Load(memorySource) fmt.Println(string(conf.Bytes())) }
95 lines
1.6 KiB
Go
95 lines
1.6 KiB
Go
// Package memory is a memory source
|
|
package memory
|
|
|
|
import (
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/micro/go-micro/config/source"
|
|
)
|
|
|
|
type memory struct {
|
|
sync.RWMutex
|
|
ChangeSet *source.ChangeSet
|
|
Watchers map[string]*watcher
|
|
}
|
|
|
|
func (s *memory) Read() (*source.ChangeSet, error) {
|
|
s.RLock()
|
|
cs := &source.ChangeSet{
|
|
Format: s.ChangeSet.Format,
|
|
Timestamp: s.ChangeSet.Timestamp,
|
|
Data: s.ChangeSet.Data,
|
|
Checksum: s.ChangeSet.Checksum,
|
|
Source: s.ChangeSet.Source,
|
|
}
|
|
s.RUnlock()
|
|
return cs, nil
|
|
}
|
|
|
|
func (s *memory) Watch() (source.Watcher, error) {
|
|
w := &watcher{
|
|
Id: uuid.New().String(),
|
|
Updates: make(chan *source.ChangeSet, 100),
|
|
Source: s,
|
|
}
|
|
|
|
s.Lock()
|
|
s.Watchers[w.Id] = w
|
|
s.Unlock()
|
|
return w, nil
|
|
}
|
|
|
|
// Update allows manual updates of the config data.
|
|
func (s *memory) Update(c *source.ChangeSet) {
|
|
// don't process nil
|
|
if c == nil {
|
|
return
|
|
}
|
|
|
|
// hash the file
|
|
s.Lock()
|
|
// update changeset
|
|
s.ChangeSet = &source.ChangeSet{
|
|
Data: c.Data,
|
|
Format: c.Format,
|
|
Source: "memory",
|
|
Timestamp: time.Now(),
|
|
}
|
|
s.ChangeSet.Checksum = s.ChangeSet.Sum()
|
|
|
|
// update watchers
|
|
for _, w := range s.Watchers {
|
|
select {
|
|
case w.Updates <- s.ChangeSet:
|
|
default:
|
|
}
|
|
}
|
|
s.Unlock()
|
|
}
|
|
|
|
func (s *memory) String() string {
|
|
return "memory"
|
|
}
|
|
|
|
func NewSource(opts ...source.Option) source.Source {
|
|
var options source.Options
|
|
for _, o := range opts {
|
|
o(&options)
|
|
}
|
|
|
|
s := &memory{
|
|
Watchers: make(map[string]*watcher),
|
|
}
|
|
|
|
if options.Context != nil {
|
|
c, ok := options.Context.Value(changeSetKey{}).(*source.ChangeSet)
|
|
if ok {
|
|
s.Update(c)
|
|
}
|
|
}
|
|
|
|
return s
|
|
}
|