diff options
| author | Shulhan <ms@kilabit.info> | 2026-01-22 00:27:18 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2026-01-22 00:27:18 +0700 |
| commit | 26fc8bd3203dae6b4705ada227439c90129bbe36 (patch) | |
| tree | 26ed784cdf2779c3a0848fbf6c1355094b216eea /cache.go | |
| parent | 6637e9d11a57c67510d79c00a1425f5e66d18280 (diff) | |
| download | jarink-26fc8bd3203dae6b4705ada227439c90129bbe36.tar.xz | |
brokenlinks: refactoring the logic, simplify the code
Previously, we made the scan logic to run in multiple goroutine with
one channel to push and consume the result and another channel to push
and pop link to be processed.
The logic is a very complicated code, making it hard to read and debug.
These changes refactoring it to use single goroutine that push and pop
link from/to a slices, as queue.
Diffstat (limited to 'cache.go')
| -rw-r--r-- | cache.go | 17 |
1 files changed, 5 insertions, 12 deletions
@@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2025 M. Shulhan <ms@kilabit.info> // SPDX-License-Identifier: GPL-3.0-only +// SPDX-FileCopyrightText: 2025 M. Shulhan <ms@kilabit.info> package jarink @@ -12,13 +12,6 @@ import ( "git.sr.ht/~shulhan/jarink/internal" ) -// ScannedLink store information about the link. -type ScannedLink struct { - Url string `json:"url"` - Size int64 `json:"size"` - ResponseCode int `json:"response_code"` -} - // Cache store external links that has been scanned, to minize // request to the same URL in the future. // The cache is stored as JSON file under user's cache directory, inside @@ -26,7 +19,7 @@ type ScannedLink struct { // For example, in Linux it should be "$HOME/.cache/jarink/cache.json". // See [os.UserCacheDir] for location specific to operating system. type Cache struct { - ScannedLinks map[string]*ScannedLink `json:"scanned_links"` + ScannedLinks map[string]*Link `json:"scanned_links"` file string mtx sync.Mutex } @@ -36,7 +29,7 @@ func LoadCache() (cache *Cache, err error) { var logp = `LoadCache` cache = &Cache{ - ScannedLinks: map[string]*ScannedLink{}, + ScannedLinks: map[string]*Link{}, } cache.file, err = internal.CacheFile() @@ -62,7 +55,7 @@ func LoadCache() (cache *Cache, err error) { } // Get return the scanned link information by url. -func (cache *Cache) Get(url string) (scannedLink *ScannedLink) { +func (cache *Cache) Get(url string) (scannedLink *Link) { cache.mtx.Lock() scannedLink = cache.ScannedLinks[url] cache.mtx.Unlock() @@ -95,7 +88,7 @@ func (cache *Cache) Set(url string, respCode int, size int64) { if scannedLink != nil { return } - scannedLink = &ScannedLink{ + scannedLink = &Link{ Url: url, Size: size, ResponseCode: respCode, |
