threadsafe

package
v6.0.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2021 License: Apache-2.0, BSD-2-Clause, BSD-3-Clause, + 1 more Imports: 14 Imported by: 15

Documentation

Overview

TODO rename

Index

Constants

View Source
const (
	InterfaceStatNameBytesIn  = "inBytes"
	InterfaceStatNameBytesOut = "outBytes"
	InterfaceStatNameSpeed    = "speed"
)

InterfaceStatNames is a "set" of the names of all the statistics that may be found on a network interface of a cache server.

View Source
const PolledBytesPerSecTimeout = time.Second * 10

Variables

This section is empty.

Functions

func CopyDurationMap

func CopyDurationMap(a map[tc.CacheName]time.Duration) map[tc.CacheName]time.Duration

CopyDurationMap copies this duration map.

func LegacyStatsMarshall

func LegacyStatsMarshall(
	statResultHistory ResultStatHistory,
	statInfo cache.ResultInfoHistory,
	combinedStates tc.CRStates,
	monitorConfig tc.TrafficMonitorConfigMap,
	statMaxKbpses cache.Kbpses,
	filter cache.Filter,
	params url.Values,
) ([]byte, error)

LegacyStatsMarshall encodes the stats in JSON, encoding up to historyCount of each stat. If statsToUse is empty, all stats are encoded; otherwise, only the given stats are encoded. If `wildcard` is true, stats which contain the text in each statsToUse are returned, instead of exact stat names. If cacheType is not CacheTypeInvalid, only stats for the given type are returned. If hosts is not empty, only the given hosts are returned.

func StatsMarshall

func StatsMarshall(
	statResultHistory ResultStatHistory,
	statInfo cache.ResultInfoHistory,
	combinedStates tc.CRStates,
	monitorConfig tc.TrafficMonitorConfigMap,
	statMaxKbpses cache.Kbpses,
	filter cache.Filter,
	params url.Values,
) ([]byte, error)

StatsMarshall encodes the stats in JSON, encoding up to historyCount of each stat. If statsToUse is empty, all stats are encoded; otherwise, only the given stats are encoded. If `wildcard` is true, stats which contain the text in each statsToUse are returned, instead of exact stat names. If cacheType is not CacheTypeInvalid, only stats for the given type are returned. If hosts is not empty, only the given hosts are returned.

Types

type CacheAvailableStatus

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

CacheAvailableStatus wraps a map of cache available statuses to be safe for multiple reader goroutines and one writer.

func NewCacheAvailableStatus

func NewCacheAvailableStatus() CacheAvailableStatus

NewCacheAvailableStatus creates and returns a new CacheAvailableStatus, initializing internal pointer values.

func (*CacheAvailableStatus) Get

Get returns the internal map of cache statuses. The returned map MUST NOT be modified. If modification is necessary, copy.

func (*CacheAvailableStatus) Set

Set sets the internal map of cache availability. This MUST NOT be called by multiple goroutines.

type CacheKbpses

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

CacheKbpses wraps a map of cache kbps measurements to be safe for multiple reader goroutines and one writer.

func NewCacheKbpses

func NewCacheKbpses() CacheKbpses

NewCacheKbpses creates and returns a new, empty CacheKbpses, initializing internal pointer values.

func (*CacheKbpses) Get

func (o *CacheKbpses) Get() cache.Kbpses

Get returns the internal map of cache kbps measurements. The returned map MUST NOT be modified. If modification is necessary, copy.

func (*CacheKbpses) Set

func (o *CacheKbpses) Set(v cache.Kbpses)

Set sets the internal map of cache kbps measurements. This MUST NOT be called by multiple goroutines.

type CacheStatHistory

type CacheStatHistory struct {
	// Interfaces is a map of the names of network interfaces that have been
	// polled for this cache server to historical collections of their polled
	// statistics.
	Interfaces map[string]ResultStatValHistory
	// Stats is a historical collection of all of the cache server's generic
	// (non-interface-dependent) statistics.
	Stats ResultStatValHistory
}

CacheStatHistory is the type of a single record in a ResultStatHistory map. It contains interface statistics as well as historical statistics for each of a cache server's polled interfaces.

func NewCacheStatHistory

func NewCacheStatHistory() CacheStatHistory

NewCacheStatHistory constructs a new empty CacheStatHistory.

type DSStats

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

DSStats wraps a deliveryservice.Stats object to be safe for multiple reader goroutines and a single writer.

func NewDSStats

func NewDSStats() DSStats

NewDSStats returns a deliveryservice.Stats object wrapped to be safe for multiple readers and a single writer.

func (*DSStats) Get

func (o *DSStats) Get() dsdata.StatsReadonly

Get returns a Stats object safe for reading by multiple goroutines

func (*DSStats) Set

func (o *DSStats) Set(newDsStats dsdata.Stats)

Set sets the internal Stats object. This MUST NOT be called by multiple goroutines.

type DSStatsReader

