-
-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from mageddo/feature/42
Local Resolution Optimizations
- Loading branch information
Showing
170 changed files
with
143,426 additions
and
275 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
2.1.8 | ||
2.2.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package cache | ||
|
||
type Cache interface { | ||
|
||
Get(key interface{}) interface{} | ||
ContainsKey(key interface{}) bool | ||
Put(key, value interface{}) | ||
PutIfAbsent(key, value interface{}) interface{} | ||
Remove(key interface{}) | ||
Clear() | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package hashicorp | ||
|
||
import "github.com/hashicorp/golang-lru" | ||
|
||
type LRUCache struct { | ||
cache *lru.Cache | ||
} | ||
|
||
func (c *LRUCache) Get(key interface{}) interface{} { | ||
v, _ := c.cache.Get(key) | ||
return v | ||
} | ||
|
||
func (c *LRUCache) Put(key, value interface{}) { | ||
c.cache.Add(key, value) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package lru | ||
|
||
import ( | ||
"github.com/mageddo/dns-proxy-server/cache" | ||
"github.com/hashicorp/golang-lru" | ||
"github.com/mageddo/dns-proxy-server/log" | ||
) | ||
|
||
type LRUCache struct { | ||
Cache *lru.Cache | ||
} | ||
|
||
func (c *LRUCache) ContainsKey(key interface{}) bool { | ||
return c.Cache.Contains(key) | ||
} | ||
|
||
func (c *LRUCache) Get(key interface{}) interface{} { | ||
v, _ := c.Cache.Get(key) | ||
return v | ||
} | ||
|
||
// | ||
// Put value in cache, it doesn't have guarantee of concurrency treat | ||
// | ||
func (c *LRUCache) Put(key, value interface{}) { | ||
c.Cache.Add(key, value) | ||
} | ||
|
||
// | ||
// Check if value is already associated, if yes just return it, if not put the passed value and return nil | ||
// This method must be thread safe (atomic) | ||
// | ||
func (c *LRUCache) PutIfAbsent(key, value interface{}) interface{} { | ||
if ok, _ := c.Cache.ContainsOrAdd(key, value); ok { | ||
return c.Get(key) | ||
} | ||
return nil; | ||
} | ||
|
||
func (c *LRUCache) Clear() { | ||
c.Cache.Purge() | ||
} | ||
|
||
func (c *LRUCache) Remove(key interface{}) { | ||
c.Cache.Remove(key) | ||
} | ||
|
||
// | ||
// Creates a LRU cache | ||
// size is the maximum size of the cache, -1 if it is unlimited | ||
// | ||
func New(size int) cache.Cache { | ||
c, err := lru.New(size) | ||
if err != nil { | ||
log.LOGGER.Errorf("status=cannot-create-cache, msg=%v", err) | ||
return nil; | ||
} | ||
return &LRUCache{c} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package lru | ||
|
||
import ( | ||
"testing" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestPutAndGetSuccess(t *testing.T){ | ||
|
||
cache := New(10); | ||
cache.Put("key1", "value1"); | ||
|
||
assert.Equal(t, "value1", cache.Get("key1").(string)) | ||
|
||
} | ||
|
||
func TestPutAndGetSuccessSizeLimited(t *testing.T){ | ||
|
||
cache := New(2); | ||
cache.Put("key1", "value1"); | ||
cache.Put("key2", "value2"); | ||
cache.Put("key3", "value3"); | ||
|
||
assert.Nil(t, cache.Get("key1")) | ||
assert.Equal(t, "value2", cache.Get("key2").(string)) | ||
assert.Equal(t, "value3", cache.Get("key3").(string)) | ||
|
||
} | ||
|
||
func TestPutAndGeRemovingLeastUsed(t *testing.T){ | ||
|
||
cache := New(3); | ||
|
||
cache.Put("key1", "value1"); | ||
cache.Put("key2", "value2"); | ||
cache.Put("key3", "value3"); | ||
|
||
|
||
cache.Get("key2") | ||
cache.Get("key1") | ||
|
||
cache.Put("key4", "value4"); | ||
|
||
|
||
assert.Equal(t, "value1", cache.Get("key1")) | ||
assert.Equal(t, "value2", cache.Get("key2").(string)) | ||
assert.Nil(t, cache.Get("key3")) | ||
assert.Equal(t, "value4", cache.Get("key4").(string)) | ||
|
||
} | ||
|
||
|
||
func TestPurge(t *testing.T){ | ||
|
||
cache := New(3); | ||
|
||
cache.Put("key1", "value1"); | ||
cache.Put("key2", "value2"); | ||
|
||
assert.Equal(t, "value1", cache.Get("key1")) | ||
assert.Equal(t, "value2", cache.Get("key2")) | ||
|
||
cache.Clear() | ||
|
||
assert.False(t, cache.ContainsKey("key1")) | ||
assert.False(t, cache.ContainsKey("key2")) | ||
|
||
} | ||
|
||
func TestRemove(t *testing.T){ | ||
|
||
cache := New(3); | ||
|
||
cache.Put("key1", "value1"); | ||
cache.Put("key2", "value2"); | ||
|
||
assert.Equal(t, "value1", cache.Get("key1")) | ||
assert.Equal(t, "value2", cache.Get("key2")) | ||
|
||
cache.Remove("key2") | ||
|
||
assert.True(t, cache.ContainsKey("key1")) | ||
assert.False(t, cache.ContainsKey("key2")) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package store | ||
|
||
import ( | ||
"github.com/mageddo/dns-proxy-server/cache/lru" | ||
"github.com/mageddo/dns-proxy-server/cache" | ||
) | ||
|
||
var c cache.Cache; | ||
func init(){ | ||
c = lru.New(43690); // about 1 MB considering HostnameVo struct | ||
} | ||
|
||
// | ||
// Singleton cache | ||
// | ||
func GetInstance() cache.Cache { | ||
return c | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package timed | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
type TimedValue interface { | ||
Creation() time.Time | ||
Timeout() time.Duration | ||
Value() interface{} | ||
|
||
// | ||
// Check if the value has expired | ||
// now current time to be compared with the Creation() | ||
// | ||
IsValid(now time.Time) bool | ||
|
||
} | ||
|
||
type timedValueImpl struct { | ||
creation time.Time | ||
timeout time.Duration | ||
value interface{} | ||
} | ||
|
||
func(t *timedValueImpl) Creation() time.Time { | ||
return t.creation | ||
} | ||
|
||
func(t *timedValueImpl) Timeout() time.Duration { | ||
return t.timeout | ||
} | ||
|
||
func(t *timedValueImpl) Value() interface{} { | ||
return t.value | ||
} | ||
|
||
func(t *timedValueImpl) IsValid(now time.Time) bool { | ||
return t.Timeout() > now.Sub(t.Creation()) | ||
} | ||
|
||
func NewTimedValue(value interface{}, creation time.Time, timeout time.Duration) TimedValue { | ||
return &timedValueImpl{creation:creation, value:value, timeout:timeout} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package timed | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestTimedValueImpl_IsValid(t *testing.T) { | ||
|
||
current1, err := time.Parse("2006-01-02 15:04:05.999", "2017-07-19 17:00:00.300") | ||
assert.Nil(t, err) | ||
|
||
current2, err := time.Parse("2006-01-02 15:04:05.999", "2017-07-19 17:00:00.700") | ||
assert.Nil(t, err) | ||
|
||
expiredTime, err := time.Parse("2006-01-02 15:04:05.999", "2017-07-19 17:00:00.900") | ||
assert.Nil(t, err) | ||
|
||
assert.True(t, NewTimedValue(1, current1, time.Duration(500 * time.Millisecond)).IsValid(current2)) | ||
assert.False(t, NewTimedValue(1, current1, time.Duration(500 * time.Millisecond)).IsValid(expiredTime)) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.