config

package
v0.0.0-...-1593278 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 7, 2023 License: Apache-2.0 Imports: 15 Imported by: 1

Documentation

Overview

Licensed to the LF AI & Data foundation under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed to the LF AI & Data foundation under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed to the LF AI & Data foundation under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed to the LF AI & Data foundation under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed to the LF AI & Data foundation under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed to the LF AI & Data foundation under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

View Source
const (
	UpdateType = "UPDATE"
	DeleteType = "DELETE"
	CreateType = "CREATE"
)

Event Constant

View Source
const (
	HighPriority   = 1
	NormalPriority = HighPriority + 10
	LowPriority    = NormalPriority + 10
)
View Source
const (
	ReadConfigTimeout = 3 * time.Second
)
View Source
const (
	TombValue = "TOMB_VAULE"
)

Variables

View Source
var (
	ErrNotInitial   = errors.New("config is not initialized")
	ErrIgnoreChange = errors.New("ignore change")
	ErrKeyNotFound  = errors.New("key not found")
)

Functions

This section is empty.

Types

type EnvSource

type EnvSource struct {
	KeyFormatter func(string) string
	// contains filtered or unexported fields
}

func NewEnvSource

func NewEnvSource(KeyFormatter func(string) string) EnvSource

func (EnvSource) Close

func (es EnvSource) Close()

func (EnvSource) GetConfigurationByKey

func (es EnvSource) GetConfigurationByKey(key string) (string, error)

GetConfigurationByKey implements ConfigSource

func (EnvSource) GetConfigurations

func (es EnvSource) GetConfigurations() (map[string]string, error)

GetConfigurations implements ConfigSource

func (EnvSource) GetPriority

func (es EnvSource) GetPriority() int

GetPriority implements ConfigSource

func (EnvSource) GetSourceName

func (es EnvSource) GetSourceName() string

GetSourceName implements ConfigSource

func (EnvSource) SetEventHandler

func (es EnvSource) SetEventHandler(eh EventHandler)

func (EnvSource) UpdateOptions

func (es EnvSource) UpdateOptions(opts Options)

type EtcdInfo

type EtcdInfo struct {
	UseEmbed   bool
	UseSSL     bool
	Endpoints  []string
	KeyPrefix  string
	CertFile   string
	KeyFile    string
	CaCertFile string
	MinVersion string

	//Pull Configuration interval, unit is second
	RefreshInterval time.Duration
}

EtcdInfo has attribute for config center source initialization

type EtcdSource

type EtcdSource struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewEtcdSource

func NewEtcdSource(etcdInfo *EtcdInfo) (*EtcdSource, error)

func (*EtcdSource) Close

func (es *EtcdSource) Close()

func (*EtcdSource) GetConfigurationByKey

func (es *EtcdSource) GetConfigurationByKey(key string) (string, error)

GetConfigurationByKey implements ConfigSource

func (*EtcdSource) GetConfigurations

func (es *EtcdSource) GetConfigurations() (map[string]string, error)

GetConfigurations implements ConfigSource

func (*EtcdSource) GetPriority

func (es *EtcdSource) GetPriority() int

GetPriority implements ConfigSource

func (*EtcdSource) GetSourceName

func (es *EtcdSource) GetSourceName() string

GetSourceName implements ConfigSource

func (*EtcdSource) SetEventHandler

func (es *EtcdSource) SetEventHandler(eh EventHandler)

func (*EtcdSource) UpdateOptions

func (es *EtcdSource) UpdateOptions(opts Options)

type Event

type Event struct {
	EventSource string
	EventType   string
	Key         string
	Value       string
	HasUpdated  bool
}

func PopulateEvents

func PopulateEvents(source string, currentConfig, updatedConfig map[string]string) ([]*Event, error)

type EventDispatcher

type EventDispatcher struct {
	// contains filtered or unexported fields
}

func NewEventDispatcher

func NewEventDispatcher() *EventDispatcher

func (*EventDispatcher) Dispatch

func (ed *EventDispatcher) Dispatch(event *Event)

func (*EventDispatcher) Get

func (ed *EventDispatcher) Get(key string) []EventHandler

func (*EventDispatcher) Register

func (ed *EventDispatcher) Register(key string, handler EventHandler)

register a handler to watch specific config changed

func (*EventDispatcher) Unregister

