mirror of
https://github.com/mpolden/echoip.git
synced 2025-01-27 02:15:01 +01:00
cache: Copy values
This commit is contained in:
parent
6878f54585
commit
971c0e11f4
@ -9,7 +9,7 @@ import (
|
|||||||
type Cache struct {
|
type Cache struct {
|
||||||
capacity int
|
capacity int
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
entries map[uint64]*Response
|
entries map[uint64]Response
|
||||||
keys []uint64
|
keys []uint64
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -19,7 +19,7 @@ func NewCache(capacity int) *Cache {
|
|||||||
}
|
}
|
||||||
return &Cache{
|
return &Cache{
|
||||||
capacity: capacity,
|
capacity: capacity,
|
||||||
entries: make(map[uint64]*Response),
|
entries: make(map[uint64]Response),
|
||||||
keys: make([]uint64, 0, capacity),
|
keys: make([]uint64, 0, capacity),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -30,7 +30,7 @@ func key(ip net.IP) uint64 {
|
|||||||
return h.Sum64()
|
return h.Sum64()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Cache) Set(ip net.IP, resp *Response) {
|
func (c *Cache) Set(ip net.IP, resp Response) {
|
||||||
if c.capacity == 0 {
|
if c.capacity == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -45,7 +45,7 @@ func (c *Cache) Set(ip net.IP, resp *Response) {
|
|||||||
c.keys = append(c.keys, k)
|
c.keys = append(c.keys, k)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Cache) Get(ip net.IP) (*Response, bool) {
|
func (c *Cache) Get(ip net.IP) (Response, bool) {
|
||||||
k := key(ip)
|
k := key(ip)
|
||||||
c.mu.RLock()
|
c.mu.RLock()
|
||||||
defer c.mu.RUnlock()
|
defer c.mu.RUnlock()
|
||||||
|
@ -4,24 +4,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"testing"
|
"testing"
|
||||||
"unsafe"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCache(t *testing.T) {
|
|
||||||
|
|
||||||
c := NewCache(10)
|
|
||||||
|
|
||||||
for i := 0; i < 100; i++ {
|
|
||||||
ip := net.ParseIP(fmt.Sprintf("192.0.2.%d", i))
|
|
||||||
r := &Response{IP: ip}
|
|
||||||
fmt.Println(unsafe.Sizeof(r))
|
|
||||||
c.Set(ip, r)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println(len(c.entries))
|
|
||||||
fmt.Println(len(c.keys))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCacheCapacity(t *testing.T) {
|
func TestCacheCapacity(t *testing.T) {
|
||||||
var tests = []struct {
|
var tests = []struct {
|
||||||
addCount, capacity, size int
|
addCount, capacity, size int
|
||||||
@ -33,10 +17,10 @@ func TestCacheCapacity(t *testing.T) {
|
|||||||
}
|
}
|
||||||
for i, tt := range tests {
|
for i, tt := range tests {
|
||||||
c := NewCache(tt.capacity)
|
c := NewCache(tt.capacity)
|
||||||
var responses []*Response
|
var responses []Response
|
||||||
for i := 0; i < tt.addCount; i++ {
|
for i := 0; i < tt.addCount; i++ {
|
||||||
ip := net.ParseIP(fmt.Sprintf("192.0.2.%d", i))
|
ip := net.ParseIP(fmt.Sprintf("192.0.2.%d", i))
|
||||||
r := &Response{IP: ip}
|
r := Response{IP: ip}
|
||||||
responses = append(responses, r)
|
responses = append(responses, r)
|
||||||
c.Set(ip, r)
|
c.Set(ip, r)
|
||||||
}
|
}
|
||||||
|
11
http/http.go
11
http/http.go
@ -7,10 +7,11 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"net/http/pprof"
|
||||||
|
|
||||||
"github.com/mpolden/echoip/iputil"
|
"github.com/mpolden/echoip/iputil"
|
||||||
"github.com/mpolden/echoip/iputil/geo"
|
"github.com/mpolden/echoip/iputil/geo"
|
||||||
"github.com/mpolden/echoip/useragent"
|
"github.com/mpolden/echoip/useragent"
|
||||||
"net/http/pprof"
|
|
||||||
|
|
||||||
"math/big"
|
"math/big"
|
||||||
"net"
|
"net"
|
||||||
@ -125,9 +126,9 @@ func (s *Server) newResponse(r *http.Request) (Response, error) {
|
|||||||
}
|
}
|
||||||
response, ok := s.cache.Get(ip)
|
response, ok := s.cache.Get(ip)
|
||||||
if ok {
|
if ok {
|
||||||
// Not Caching the userAgent as it can vary for a given IP
|
// Do not cache user agent
|
||||||
response.UserAgent = userAgentFromRequest(r)
|
response.UserAgent = userAgentFromRequest(r)
|
||||||
return *response, nil
|
return response, nil
|
||||||
}
|
}
|
||||||
ipDecimal := iputil.ToDecimal(ip)
|
ipDecimal := iputil.ToDecimal(ip)
|
||||||
country, _ := s.gr.Country(ip)
|
country, _ := s.gr.Country(ip)
|
||||||
@ -141,7 +142,7 @@ func (s *Server) newResponse(r *http.Request) (Response, error) {
|
|||||||
if asn.AutonomousSystemNumber > 0 {
|
if asn.AutonomousSystemNumber > 0 {
|
||||||
autonomousSystemNumber = fmt.Sprintf("AS%d", asn.AutonomousSystemNumber)
|
autonomousSystemNumber = fmt.Sprintf("AS%d", asn.AutonomousSystemNumber)
|
||||||
}
|
}
|
||||||
response = &Response{
|
response = Response{
|
||||||
IP: ip,
|
IP: ip,
|
||||||
IPDecimal: ipDecimal,
|
IPDecimal: ipDecimal,
|
||||||
Country: country.Name,
|
Country: country.Name,
|
||||||
@ -161,7 +162,7 @@ func (s *Server) newResponse(r *http.Request) (Response, error) {
|
|||||||
}
|
}
|
||||||
s.cache.Set(ip, response)
|
s.cache.Set(ip, response)
|
||||||
response.UserAgent = userAgentFromRequest(r)
|
response.UserAgent = userAgentFromRequest(r)
|
||||||
return *response, nil
|
return response, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) newPortResponse(r *http.Request) (PortResponse, error) {
|
func (s *Server) newPortResponse(r *http.Request) (PortResponse, error) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user