Skip to content

Commit

Permalink
Re-enabled the cache and added a mutex to avoid concurrent writes
Browse files Browse the repository at this point in the history
  • Loading branch information
pieterclaerhout committed May 22, 2020
1 parent 2e75f2c commit 69c1c4c
Show file tree
Hide file tree
Showing 19 changed files with 72 additions and 74 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ FROM mod-download AS builder
ADD . /app
WORKDIR /app

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath -a --ldflags '-extldflags -static' -o geoip-server github.com/pieterclaerhout/go-geoip/cmd/geoip-server
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath -a --ldflags '-extldflags -static' -o geoip-server github.com/pieterclaerhout/go-geoip/v2/cmd/geoip-server

# STAGE 3 - FINAL

Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ init:
@mkdir -p build

build-server: init
@go build -trimpath -ldflags "-s -w" -o build/geoip-server github.com/pieterclaerhout/go-geoip/cmd/geoip-server
@go build -trimpath -ldflags "-s -w" -o build/geoip-server github.com/pieterclaerhout/go-geoip/v2/cmd/geoip-server

build-db-downloader: init
@go build -trimpath -ldflags "-s -w" -o build/db-downloader github.com/pieterclaerhout/go-geoip/cmd/db-downloader
@go build -trimpath -ldflags "-s -w" -o build/db-downloader github.com/pieterclaerhout/go-geoip/v2/cmd/db-downloader

build-docker-image:
docker build -t geoip-server .
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ import (
"fmt"
"os"
"time"
"github.com/pieterclaerhout/go-geoip"

geoip "github.com/pieterclaerhout/go-geoip/v2"
)

func main() {
Expand Down
32 changes: 16 additions & 16 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@ import (

// Client is used to get the results from the server API
type Client struct {
url string
timeout time.Duration
// lookupCache map[string]*IPLocation
url string
timeout time.Duration
lookupCache map[string]*IPLocation
}

// NewClient returns a new Client instance with the given URL
func NewClient(url string, timeout time.Duration) *Client {
return &Client{
url: url,
timeout: timeout,
// lookupCache: map[string]*IPLocation{},
url: url,
timeout: timeout,
lookupCache: map[string]*IPLocation{},
}
}

// // ClearCache clears the cache for the lookups
// func (client *Client) ClearCache() {
// client.lookupCache = map[string]*IPLocation{}
// }
// ClearCache clears the cache for the lookups
func (client *Client) ClearCache() {
client.lookupCache = map[string]*IPLocation{}
}

// Lookup returns the full country information for a specific IP address
func (client *Client) Lookup(ipaddress string) (*IPLocation, error) {
Expand All @@ -37,10 +37,10 @@ func (client *Client) Lookup(ipaddress string) (*IPLocation, error) {
Error string `json:"error"`
}

// if location, cached := client.lookupCache[ipaddress]; cached {
// location.IsCached = true
// return location, nil
// }
if location, cached := client.lookupCache[ipaddress]; cached {
location.IsCached = true
return location, nil
}

params := url.Values{}
params.Add("ip", ipaddress)
Expand Down Expand Up @@ -75,9 +75,9 @@ func (client *Client) Lookup(ipaddress string) (*IPLocation, error) {
return nil, err
}

// location.IsCached = false
location.IsCached = false

// client.lookupCache[ipaddress] = location
client.lookupCache[ipaddress] = location

return location, nil

Expand Down
14 changes: 7 additions & 7 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"testing"
"time"

"github.com/pieterclaerhout/go-geoip"
"github.com/pieterclaerhout/go-geoip/v2"
"github.com/stretchr/testify/assert"
)

Expand All @@ -22,7 +22,7 @@ func TestClientLookup(t *testing.T) {
assert.NoError(t, err)
assert.NotNil(t, actual)
assert.Equal(t, "1.1.1.1", actual.IPAddress, "ipaddress")
// assert.Equal(t, false, actual.IsCached, "is-cached")
assert.Equal(t, false, actual.IsCached, "is-cached")

}

Expand Down Expand Up @@ -109,25 +109,25 @@ func TestClientLookupCache(t *testing.T) {
s := testServer(t)

client := geoip.NewClient(s.URL, 250*time.Millisecond)
// client.ClearCache()
client.ClearCache()

input := "213.118.8.79"

nonCached, err := client.Lookup(input)
assert.NotNil(t, nonCached, "nonCached")
// assert.False(t, nonCached.IsCached, "nonCached.IsCached")
assert.False(t, nonCached.IsCached, "nonCached.IsCached")
assert.NoErrorf(t, err, "nonCached")

cached, err := client.Lookup(input)
assert.NotNil(t, cached, "cached")
// assert.True(t, cached.IsCached, "cached.IsCached")
assert.True(t, cached.IsCached, "cached.IsCached")
assert.NoErrorf(t, err, "cached")

// client.ClearCache()
client.ClearCache()

clearCached, err := client.Lookup(input)
assert.NotNil(t, clearCached, "clearCached")
// assert.False(t, clearCached.IsCached, "clearCached.IsCached")
assert.False(t, clearCached.IsCached, "clearCached.IsCached")
assert.NoErrorf(t, err, "clearCached")

}
Expand Down
2 changes: 1 addition & 1 deletion cmd/db-downloader/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"os"
"time"

"github.com/pieterclaerhout/go-geoip"
"github.com/pieterclaerhout/go-geoip/v2"
"github.com/pieterclaerhout/go-log"
)

Expand Down
2 changes: 1 addition & 1 deletion cmd/geoip-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"path/filepath"

"github.com/joho/godotenv"
"github.com/pieterclaerhout/go-geoip/serverapp"
"github.com/pieterclaerhout/go-geoip/v2/serverapp"
"github.com/pieterclaerhout/go-log"
webserver "github.com/pieterclaerhout/go-webserver/v2"
)
Expand Down
1 change: 0 additions & 1 deletion countries.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,6 @@ func CountryNameToCode(name string) (string, error) {
return "", errors.New("Unknown country name: " + name)
}