func (ed *EventDispatcher) Unregister(key string, handler EventHandler)

type EventHandler

type EventHandler interface {
	OnEvent(event *Event)
	GetIdentifier() string
}

EventHandler handles config change event

func NewHandler

func NewHandler(ident string, onEvent func(*Event)) EventHandler

type FileInfo

type FileInfo struct {
	Files           []string
	RefreshInterval time.Duration
}

FileInfo has attribute for file source

type FileSource

type FileSource struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewFileSource

func NewFileSource(fileInfo *FileInfo) *FileSource

func (*FileSource) Close

func (fs *FileSource) Close()

func (*FileSource) GetConfigurationByKey

func (fs *FileSource) GetConfigurationByKey(key string) (string, error)

GetConfigurationByKey implements ConfigSource

func (*FileSource) GetConfigurations

func (fs *FileSource) GetConfigurations() (map[string]string, error)

GetConfigurations implements ConfigSource

func (*FileSource) GetPriority

func (fs *FileSource) GetPriority() int

GetPriority implements ConfigSource

func (*FileSource) GetSourceName

func (fs *FileSource) GetSourceName() string

GetSourceName implements ConfigSource

func (*FileSource) SetEventHandler

func (fs *FileSource) SetEventHandler(eh EventHandler)

func (*FileSource) UpdateOptions

func (fs *FileSource) UpdateOptions(opts Options)

type Filter

type Filter func(key string) (string, bool)

func RemovePrefix

func RemovePrefix(prefix string) Filter

func WithOneOfPrefixs

func WithOneOfPrefixs(prefixs ...string) Filter

func WithPrefix

func WithPrefix(prefix string) Filter

func WithSubstr

func WithSubstr(substring string) Filter

type Manager

type Manager struct {
	sync.RWMutex
	Dispatcher *EventDispatcher
	// contains filtered or unexported fields
}

func Init

func Init(opts ...Option) (*Manager, error)

func NewManager

func NewManager() *Manager

func (*Manager) AddSource

func (m *Manager) AddSource(source Source) error

func (*Manager) Close

func (m *Manager) Close()

func (*Manager) DeleteConfig

func (m *Manager) DeleteConfig(key string)

Delete config at runtime, which has the highest priority to override all other sources

func (*Manager) FileConfigs

func (m *Manager) FileConfigs() map[string]string

func (*Manager) ForbidUpdate

func (m *Manager) ForbidUpdate(key string)

Ignore any of update events, which means the config cannot auto refresh anymore

func (*Manager) GetBy

func (m *Manager) GetBy(filters ...Filter) map[string]string

func (*Manager) GetConfig

func (m *Manager) GetConfig(key string) (string, error)

func (*Manager) GetConfigs

func (m *Manager) GetConfigs() map[string]string

GetConfigs returns all the key values

func (*Manager) GetIdentifier

func (m *Manager) GetIdentifier() string

func (*Manager) OnEvent

func (m *Manager) OnEvent(event *Event)

OnEvent Triggers actions when an event is generated

func (*Manager) ResetConfig

func (m *Manager) ResetConfig(key string)

Remove the config which set at runtime, use config from sources

func (*Manager) SetConfig

func (m *Manager) SetConfig(key, value string)

Update config at runtime, which can be called by others The most used scenario is UT

func (*Manager) UpdateSourceOptions

func (m *Manager) UpdateSourceOptions(opts ...Option)

type Option

type Option func(options *Options)

Option is a func

func WithEnvSource

func WithEnvSource(keyFormatter func(string) string) Option

WithEnvSource enable env source archaius will read ENV as key value

func WithEtcdSource

func WithEtcdSource(ri *EtcdInfo) Option

WithEtcdSource accept the information for initiating a remote source

func WithFilesSource

func WithFilesSource(fi *FileInfo) Option

WithRequiredFiles tell archaius to manage files, if not exist will return error

type Options

type Options struct {
	FileInfo        *FileInfo
	EtcdInfo        *EtcdInfo
	EnvKeyFormatter func(string) string
}

Options hold options

type Source

type Source interface {
	GetConfigurations() (map[string]string, error)
	GetConfigurationByKey(string) (string, error)
	GetPriority() int
	GetSourceName() string
	SetEventHandler(eh EventHandler)
	UpdateOptions(opt Options)
	Close()
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL