system

package
v1.13.2 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrLockerLocked = errors.Sentinel("locker: 無法取得鎖定,目前已被鎖定")
View Source
var Version = "develop"

Functions

func FirstNotEmpty

func FirstNotEmpty(v ...string) string

FirstNotEmpty 回傳傳入的字串中第一個非空值的字串。

func FormatBytes

func FormatBytes[T int | int16 | int32 | int64 | uint | uint16 | uint32 | uint64](b T) string

func GetDockerInfo

func GetDockerInfo(ctx context.Context) (types.Version, system.Info, error)

func MustInt

func MustInt(v string) int

func ScanReader

func ScanReader(r io.Reader, callback func(line []byte)) error

ScanReader 從 reader 中讀取最多 64KB 的行內容,並將該值透過 websocket 發出。若某一行超過該大小,就會被截斷,只傳送截斷後的 部分。

Types

type Atomic

type Atomic[T any] struct {
	// contains filtered or unexported fields
}

func NewAtomic

func NewAtomic[T any](v T) *Atomic[T]

func (*Atomic[T]) Load

func (a *Atomic[T]) Load() T

Load 讀取此值並回傳。

func (*Atomic[T]) MarshalJSON

func (a *Atomic[T]) MarshalJSON() ([]byte, error)

MarshalJSON 將 Atomic[T] 的值序列化為 JSON。

func (*Atomic[T]) Store

func (a *Atomic[T]) Store(v T)

Store 以原子操作方式儲存傳入的值。

func (*Atomic[T]) UnmarshalJSON

func (a *Atomic[T]) UnmarshalJSON(b []byte) error

UnmarshalJSON 將 JSON 值反序列化為 Atomic[T] 的值。

type AtomicBool

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

func NewAtomicBool

func NewAtomicBool(v bool) *AtomicBool

func (*AtomicBool) Load

func (ab *AtomicBool) Load() bool

func (*AtomicBool) MarshalJSON

func (ab *AtomicBool) MarshalJSON() ([]byte, error)

func (*AtomicBool) Store

func (ab *AtomicBool) Store(v bool)

func (*AtomicBool) SwapIf

func (ab *AtomicBool) SwapIf(v bool) bool

SwapIf 若 AtomicBool 目前儲存的值與相反的布林值不同,就會儲存值 「v」。若成功交換,回應為「true」,否則回傳「false」。

func (*AtomicBool) UnmarshalJSON

func (ab *AtomicBool) UnmarshalJSON(b []byte) error

type AtomicString

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

AtomicString 讓你可以讀寫指定的結構欄位,而不必擔心潛在的競態 條件情況。其底層使用一個簡單的 sync.RWMutex 來控制對此值的存取。

func NewAtomicString

func NewAtomicString(v string) *AtomicString

func (*AtomicString) Load

func (as *AtomicString) Load() string

Load 讀取字串值並回傳。

func (*AtomicString) MarshalJSON

func (as *AtomicString) MarshalJSON() ([]byte, error)

func (*AtomicString) Store

func (as *AtomicString) Store(v string)

Store 以原子操作方式儲存傳入的字串值。

func (*AtomicString) UnmarshalJSON

func (as *AtomicString) UnmarshalJSON(b []byte) error

type ContextBag

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

func NewContextBag

func NewContextBag(ctx context.Context) *ContextBag

func (*ContextBag) Cancel

func (cb *ContextBag) Cancel(key string)

func (*ContextBag) CancelAll

func (cb *ContextBag) CancelAll()

func (*ContextBag) Context

func (cb *ContextBag) Context(key string) context.Context

Context 回傳指定 key 對應的 context。若內部映射中已存在該值,就會 回傳它,否則會回傳一個新的可取消 context。此 context 會在呼叫 Cancel 或 CancelAll 觸發其取消函式之前,於所有呼叫端之間共用。

type DockerCgroups

type DockerCgroups struct {
	Driver  string `json:"driver"`
	Version string `json:"version"`
}

type DockerContainers

type DockerContainers struct {
	Total   int `json:"total"`
	Running int `json:"running"`
	Paused  int `json:"paused"`
	Stopped int `json:"stopped"`
}

type DockerInformation

type DockerInformation struct {
	Version    string           `json:"version"`
	Cgroups    DockerCgroups    `json:"cgroups"`
	Containers DockerContainers `json:"containers"`
	Storage    DockerStorage    `json:"storage"`
	Runc       DockerRunc       `json:"runc"`
}

type DockerRunc

