diff options
| author | mhsanaei <ho3ein.sanaei@gmail.com> | 2025-09-20 10:35:50 +0300 |
|---|---|---|
| committer | mhsanaei <ho3ein.sanaei@gmail.com> | 2025-09-20 10:35:50 +0300 |
| commit | 6ced549deaecb42b9bb93ea9efcb4c1bbaabe8a4 (patch) | |
| tree | 28d8d82530476cf607e4d05ca189ae05868711e6 /web/global | |
| parent | f60682a6b7cb749fee403c84e2587c3ad7e7ced0 (diff) | |
docs: add comments for all functions
Diffstat (limited to 'web/global')
| -rw-r--r-- | web/global/global.go | 13 | ||||
| -rw-r--r-- | web/global/hashStorage.go | 18 |
2 files changed, 23 insertions, 8 deletions
diff --git a/web/global/global.go b/web/global/global.go index e92c375b..025fa081 100644 --- a/web/global/global.go +++ b/web/global/global.go @@ -1,3 +1,4 @@ +// Package global provides global variables and interfaces for accessing web and subscription servers. package global import ( @@ -12,27 +13,33 @@ var ( subServer SubServer ) +// WebServer interface defines methods for accessing the web server instance. type WebServer interface { - GetCron() *cron.Cron - GetCtx() context.Context + GetCron() *cron.Cron // Get the cron scheduler + GetCtx() context.Context // Get the server context } +// SubServer interface defines methods for accessing the subscription server instance. type SubServer interface { - GetCtx() context.Context + GetCtx() context.Context // Get the server context } +// SetWebServer sets the global web server instance. func SetWebServer(s WebServer) { webServer = s } +// GetWebServer returns the global web server instance. func GetWebServer() WebServer { return webServer } +// SetSubServer sets the global subscription server instance. func SetSubServer(s SubServer) { subServer = s } +// GetSubServer returns the global subscription server instance. func GetSubServer() SubServer { return subServer } diff --git a/web/global/hashStorage.go b/web/global/hashStorage.go index 5d8135ee..962493f7 100644 --- a/web/global/hashStorage.go +++ b/web/global/hashStorage.go @@ -8,18 +8,21 @@ import ( "time" ) +// HashEntry represents a stored hash entry with its value and timestamp. type HashEntry struct { - Hash string - Value string - Timestamp time.Time + Hash string // MD5 hash string + Value string // Original value + Timestamp time.Time // Time when the hash was created } +// HashStorage provides thread-safe storage for hash-value pairs with expiration. type HashStorage struct { sync.RWMutex - Data map[string]HashEntry - Expiration time.Duration + Data map[string]HashEntry // Map of hash to entry + Expiration time.Duration // Expiration duration for entries } +// NewHashStorage creates a new HashStorage instance with the specified expiration duration. func NewHashStorage(expiration time.Duration) *HashStorage { return &HashStorage{ Data: make(map[string]HashEntry), @@ -27,6 +30,7 @@ func NewHashStorage(expiration time.Duration) *HashStorage { } } +// SaveHash generates an MD5 hash for the given query string and stores it with a timestamp. func (h *HashStorage) SaveHash(query string) string { h.Lock() defer h.Unlock() @@ -45,6 +49,7 @@ func (h *HashStorage) SaveHash(query string) string { return md5HashString } +// GetValue retrieves the original value for the given hash, returning true if found. func (h *HashStorage) GetValue(hash string) (string, bool) { h.RLock() defer h.RUnlock() @@ -54,11 +59,13 @@ func (h *HashStorage) GetValue(hash string) (string, bool) { return entry.Value, exists } +// IsMD5 checks if the given string is a valid 32-character MD5 hash. func (h *HashStorage) IsMD5(hash string) bool { match, _ := regexp.MatchString("^[a-f0-9]{32}$", hash) return match } +// RemoveExpiredHashes removes all hash entries that have exceeded the expiration duration. func (h *HashStorage) RemoveExpiredHashes() { h.Lock() defer h.Unlock() @@ -72,6 +79,7 @@ func (h *HashStorage) RemoveExpiredHashes() { } } +// Reset clears all stored hash entries. func (h *HashStorage) Reset() { h.Lock() defer h.Unlock() |