type DSStatsReader interface {
	Get() dsdata.StatsReadonly
}

DSStatsReader permits reading of a dsdata.Stats object, but not writing. This is designed so a Stats object can safely be passed to multiple goroutines, without worry one may unsafely write.

type DurationMap

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

DurationMap wraps a map[tc.CacheName]time.Duration in an object safe for a single writer and multiple readers.

func NewDurationMap

func NewDurationMap() DurationMap

NewDurationMap returns a new DurationMap safe for multiple readers and a single writer goroutine.

func (*DurationMap) Get

func (o *DurationMap) Get() map[tc.CacheName]time.Duration

Get returns the duration map. Callers MUST NOT mutate. If mutation is necessary, call DurationMap.Copy().

func (*DurationMap) Set

func (o *DurationMap) Set(d map[tc.CacheName]time.Duration)

Set sets the internal duration map. This MUST NOT be called by multiple goroutines.

type LastStats

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

LastStats wraps a deliveryservice.LastStats object to be safe for multiple readers and one writer.

func NewLastStats

func NewLastStats() LastStats

NewLastStats returns a wrapped a deliveryservice.LastStats object safe for multiple readers and one writer.

func (LastStats) Get

func (o LastStats) Get() dsdata.LastStats

Get returns the last KBPS stats object. Callers MUST NOT modify the object. It is not threadsafe for writing. If the object must be modified, callers must call LastStats.Copy() and modify the copy.

func (LastStats) Set

func (o LastStats) Set(s dsdata.LastStats)

Set sets the internal LastStats object. This MUST NOT be called by multiple goroutines.

type OpsConfig

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

OpsConfig provides safe access for multiple reader goroutines and a single writer to a stored OpsConfig object. This could be made lock-free, if the performance was necessary

func NewOpsConfig

func NewOpsConfig() OpsConfig

NewOpsConfig returns a new single-writer-multiple-reader OpsConfig

func (*OpsConfig) Get

func (o *OpsConfig) Get() handler.OpsConfig

Get gets the internal OpsConfig object. This MUST NOT be modified. If modification is necessary, copy the object.

func (*OpsConfig) Set

func (o *OpsConfig) Set(newOpsConfig handler.OpsConfig)

Set sets the internal OpsConfig object. This MUST NOT be called from multiple goroutines.

type ResultHistory

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

ResultHistory provides safe access for multiple goroutines readers and a single writer to a stored ResultHistory object. This could be made lock-free, if the performance was necessary TODO add separate locks for Caches and Deliveryservice maps?

func NewResultHistory

func NewResultHistory() ResultHistory

NewResultHistory returns a new ResultHistory safe for multiple readers and a single writer.

func (*ResultHistory) Get

Get returns the ResultHistory. Callers MUST NOT modify. If mutation is necessary, call ResultHistory.Copy()

func (*ResultHistory) Set

func (h *ResultHistory) Set(v cache.ResultHistory)

Set sets the internal ResultHistory. This is only safe for one thread of execution. This MUST NOT be called from multiple threads.

type ResultInfoHistory

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

ResultStatHistory provides safe access for multiple goroutines readers and a single writer to a stored HistoryHistory object. This could be made lock-free, if the performance was necessary

func NewResultInfoHistory

func NewResultInfoHistory() ResultInfoHistory

NewResultInfoHistory returns a new ResultInfoHistory safe for multiple readers and a single writer.

func (*ResultInfoHistory) Get

Get returns the ResultInfoHistory. Callers MUST NOT modify. If mutation is necessary, call ResultInfoHistory.Copy()

func (*ResultInfoHistory) Set

Set sets the internal ResultInfoHistory. This is only safe for one thread of execution. This MUST NOT be called from multiple threads.

type ResultStatHistory

type ResultStatHistory struct{ *sync.Map } // map[string]CacheStatHistory

ResultStatHistory is a thread-safe mapping of cache server hostnames to CacheStatHistory objects containing statistics for those cache servers.

func NewResultStatHistory

func NewResultStatHistory() ResultStatHistory

NewResultStatHistory constructs a new, empty ResultStatHistory.

func (ResultStatHistory) Add

func (a ResultStatHistory) Add(r cache.Result, limit uint64) error

Add adds the given result to the stored statistics history, keeping only up to `limit` number of records for any given stat (oldest records will be removed to make way for new ones if the limit would otherwise be exceeded).

If `limit` is zero, it will be treated as though it were one instead.

func (ResultStatHistory) LoadOrStore

func (h ResultStatHistory) LoadOrStore(hostname string) CacheStatHistory

LoadOrStore returns the stored CacheStatHistory for the given cache server hostname if it has already been stored. If it has not already been stored, a new, empty CacheStatHistory object is created, stored under the given hostname, and returned.

func (ResultStatHistory) Range

func (h ResultStatHistory) Range(f func(cacheName string, val CacheStatHistory) bool)

