added dynamic loading of favicons

This commit is contained in:
partisan 2025-05-09 08:26:14 +02:00
parent 255acb360f
commit 81fb811111
4 changed files with 355 additions and 35 deletions

View file

@ -50,6 +50,30 @@ var (
iconLinkRegex = regexp.MustCompile(`<link[^>]+rel=["'](?:icon|shortcut icon|apple-touch-icon)["'][^>]+href=["']([^"']+)["']`)
)
// Add this near the top with other vars
var (
faviconDownloadQueue = make(chan faviconDownloadRequest, 1000)
)
type faviconDownloadRequest struct {
faviconURL string
pageURL string
cacheID string
}
func init() {
// Start 5 worker goroutines to process favicon downloads
for i := 0; i < 5; i++ {
go faviconDownloadWorker()
}
}
func faviconDownloadWorker() {
for req := range faviconDownloadQueue {
cacheFavicon(req.faviconURL, req.cacheID)
}
}
// Generates a cache ID from URL
func faviconIDFromURL(rawURL string) string {
hasher := md5.New()
@ -312,14 +336,14 @@ func getFaviconProxyURL(rawFavicon, pageURL string) string {
return fmt.Sprintf("/image/%s_thumb.webp", cacheID)
}
// Resolve URL (but ignore resolved ID — we always use the one from pageURL)
// Resolve URL
faviconURL, _ := resolveFaviconURL(rawFavicon, pageURL)
if faviconURL == "" {
recordInvalidImageID(cacheID)
return "/static/images/missing.svg"
}
// Avoid re-downloading
// Check if already downloading
faviconCache.RLock()
downloading := faviconCache.m[cacheID]
faviconCache.RUnlock()
@ -329,17 +353,12 @@ func getFaviconProxyURL(rawFavicon, pageURL string) string {
faviconCache.m[cacheID] = true
faviconCache.Unlock()
go func() {
defer func() {
faviconCache.Lock()
delete(faviconCache.m, cacheID)
faviconCache.Unlock()
}()
_, _, err := cacheFavicon(faviconURL, cacheID)
if err != nil {
recordInvalidImageID(cacheID)
}
}()
// Send to download queue instead of starting goroutine
faviconDownloadQueue <- faviconDownloadRequest{
faviconURL: faviconURL,
pageURL: pageURL,
cacheID: cacheID,
}
}
return fmt.Sprintf("/image/%s_thumb.webp", cacheID)