type DockerRunc struct {
	Version string `json:"version"`
}

type DockerStorage

type DockerStorage struct {
	Driver     string `json:"driver"`
	Filesystem string `json:"filesystem"`
}

type Information

type Information struct {
	Version string            `json:"version"`
	Docker  DockerInformation `json:"docker"`
	System  System            `json:"system"`
}

func GetSystemInformation

func GetSystemInformation() (*Information, error)

type Locker

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

func NewLocker

func NewLocker() *Locker

NewLocker 回傳一個新的 Locker 實例。

func (*Locker) Acquire

func (l *Locker) Acquire() error

Acquire 若電源鎖目前尚未鎖定,就會取得該鎖。若已經被鎖定, acquire 將無法取得鎖,並回傳 false。

func (*Locker) Destroy

func (l *Locker) Destroy()

Destroy 藉由關閉 channel 來清理電源鎖定器。

func (*Locker) IsLocked

func (l *Locker) IsLocked() bool

IsLocked 回傳鎖定器 channel 目前的狀態。若 channel 目前存在一個值, 就會被視為已鎖定。

func (*Locker) Release

func (l *Locker) Release()

Release 會清空鎖定器的 channel,讓我們之後能夠正確地重新取得鎖。 若 channel 目前並未鎖定,此函式將不做任何事並立即回傳。

func (*Locker) TryAcquire

func (l *Locker) TryAcquire(ctx context.Context) error

TryAcquire 會持續嘗試取得電源鎖,直到提供的 context 被取消為止。

type Rate

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

Rate 定義了一個速率限制器,在每段時間長度(duration)內限制 n 個項目 (limit)。

func NewRate

func NewRate(limit uint64, duration time.Duration) *Rate

func (*Rate) Reset

func (r *Rate) Reset()

Reset 將速率限制器的內部狀態重設回零。

func (*Rate) Try

func (r *Rate) Try() bool

Try 若在定義的速率限制內則回傳 true,若目前這段時間內已超過速率 限制則回傳 false。

type SinkName

type SinkName string

SinkName 代表伺服器已註冊的其中一個 sink。

const (
	// LogSink 處理遊戲伺服器的主控台輸出,包括透過 Wings 傳送到主控台
	// 實例的訊息。
	LogSink SinkName = "log"
	// InstallSink 處理伺服器的安裝輸出。
	InstallSink SinkName = "install"
)

type SinkPool

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

SinkPool 代表一個包含多個 sink 的池。

func NewSinkPool

func NewSinkPool() *SinkPool

NewSinkPool 回傳一個新的空 SinkPool。sink 池通常會伴隨伺服器實例 的整個生命週期存在。

func (*SinkPool) Destroy

func (p *SinkPool) Destroy()

Destroy 藉由移除並關閉所有 sink,並銷毀所有目前存在的 channel, 來銷毀此池。

func (*SinkPool) Off

func (p *SinkPool) Off(c chan []byte)

Off 從此 sink 池中移除指定的 channel。若找不到相符的 sink,此函式 將不做任何事。若找到相符的 channel,則會將其移除。

func (*SinkPool) On

func (p *SinkPool) On(c chan []byte)

On 將一個 channel 加入此 sink 池實例。

func (*SinkPool) Push

func (p *SinkPool) Push(data []byte)

Push 將指定的訊息傳送給池中已註冊的每一個 channel。此函式會使用 環形緩衝區(Ring Buffer)的 channel 概念,以避免阻塞 channel 的傳送, 並嘗試優先推送佇列中較新的訊息,而非較舊的訊息。

若 channel 已滿且未能及時被消耗,此函式會移除 channel 中最舊的 訊息,然後將取得的訊息推送到尾端,實際上讓此 channel 成為一個 滾動式緩衝區。

透過此函式傳遞資料時,有可能會遺失資料,但僅限於 channel 緩衝區 已滿且未能及時被消耗的情況,這種情況下捨棄訊息很可能仍是最好的 選擇。此函式使用 waitgroup,讓每個 channel 能同時嘗試傳送,因此 此函式的總阻塞時間為「O(1)」而非「O(n)」。

type System

type System struct {
	Architecture  string `json:"architecture"`
	CPUThreads    int    `json:"cpu_threads"`
	MemoryBytes   int64  `json:"memory_bytes"`
	KernelVersion string `json:"kernel_version"`
	OS            string `json:"os"`
	OSType        string `json:"os_type"`
}

Jump to

Keyboard shortcuts

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