talos/internal/pkg/circular/options.go
Andrey Smirnov 0a4645fe80 feat: implement circular buffer for system logs
This replaces logging to files with inotify following to pure in-memory
circular buffer which grows on demand capped at specified maximum
capacity.

The concern with previous approach was that logs on tmpfs were growing
without any bound potentially consuming all the node memory.

Signed-off-by: Andrey Smirnov <smirnov.andrey@gmail.com>
2020-06-26 15:33:54 -07:00

70 lines
1.7 KiB
Go

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package circular
import "fmt"
// Options defines settings for Buffer.
type Options struct {
InitialCapacity int
MaxCapacity int
SafetyGap int
}
// defaultOptions returns default initial values.
func defaultOptions() Options {
return Options{
InitialCapacity: 16384,
MaxCapacity: 1048576,
SafetyGap: 1024,
}
}
// OptionFunc allows setting Buffer options.
type OptionFunc func(*Options) error
// WithInitialCapacity sets initial buffer capacity.
func WithInitialCapacity(cap int) OptionFunc {
return func(opt *Options) error {
if cap <= 0 {
return fmt.Errorf("initial capacity should be positive: %d", cap)
}
opt.InitialCapacity = cap
return nil
}
}
// WithMaxCapacity sets maximum buffer capacity.
func WithMaxCapacity(cap int) OptionFunc {
return func(opt *Options) error {
if cap <= 0 {
return fmt.Errorf("max capacity should be positive: %d", cap)
}
opt.MaxCapacity = cap
return nil
}
}
// WithSafetyGap sets safety gap between readers and writers to avoid buffer overrun for the reader.
//
// Reader initial position is set to be as far as possible in the buffer history, but next concurrent write
// might overwrite read position, and safety gap helps to prevent it. With safety gap, maximum available
// bytes to read are: MaxCapacity-SafetyGap.
func WithSafetyGap(gap int) OptionFunc {
return func(opt *Options) error {
if gap <= 0 {
return fmt.Errorf("safety gap should be positive: %q", gap)
}
opt.SafetyGap = gap
return nil
}
}