blink

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Exists = func(path string) bool {
	_, err := os.Stat(path)
	return err == nil
}

Exists checks if the given path exists, using os.Stat

View Source
var FatalExit = func(err error) {
	log.Fatalln(err)
}

FatalExit ends the program after logging a message

View Source
var LogError = func(err error) {
	log.Println(err.Error())
}

LogError logs a message as an error, but does not end the program

View Source
var LogInfo = func(msg string) {
	log.Println(msg)
}

LogInfo logs a message as information

Functions

func CollectFileChangeEvents

func CollectFileChangeEvents(watcher *RecursiveWatcher, mut *sync.Mutex, events TimeEventMap, maxAge time.Duration)

CollectFileChangeEvents gathers filesystem events in a way that web handle functions can use. Performance improvements: 1. Uses a buffered channel for event processing to handle bursts of events 2. Implements event debouncing to reduce duplicate events 3. Uses a separate goroutine for event processing to avoid blocking the main thread 4. Implements periodic cleanup of old events to prevent memory leaks

func EventServer

func EventServer(path, allowed, eventAddr, eventPath string, refreshDuration time.Duration)

EventServer serves events on a dedicated port. addr is the host address ([host][:port]) The filesystem events are gathered independently of that. Allowed can be "*" or a hostname and sets a header in the SSE stream.

func Flush

func Flush(w http.ResponseWriter) bool

Flush can flush the given ResponseWriter. Returns false if it wasn't an http.Flusher.

func GenFileChangeEvents

func GenFileChangeEvents(events TimeEventMap, mut *sync.Mutex, maxAge time.Duration, allowed string) http.HandlerFunc

GenFileChangeEvents creates an SSE event whenever a file in the server directory changes.

Uses the following HTTP headers:

Content-Type: text/event-stream;charset=utf-8
Cache-Control: no-cache
Connection: keep-alive
Access-Control-Allow-Origin: (custom value)

The "Access-Control-Allow-Origin" header uses the value that is passed in the "allowed" argument.

func RemoveOldEvents

func RemoveOldEvents(events *TimeEventMap, maxAge time.Duration)

RemoveOldEvents can remove old filesystem events, after a certain duration. Needs to be called within a mutex!

func SetVerbose

func SetVerbose(enabled bool)

SetVerbose can be used to enable or disable logging of incoming events

func ShouldIgnoreFile

func ShouldIgnoreFile(name string) bool

ShouldIgnoreFile determines if a file or directory should be ignored based on its name. This is a performance-critical function as it's called for every file and directory. Performance improvement: Using a pre-defined list of prefixes to check against.

func Subfolders

func Subfolders(path string) (paths []string)

Subfolders returns a list of all subdirectories in the given path. It follows symbolic links and ignores directories that match the ignore criteria. Performance improvements: 1. Pre-allocates the paths slice with a reasonable capacity 2. Uses a concurrent approach with worker pools for large directory structures 3. Implements early termination for ignored directories

func WriteEvent

func WriteEvent(w http.ResponseWriter, id *uint64, message string, flush bool)

WriteEvent writes SSE events to the given ResponseWriter. id can be nil.

Types

type Event

type Event fsnotify.Event

func (Event) String

func (e Event) String() string

type RecursiveWatcher

type RecursiveWatcher struct {
	*fsnotify.Watcher
	Files   chan string // Channel for file events
	Folders chan string // Channel for folder events
	// contains filtered or unexported fields
}

RecursiveWatcher keeps the data for watching files and directories. It embeds fsnotify.Watcher and adds channels for tracking files and folders.

func NewRecursiveWatcher

func NewRecursiveWatcher(path string) (*RecursiveWatcher, error)

NewRecursiveWatcher creates a new RecursiveWatcher. Takes a path to a directory to watch recursively. Performance improvements: 1. Uses a worker pool for adding folders in parallel 2. Pre-allocates the paths slice with a reasonable capacity 3. Uses a mutex for thread-safe operations

func (*RecursiveWatcher) AddFolder

func (watcher *RecursiveWatcher) AddFolder(folder string) error

AddFolder adds a directory to watch, non-recursively. It's thread-safe due to the mutex lock.

func (*RecursiveWatcher) Close

func (watcher *RecursiveWatcher) Close() error

Close properly closes the watcher and its channels. This method should be called when the watcher is no longer needed to prevent resource leaks.

type TimeEventMap

type TimeEventMap map[time.Time]Event

TimeEventMap stores filesystem events

Jump to

Keyboard shortcuts

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