// sliceContainsString checks if the slice contains the given string in a case-sensitive way
func sliceContainsString(slice []string, item string) bool {
set := make(map[string]struct{}, len(slice))
Expand Down
2 changes: 1 addition & 1 deletion countries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package geoip_test
import (
"testing"

"github.com/pieterclaerhout/go-geoip"
"github.com/pieterclaerhout/go-geoip/v2"
"github.com/stretchr/testify/assert"
)

Expand Down
32 changes: 18 additions & 14 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,39 @@ package geoip

import (
"net"
"sync"

"github.com/oschwald/maxminddb-golang"
"github.com/pkg/errors"
)

// Database is the main wrapper around the MaxMind GeoIP database
type Database struct {
path string
// lookupCache map[string]*IPLocation
path string
lookupCache map[string]*IPLocation
lookupCacheMutex sync.Mutex
}

// NewDatabase returns a new Database instance with the given database path
func NewDatabase(path string) *Database {
return &Database{
path: path,
// lookupCache: map[string]*IPLocation{},
path: path,
lookupCache: map[string]*IPLocation{},
}
}

// // ClearCache clears the cache for the lookups
// func (database *Database) ClearCache() {
// database.lookupCache = map[string]*IPLocation{}
// }
// ClearCache clears the cache for the lookups
func (database *Database) ClearCache() {
database.lookupCache = map[string]*IPLocation{}
}

// Lookup returns the full country information for a specific IP address
func (database *Database) Lookup(ipaddress string) (*IPLocation, error) {

// if location, cached := database.lookupCache[ipaddress]; cached {
// location.IsCached = true
// return location, nil
// }
if location, cached := database.lookupCache[ipaddress]; cached {
location.IsCached = true
return location, nil
}

var location *IPLocation
var record interface{}
Expand All @@ -57,9 +59,11 @@ func (database *Database) Lookup(ipaddress string) (*IPLocation, error) {
}

location.IPAddress = ipaddress
// location.IsCached = false
location.IsCached = false

// database.lookupCache[ipaddress] = location
database.lookupCacheMutex.Lock()
defer database.lookupCacheMutex.Unlock()
database.lookupCache[ipaddress] = location

return location, nil

Expand Down
2 changes: 1 addition & 1 deletion database_downloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"time"

"github.com/Flaque/filet"
"github.com/pieterclaerhout/go-geoip"
"github.com/pieterclaerhout/go-geoip/v2"
"github.com/stretchr/testify/assert"
)

Expand Down
16 changes: 8 additions & 8 deletions database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"testing"
"time"

"github.com/pieterclaerhout/go-geoip"
"github.com/pieterclaerhout/go-geoip/v2"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -42,7 +42,7 @@ func TestDatabaseLookup(t *testing.T) {

t.Run(tc.input, func(t *testing.T) {

// db.ClearCache()
db.ClearCache()

actual, err := db.Lookup(tc.input)

Expand Down Expand Up @@ -91,7 +91,7 @@ func TestDatabaseCountryCode(t *testing.T) {

t.Run(tc.input, func(t *testing.T) {

// db.ClearCache()
db.ClearCache()

actual, err := db.CountryCode(tc.input)

Expand Down Expand Up @@ -138,7 +138,7 @@ func TestDatabaseCountryName(t *testing.T) {

t.Run(tc.input, func(t *testing.T) {

// db.ClearCache()
db.ClearCache()

actual, err := db.CountryName(tc.input)

Expand Down Expand Up @@ -185,7 +185,7 @@ func TestDatabaseRegionName(t *testing.T) {

t.Run(tc.input, func(t *testing.T) {

// db.ClearCache()
db.ClearCache()

actual, err := db.RegionName(tc.input)

Expand Down Expand Up @@ -233,7 +233,7 @@ func TestDatabaseTimeZone(t *testing.T) {

t.Run(tc.input, func(t *testing.T) {

// db.ClearCache()
db.ClearCache()

actual, err := db.TimeZone(tc.input)

Expand Down Expand Up @@ -264,7 +264,7 @@ func TestDatabaseDatabaseWithPath_Invalid(t *testing.T) {
func TestDatabaseLookupCache(t *testing.T) {

db := openTestDatabase(t)
// db.ClearCache()
db.ClearCache()

input := "213.118.8.79"

Expand All @@ -278,7 +278,7 @@ func TestDatabaseLookupCache(t *testing.T) {
// assert.True(t, cached.IsCached, "cached.IsCached")
assert.NoErrorf(t, err, "cached")

// db.ClearCache()
db.ClearCache()

clearCached, err := db.Lookup(input)
assert.NotNil(t, clearCached, "clearCached")
Expand Down
4 changes: 2 additions & 2 deletions go-james.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"version": "1.1.0",
"description": "",
"copyright": "",
"package": "github.com/pieterclaerhout/go-geoip",
"main_package": "github.com/pieterclaerhout/go-geoip/cmd/geoip-server"
"package": "github.com/pieterclaerhout/go-geoip/v2",
"main_package": "github.com/pieterclaerhout/go-geoip/v2/cmd/geoip-server"
},
"build": {
"output_path": "build",
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
module github.com/pieterclaerhout/go-geoip
module github.com/pieterclaerhout/go-geoip/v2

go 1.14

require (
github.com/Flaque/filet v0.0.0-20190209224823-fc4d33cfcf93
github.com/go-chi/chi v4.1.1+incompatible
github.com/joho/godotenv v1.3.0
github.com/labstack/echo v3.3.10+incompatible
github.com/oschwald/maxminddb-golang v1.6.0
github.com/pieterclaerhout/go-log v1.11.0
github.com/pieterclaerhout/go-webserver v1.0.6
github.com/pieterclaerhout/go-webserver/v2 v2.0.1
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.5.1
)
Loading

0 comments on commit 69c1c4c

Please sign in to comment.