Skip to content

Commit

Permalink
Limit cache size
Browse files Browse the repository at this point in the history
  • Loading branch information
mike76-dev committed May 10, 2024
1 parent 5950646 commit 135dd7d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ ldflags= \
-X "github.com/mike76-dev/hostscore/internal/build.NodeBinaryName=hsd" \
-X "github.com/mike76-dev/hostscore/internal/build.NodeVersion=1.1.3" \
-X "github.com/mike76-dev/hostscore/internal/build.ClientBinaryName=hsc" \
-X "github.com/mike76-dev/hostscore/internal/build.ClientVersion=1.4.1" \
-X "github.com/mike76-dev/hostscore/internal/build.ClientVersion=1.4.2" \
-X "github.com/mike76-dev/hostscore/internal/build.GitRevision=${GIT_DIRTY}${GIT_REVISION}" \
-X "github.com/mike76-dev/hostscore/internal/build.BuildTime=${BUILD_TIME}"

Expand Down
20 changes: 16 additions & 4 deletions cmd/hsc/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

const (
hostsExpireThreshold = 10 * time.Minute
hostsRequestsLimit = 100
cachedHostsLimit = 2000
)

type cachedHosts struct {
Expand All @@ -29,6 +29,7 @@ type cachedHosts struct {

type responseCache struct {
hosts []cachedHosts
count int
mu sync.Mutex
stopChan chan struct{}
}
Expand All @@ -54,13 +55,15 @@ func (rc *responseCache) prune() {
}
rc.mu.Lock()
i := 0
rc.count = 0
for {
if i >= len(rc.hosts) {
break
}
if time.Since(rc.hosts[i].modified) > hostsExpireThreshold {
rc.hosts = append(rc.hosts[:i], rc.hosts[i+1:]...)
} else {
rc.count += len(rc.hosts[i].hosts)
i++
}
}
Expand Down Expand Up @@ -108,8 +111,20 @@ func (rc *responseCache) getHosts(network string, all bool, offset, limit int, q
}

func (rc *responseCache) putHosts(network string, all bool, offset, limit int, query, country string, sortBy sortType, asc bool, hosts []portalHost, more bool, total int) {
if len(hosts) > cachedHostsLimit {
return
}
rc.mu.Lock()
defer rc.mu.Unlock()
rc.count += len(hosts)
for rc.count > cachedHostsLimit {
rc.count -= len(rc.hosts[0].hosts)
if len(rc.hosts) > 0 {
rc.hosts = rc.hosts[1:]
} else {
rc.hosts = nil
}
}
rc.hosts = append(rc.hosts, cachedHosts{
hosts: hosts,
more: more,
Expand All @@ -124,7 +139,4 @@ func (rc *responseCache) putHosts(network string, all bool, offset, limit int, q
asc: asc,
modified: time.Now(),
})
if len(rc.hosts) > hostsRequestsLimit {
rc.hosts = rc.hosts[1:]
}
}

0 comments on commit 135dd7d

Please sign in to comment.