Range behaves like sync.Map.Range. It calls f for every value in the map; if f returns false, the iteration is stopped.

type ResultStatValHistory

type ResultStatValHistory struct{ *sync.Map } //  map[string][]ResultStatVal

ResultStatValHistory is thread-safe for one writer. Specifically, because a CompareAndSwap is not provided, it's not possible to Load and Store without a race condition. If multiple writers were necessary, it wouldn't be difficult to add a CompareAndSwap, internally storing an atomically-accessed pointer to the slice.

func NewResultStatValHistory

func NewResultStatValHistory() ResultStatValHistory

func (ResultStatValHistory) Load

func (h ResultStatValHistory) Load(stat string) []tc.ResultStatVal

Load returns the []ResultStatVal for the given stat. If the given stat does not exist, nil is returned.

func (ResultStatValHistory) Range

func (h ResultStatValHistory) Range(f func(stat string, val []tc.ResultStatVal) bool)

Range behaves like sync.Map.Range. It calls f for every value in the map; if f returns false, the iteration is stopped.

func (ResultStatValHistory) Store

func (h ResultStatValHistory) Store(stat string, vals []tc.ResultStatVal)

Store stores the given []ResultStatVal in the ResultStatValHistory for the given stat. Store is thread-safe for only one writer. Specifically, if there are multiple writers, there's a race, that one writer could Load(), another writer could Store() underneath it, and the first writer would then Store() having lost values. To safely use ResultStatValHistory with multiple writers, a CompareAndSwap method would have to be added.

type TrafficMonitorConfigMap

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

TrafficMonitorConfigMapThreadsafe encapsulates a LegacyTrafficMonitorConfigMap safe for multiple readers and a single writer.

func NewTrafficMonitorConfigMap

func NewTrafficMonitorConfigMap() TrafficMonitorConfigMap

NewTrafficMonitorConfigMap returns an encapsulated LegacyTrafficMonitorConfigMap safe for multiple readers and a single writer.

func (*TrafficMonitorConfigMap) Get

Get returns the LegacyTrafficMonitorConfigMap. Callers MUST NOT modify, it is not threadsafe for mutation. If mutation is necessary, call CopyTrafficMonitorConfigMap().

func (*TrafficMonitorConfigMap) Set

Set sets the LegacyTrafficMonitorConfigMap. This is only safe for one writer. This MUST NOT be called by multiple threads.

type Uint

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

Uint provides safe access for multiple goroutines readers and a single writer to a stored uint.

func NewUint

func NewUint() Uint

NewUint returns a new single-writer-multiple-reader threadsafe uint

func (*Uint) Get

func (u *Uint) Get() uint64

Get gets the internal uint. This is safe for multiple readers

func (*Uint) Inc

func (u *Uint) Inc() uint64

Inc increments the internal uint64. TODO make sure everything using this uses the value it returns, not a separate Get

func (*Uint) Set

func (u *Uint) Set(v uint64)

Set sets the internal uint. This MUST NOT be called by multiple goroutines.

type UnpolledCaches

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

UnpolledCaches is a structure containing a map of caches which have yet to be polled, which is threadsafe for multiple readers and one writer. This could be made lock-free, if the performance was necessary

func NewUnpolledCaches

func NewUnpolledCaches() UnpolledCaches

NewUnpolledCaches returns a new UnpolledCaches object.

func (*UnpolledCaches) Any

func (t *UnpolledCaches) Any() bool

Any returns whether there are any caches marked as not polled. Also returns true if SetNewCaches() has never been called (assuming there exist caches, if this hasn't been initialized, we couldn't have polled any of them).

func (*UnpolledCaches) SetNewCaches

func (t *UnpolledCaches) SetNewCaches(newCaches map[tc.CacheName]struct{})

SetNewCaches takes a list of new caches, which may overlap with the existing caches, diffs them, removes any `unpolledCaches` which aren't in the new list, and sets the list of `polledCaches` (which is only used by this func) to the `newCaches`. This is threadsafe with one writer, along with `setUnpolledCaches`.

func (*UnpolledCaches) SetPolled

func (t *UnpolledCaches) SetPolled(results []cache.Result, lastStats dsdata.LastStats)

SetPolled sets cache which have been polled. This is used to determine when the app has fully started up, and we can start serving. Serving Traffic Router with caches as 'down' which simply haven't been polled yet would be bad. Therefore, a cache is set as 'polled' if it has received different bandwidths from two different ATS ticks, OR if the cache is marked as down (and thus we won't get a bandwidth). This is threadsafe for one writer, along with `Set`. This is fast if there are no unpolled caches. Moreover, its speed is a function of the number of unpolled caches, not the number of caches total.

func (*UnpolledCaches) UnpolledCaches

func (t *UnpolledCaches) UnpolledCaches() map[tc.CacheName]struct{}

UnpolledCaches returns a map of caches not yet polled. Callers MUST NOT modify. If mutation is necessary, copy the map

Jump to

Keyboard shortcuts

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