Documentation ¶
Index ¶
- func CopyFileAtomic(dest string, src string, opts *AtomicFileOptions) error
- func CreateAtomic(dest string, r io.Reader, opts *AtomicFileOptions) error
- func DerivedInstanceUUID(input string) uuid.UUID
- func DerivedUUID(input string) uuid.UUID
- func DuplicateBytes(a []byte) []byte
- func DuplicateStrings(a []string) []string
- func EnsureDirectory(path string, perm os.FileMode) error
- func IndexOfString(a []string, s string) int
- func MimeTypeByExtension(ext string) (mimeType string, ok bool)
- func PathExists(path string) bool
- func RandomUUID(ns string) uuid.UUID
- func RemoveFromStringSlice(a []string, s string) []string
- func ReplaceFileAtomic(dest string, src string, opts *AtomicFileOptions) error
- func SafeFirst16Bytes(data []byte) string
- func SafeFirst16Chars(s string) string
- func StringInSlice(a []string, s string) bool
- func StringSliceEqual(a []string, b []string) bool
- type AtomicFileOptions
- type BroadcastFlag
- type CallLimiter
- type DirStructure
- func (ds *DirStructure) ChildDir(dirName string, perm os.FileMode) (child *DirStructure)
- func (ds *DirStructure) Ensure() error
- func (ds *DirStructure) EnsureAbsPath(dirPath string) error
- func (ds *DirStructure) EnsureRelDir(dirNames ...string) error
- func (ds *DirStructure) EnsureRelPath(dirPath string) error
- type Flag
- type OnceAgain
- type StablePool
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CopyFileAtomic ¶
func CopyFileAtomic(dest string, src string, opts *AtomicFileOptions) error
CopyFileAtomic is like CreateAtomic but copies content from src to dest. If opts.Mode is 0 CopyFileAtomic tries to set the file mode of src to dest.
func CreateAtomic ¶
func CreateAtomic(dest string, r io.Reader, opts *AtomicFileOptions) error
CreateAtomic creates or overwrites a file at dest atomically using data from r. Atomic means that even in case of a power outage, dest will never be a zero-length file. It will always either contain the previous data (or not exist) or the new data but never anything in between.
func DerivedInstanceUUID ¶
DerivedInstanceUUID returns a new UUID that is derived from the input, but is unique per instance (execution) and therefore is only reproducible with the same process.
func DerivedUUID ¶
DerivedUUID returns a new UUID that is derived from the input only, and therefore is always reproducible.
func DuplicateBytes ¶
DuplicateBytes returns a new copy of the given byte slice.
func DuplicateStrings ¶
DuplicateStrings returns a new copy of the given string slice.
func EnsureDirectory ¶
EnsureDirectory ensures that the given directory exists and that is has the given permissions set. If path is a file, it is deleted and a directory created.
func IndexOfString ¶
IndexOfString returns the index of given string and -1 if its not part of the slice.
func MimeTypeByExtension ¶
MimeTypeByExtension returns a mimetype for the given file name extension, which must including the leading dot. If the extension is not known, the call returns with ok=false and, additionally, a default "application/octet-stream" mime type is returned.
func PathExists ¶
PathExists returns whether the given path (file or dir) exists.
func RandomUUID ¶
RandomUUID returns a new random UUID with optionally provided ns.
func RemoveFromStringSlice ¶
RemoveFromStringSlice removes the given string from the slice and returns a new slice.
func ReplaceFileAtomic ¶
func ReplaceFileAtomic(dest string, src string, opts *AtomicFileOptions) error
ReplaceFileAtomic replaces the file at dest with the content from src. If dest exists it's file mode copied and used for the replacement. If not, dest will get the same file mode as src. See CopyFileAtomic and CreateAtomic for more information.
func SafeFirst16Bytes ¶
SafeFirst16Bytes return the first 16 bytes of the given data in safe form.
func SafeFirst16Chars ¶
SafeFirst16Chars return the first 16 characters of the given data in safe form.
func StringInSlice ¶
StringInSlice returns whether the given string is in the string slice.
func StringSliceEqual ¶
StringSliceEqual returns whether the given string slices are equal.
Types ¶
type AtomicFileOptions ¶
type AtomicFileOptions struct { // Mode is the file mode for the new file. If // 0, the file mode will be set to 0600. Mode os.FileMode // TempDir is the path to the temp-directory // that should be used. If empty, it defaults // to the system temp. TempDir string }
AtomicFileOptions holds additional options for manipulating the behavior of CreateAtomic and friends.
type BroadcastFlag ¶
type BroadcastFlag struct {
// contains filtered or unexported fields
}
BroadcastFlag is a simple system to broadcast a flag value.
func NewBroadcastFlag ¶
func NewBroadcastFlag() *BroadcastFlag
NewBroadcastFlag returns a new BroadcastFlag. In the initial state, the flag is not set and the signal does not trigger.
func (*BroadcastFlag) NewFlag ¶
func (bf *BroadcastFlag) NewFlag() *Flag
NewFlag returns a new Flag that listens to this broadcasting flag. In the initial state, the flag is set and the signal triggers. You can call Refresh immediately to get the current state from the broadcasting flag.
func (*BroadcastFlag) NotifyAndReset ¶
func (bf *BroadcastFlag) NotifyAndReset()
NotifyAndReset notifies all flags of this broadcasting flag and resets the internal broadcast flag state.
type CallLimiter ¶
type CallLimiter struct {
// contains filtered or unexported fields
}
CallLimiter bundles concurrent calls and optionally limits how fast a function is called.
func NewCallLimiter ¶
func NewCallLimiter(minPause time.Duration) *CallLimiter
NewCallLimiter returns a new call limiter. Set minPause to zero to disable the minimum pause between calls.
func (*CallLimiter) Do ¶
func (l *CallLimiter) Do(f func())
Do executes the given function. All concurrent calls to Do are bundled and return when f() finishes. Waits until the minimum pause is over before executing f() again.
type DirStructure ¶
type DirStructure struct { sync.Mutex Path string Dir string Perm os.FileMode Parent *DirStructure Children map[string]*DirStructure }
DirStructure represents a directory structure with permissions that should be enforced.
Example ¶
Output: / [755] /repo [777] /repo/b [707] /repo/b/c [750] /repo/b/d [707] /repo/b/d/e [707] /repo/b/d/f [707] /repo/b/d/f/g [707] /repo/b/d/f/g/h [707] /secret [700]
func NewDirStructure ¶
func NewDirStructure(path string, perm os.FileMode) *DirStructure
NewDirStructure returns a new DirStructure.
func (*DirStructure) ChildDir ¶
func (ds *DirStructure) ChildDir(dirName string, perm os.FileMode) (child *DirStructure)
ChildDir adds a new child DirStructure and returns it. Should the child already exist, the existing child is returned and the permissions are updated.
func (*DirStructure) Ensure ¶
func (ds *DirStructure) Ensure() error
Ensure ensures that the specified directory structure (from the first parent on) exists.
func (*DirStructure) EnsureAbsPath ¶
func (ds *DirStructure) EnsureAbsPath(dirPath string) error
EnsureAbsPath ensures that the specified directory structure (from the first parent on) and the given absolute path exists. If the given path is outside the DirStructure, an error will be returned.
func (*DirStructure) EnsureRelDir ¶
func (ds *DirStructure) EnsureRelDir(dirNames ...string) error
EnsureRelDir ensures that the specified directory structure (from the first parent on) and the given relative path (to the DirStructure) exists.
func (*DirStructure) EnsureRelPath ¶
func (ds *DirStructure) EnsureRelPath(dirPath string) error
EnsureRelPath ensures that the specified directory structure (from the first parent on) and the given relative path (to the DirStructure) exists.
type Flag ¶
type Flag struct {
// contains filtered or unexported fields
}
Flag receives changes from its broadcasting flag. A Flag must only be used in one goroutine and is not concurrency safe, but fast.
func (*Flag) IsSet ¶
IsSet returns whether the flag was set since the last Refresh. This does not reset the Flag itself, you'll need to call Refresh for that.
type OnceAgain ¶
type OnceAgain struct {
// contains filtered or unexported fields
}
OnceAgain is an object that will perform only one action "in flight". It's basically the same as sync.Once, but is automatically reused when the function was executed and everyone who waited has left. Important: This is somewhat racy when used heavily as it only resets _after_ everyone who waited has left. So, while some goroutines are waiting to be activated again to leave the waiting state, other goroutines will call Do() without executing the function again.
func (*OnceAgain) Do ¶
func (o *OnceAgain) Do(f func())
Do calls the function f if and only if Do is being called for the first time for this instance of Once. In other words, given
var once Once
if once.Do(f) is called multiple times, only the first call will invoke f, even if f has a different value in each invocation. A new instance of Once is required for each function to execute.
Do is intended for initialization that must be run exactly once. Since f is niladic, it may be necessary to use a function literal to capture the arguments to a function to be invoked by Do:
config.once.Do(func() { config.init(filename) })
Because no call to Do returns until the one call to f returns, if f causes Do to be called, it will deadlock.
If f panics, Do considers it to have returned; future calls of Do return without calling f.
type StablePool ¶
type StablePool struct { // New optionally specifies a function to generate // a value when Get would otherwise return nil. // It may not be changed concurrently with calls to Get. New func() interface{} // contains filtered or unexported fields }
A StablePool is a drop-in replacement for sync.Pool that is slower, but predictable. A StablePool is a set of temporary objects that may be individually saved and retrieved.
In contrast to sync.Pool, items are not removed automatically. Every item will be returned at some point. Items are returned in a FIFO manner in order to evenly distribute usage of a set of items.
A StablePool is safe for use by multiple goroutines simultaneously and must not be copied after first use.
func (*StablePool) Get ¶
func (p *StablePool) Get() interface{}
Get returns the next item from the Pool, removes it from the Pool, and returns it to the caller. In contrast to sync.Pool, Get never ignores the pool. Callers should not assume any relation between values passed to Put and the values returned by Get.
If Get would otherwise return nil and p.New is non-nil, Get returns the result of calling p.New.
func (*StablePool) Max ¶
func (p *StablePool) Max() int
Max returns the amount of items the pool held at maximum.
func (*StablePool) Size ¶
func (p *StablePool) Size() int
Size returns the amount of items the pool currently holds.