refactoring

This commit is contained in:
Aleksandr Dubovikov 2024-10-01 11:34:04 +02:00
parent d8d6df3166
commit 677fbb2c97

View file

@ -94,19 +94,13 @@ func makeUrlPreviewHandler(
} }
} }
// Get url preview from cache hash := getHashFromString(pUrl)
if cacheRecord, ok := urlPreviewCache.Records[pUrl]; ok {
if cacheRecord.Error != nil { // Get for url preview from in-memory cache
return util.ErrorResponse(cacheRecord.Error) if response, ok := checkInternalCacheResponse(urlPreviewCache, pUrl); ok {
} return response
return util.JSONResponse{
Code: http.StatusOK,
JSON: cacheRecord.Preview,
}
} }
hash := getHashFromString(pUrl)
// Check if we have a previously stored response
if urlPreviewCached, err := loadUrlPreviewResponse(req.Context(), cfg, db, hash); err == nil { if urlPreviewCached, err := loadUrlPreviewResponse(req.Context(), cfg, db, hash); err == nil {
logger.Debug("Loaded url preview from the cache") logger.Debug("Loaded url preview from the cache")
// Put in into the cache for further usage // Put in into the cache for further usage
@ -130,21 +124,8 @@ func makeUrlPreviewHandler(
} }
// Check if there is an active request // Check if there is an active request
activeUrlPreviewRequests.Lock() if response, ok := checkActivePreviewResponse(activeUrlPreviewRequests, pUrl); ok {
if activeUrlPreviewRequest, ok := activeUrlPreviewRequests.Url[pUrl]; ok { return response
activeUrlPreviewRequests.Unlock()
// Wait for it to complete
activeUrlPreviewRequest.Cond.L.Lock()
defer activeUrlPreviewRequest.Cond.L.Unlock()
activeUrlPreviewRequest.Cond.Wait()
if activeUrlPreviewRequest.Error != nil {
return util.ErrorResponse(activeUrlPreviewRequest.Error)
}
return util.JSONResponse{
Code: http.StatusOK,
JSON: activeUrlPreviewRequest.Preview,
}
} }
// Start new url preview request // Start new url preview request
@ -260,6 +241,39 @@ func makeUrlPreviewHandler(
} }
func checkInternalCacheResponse(urlPreviewCache *types.UrlPreviewCache, url string) (util.JSONResponse, bool) {
if cacheRecord, ok := urlPreviewCache.Records[url]; ok {
if cacheRecord.Error != nil {
return util.ErrorResponse(cacheRecord.Error), true
}
return util.JSONResponse{
Code: http.StatusOK,
JSON: cacheRecord.Preview,
}, true
}
return util.JSONResponse{}, false
}
func checkActivePreviewResponse(activeUrlPreviewRequests *types.ActiveUrlPreviewRequests, url string) (util.JSONResponse, bool) {
activeUrlPreviewRequests.Lock()
if activeUrlPreviewRequest, ok := activeUrlPreviewRequests.Url[url]; ok {
activeUrlPreviewRequests.Unlock()
// Wait for it to complete
activeUrlPreviewRequest.Cond.L.Lock()
defer activeUrlPreviewRequest.Cond.L.Unlock()
activeUrlPreviewRequest.Cond.Wait()
if activeUrlPreviewRequest.Error != nil {
return util.ErrorResponse(activeUrlPreviewRequest.Error), true
}
return util.JSONResponse{
Code: http.StatusOK,
JSON: activeUrlPreviewRequest.Preview,
}, true
}
return util.JSONResponse{}, false
}
func downloadUrl(url string, t time.Duration) (*http.Response, error) { func downloadUrl(url string, t time.Duration) (*http.Response, error) {
client := http.Client{Timeout: t} client := http.Client{Timeout: t}
resp, err := client.Get(url) resp, err := client.Get(url)
@ -379,6 +393,8 @@ func downloadAndStoreImage(
hash, activeThumbnailGeneration, cfg.MaxThumbnailGenerators, logger) hash, activeThumbnailGeneration, cfg.MaxThumbnailGenerators, logger)
if err != nil { if err != nil {
if errors.Is(err, thumbnailer.ErrThumbnailTooLarge) { if errors.Is(err, thumbnailer.ErrThumbnailTooLarge) {
// In case the image is smaller than the thumbnail size
// we don't create a thumbnail
thumbnailPath = tmpFileName thumbnailPath = tmpFileName
} else { } else {
return nil, width, height, err return nil, width, height, err
@ -436,10 +452,10 @@ func downloadAndStoreImage(
} }
func createThumbnail(src types.Path, dst types.Path, size types.ThumbnailSize, hash types.Base64Hash, activeThumbnailGeneration *types.ActiveThumbnailGeneration, maxThumbnailGenerators int, logger *log.Entry) (int, int, error) { func createThumbnail(src types.Path, dst types.Path, size types.ThumbnailSize, hash types.Base64Hash, activeThumbnailGeneration *types.ActiveThumbnailGeneration, maxThumbnailGenerators int, logger *log.Entry) (int, int, error) {
// Check if we have too many thumbnail generators running
// If so, wait up to 30 seconds for one to finish
timeout := time.After(30 * time.Second) timeout := time.After(30 * time.Second)
for { for {
// Check if we have too many thumbnail generators running
// If so, wait up to 30 seconds for one to finish
if len(activeThumbnailGeneration.PathToResult) < maxThumbnailGenerators { if len(activeThumbnailGeneration.PathToResult) < maxThumbnailGenerators {
activeThumbnailGeneration.Lock() activeThumbnailGeneration.Lock()