Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
3ca245dadb | |||
6f0e883bff | |||
5fc1b55d52 |
@ -43,16 +43,16 @@ func NewClient(serverURL string, folder string) (*Client, error) {
|
||||
serverURL: baseURL,
|
||||
httpClient: httpClient,
|
||||
db: db,
|
||||
retryQueue: make(chan shortenJob, 1000),
|
||||
retryQueue: make(chan shortenJob, 100000),
|
||||
stopRetry: make(chan struct{}),
|
||||
}
|
||||
|
||||
cli.maxCacheSize = 100000 // or make this configurable
|
||||
cli.maxCacheInitialLoad = 10000
|
||||
cli.cacheMap, err = lru.New(cli.maxCacheSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cli.maxCacheSize = 100000 // or make this configurable
|
||||
cli.maxCacheInitialLoad = 10000
|
||||
|
||||
// Create buckets if not exist
|
||||
err = db.Update(func(tx *bolt.Tx) error {
|
||||
@ -194,3 +194,15 @@ 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,6 +65,7 @@ func (c *Client) retryWorker() {
|
||||
case job := <-c.retryQueue:
|
||||
err := c.sendShortenJob(job)
|
||||
if err != nil {
|
||||
log.Panicln("got error sending shorten job to server", err)
|
||||
// Re-enqueue with delay
|
||||
go func(j shortenJob) {
|
||||
time.Sleep(2 * time.Second)
|
||||
@ -95,7 +96,8 @@ func (c *Client) enqueueJob(job shortenJob) {
|
||||
select {
|
||||
case c.retryQueue <- job:
|
||||
default:
|
||||
log.Println("Retry queue full, dropping job:", job.ID)
|
||||
log.Println("Retry queue full, dropping job and removing from caches:", job.ID, job.URL)
|
||||
go c.remFromCache(job.URL)
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user