Compare commits
No commits in common. "main" and "v0.7.2" have entirely different histories.
@ -4,10 +4,10 @@ import (
|
|||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.tijl.dev/tijl/shortify/pkg/generation"
|
"git.tijl.dev/tijl/shortify/pkg/generation"
|
||||||
lru "github.com/hashicorp/golang-lru"
|
|
||||||
bolt "go.etcd.io/bbolt"
|
bolt "go.etcd.io/bbolt"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -22,9 +22,10 @@ type Client struct {
|
|||||||
stopRetry chan struct{}
|
stopRetry chan struct{}
|
||||||
|
|
||||||
// In-memory cache
|
// In-memory cache
|
||||||
cacheMap *lru.Cache
|
cacheMap map[string]string // longURL -> shortID
|
||||||
maxCacheSize int
|
cacheLock sync.RWMutex
|
||||||
maxCacheInitialLoad int
|
maxCacheSize int
|
||||||
|
cacheCount int
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewClient with persistence and retry queue
|
// NewClient with persistence and retry queue
|
||||||
@ -43,16 +44,13 @@ func NewClient(serverURL string, folder string) (*Client, error) {
|
|||||||
serverURL: baseURL,
|
serverURL: baseURL,
|
||||||
httpClient: httpClient,
|
httpClient: httpClient,
|
||||||
db: db,
|
db: db,
|
||||||
retryQueue: make(chan shortenJob, 100000),
|
retryQueue: make(chan shortenJob, 1000),
|
||||||
stopRetry: make(chan struct{}),
|
stopRetry: make(chan struct{}),
|
||||||
}
|
}
|
||||||
|
|
||||||
cli.maxCacheSize = 100000 // or make this configurable
|
cli.cacheMap = make(map[string]string)
|
||||||
cli.maxCacheInitialLoad = 10000
|
cli.maxCacheSize = 10000 // or make this configurable
|
||||||
cli.cacheMap, err = lru.New(cli.maxCacheSize)
|
cli.cacheCount = 0
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create buckets if not exist
|
// Create buckets if not exist
|
||||||
err = db.Update(func(tx *bolt.Tx) error {
|
err = db.Update(func(tx *bolt.Tx) error {
|
||||||
@ -120,10 +118,9 @@ func NewClient(serverURL string, folder string) (*Client, error) {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
c := b.Cursor()
|
c := b.Cursor()
|
||||||
initalCounter := 0
|
for k, v := c.First(); k != nil && cli.cacheCount < cli.maxCacheSize; k, v = c.Next() {
|
||||||
for k, v := c.First(); k != nil && initalCounter < cli.maxCacheInitialLoad; k, v = c.Next() {
|
cli.cacheMap[string(k)] = string(v)
|
||||||
cli.cacheMap.Add(string(k), string(v))
|
cli.cacheCount++
|
||||||
initalCounter++
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
@ -161,9 +158,12 @@ func (c *Client) Shorten(longURL string, opts ...ShortenOpt) string {
|
|||||||
|
|
||||||
// Check memory cache
|
// Check memory cache
|
||||||
if options.useCache {
|
if options.useCache {
|
||||||
if shortID, ok := c.cacheMap.Get(longURL); ok {
|
c.cacheLock.RLock()
|
||||||
return shortID.(string)
|
if shortID, ok := c.cacheMap[longURL]; ok {
|
||||||
|
c.cacheLock.RUnlock()
|
||||||
|
return shortID
|
||||||
}
|
}
|
||||||
|
c.cacheLock.RUnlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate new ID
|
// Generate new ID
|
||||||
@ -184,7 +184,12 @@ func (c *Client) Shorten(longURL string, opts ...ShortenOpt) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) addToCache(longURL, shortID string) {
|
func (c *Client) addToCache(longURL, shortID string) {
|
||||||
c.cacheMap.Add(longURL, shortID)
|
c.cacheLock.Lock()
|
||||||
|
if _, exists := c.cacheMap[longURL]; !exists && c.cacheCount < c.maxCacheSize {
|
||||||
|
c.cacheMap[longURL] = shortID
|
||||||
|
c.cacheCount++
|
||||||
|
}
|
||||||
|
c.cacheLock.Unlock()
|
||||||
|
|
||||||
// Async write to BoltDB
|
// Async write to BoltDB
|
||||||
go func() {
|
go func() {
|
||||||
@ -194,15 +199,3 @@ func (c *Client) addToCache(longURL, shortID string) {
|
|||||||
})
|
})
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) remFromCache(longURL string) {
|
|
||||||
c.cacheMap.Remove(longURL)
|
|
||||||
|
|
||||||
// Async write to BoltDB
|
|
||||||
go func() {
|
|
||||||
_ = c.db.Update(func(tx *bolt.Tx) error {
|
|
||||||
b := tx.Bucket([]byte("url_cache"))
|
|
||||||
return b.Delete([]byte(longURL))
|
|
||||||
})
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
@ -65,7 +65,6 @@ func (c *Client) retryWorker() {
|
|||||||
case job := <-c.retryQueue:
|
case job := <-c.retryQueue:
|
||||||
err := c.sendShortenJob(job)
|
err := c.sendShortenJob(job)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicln("got error sending shorten job to server", err)
|
|
||||||
// Re-enqueue with delay
|
// Re-enqueue with delay
|
||||||
go func(j shortenJob) {
|
go func(j shortenJob) {
|
||||||
time.Sleep(2 * time.Second)
|
time.Sleep(2 * time.Second)
|
||||||
@ -96,8 +95,7 @@ func (c *Client) enqueueJob(job shortenJob) {
|
|||||||
select {
|
select {
|
||||||
case c.retryQueue <- job:
|
case c.retryQueue <- job:
|
||||||
default:
|
default:
|
||||||
log.Println("Retry queue full, dropping job and removing from caches:", job.ID, job.URL)
|
log.Println("Retry queue full, dropping job:", job.ID)
|
||||||
go c.remFromCache(job.URL)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user