Skip to content

Commit 4c4bc14

Browse files
committed
wip go go agent
1 parent 5f91e4c commit 4c4bc14

File tree

23 files changed

+2120
-0
lines changed

23 files changed

+2120
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@
1313

1414
# Dependency directories (remove the comment below to include it)
1515
# vendor/
16+
17+
*.iml

go.mod

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module github.com/scouter-contrib/scouter-agent-golang
2+
3+
go 1.15
4+
5+
require (
6+
github.com/emirpasic/gods v1.12.0
7+
github.com/scouter-project/scouter-go-lib v0.0.0
8+
)
9+
10+
replace github.com/scouter-project/scouter-go-lib => /Users/gunlee/Documents/workspace/gunlee01/scouter-go-lib

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg=
3+
github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
6+
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
7+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
8+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

scouterx/agent.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package scouterx
2+
3+
import (
4+
"sync"
5+
)
6+
7+
var agentOnce sync.Once
8+
9+
func Init() {
10+
agentOnce.Do(func() {
11+
//confDir := "./nil"
12+
//dir, err := os.Getwd()
13+
//if err == nil {
14+
// confDir = dir
15+
//}
16+
//UdpSender = udpsender.GetInstance()
17+
})
18+
}
19+
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package cachemap
2+
3+
import (
4+
"github.com/emirpasic/gods/lists/singlylinkedlist"
5+
"strconv"
6+
"sync"
7+
)
8+
9+
var lock sync.RWMutex
10+
11+
type CacheMap struct {
12+
table map[interface{}]interface{}
13+
ordering *singlylinkedlist.List
14+
orderPos map[interface{}]int
15+
maxSize int
16+
}
17+
18+
func New(maxSize int) *CacheMap {
19+
m := &CacheMap{
20+
table: make(map[interface{}]interface{}),
21+
ordering: singlylinkedlist.New(),
22+
orderPos: make(map[interface{}]int),
23+
maxSize: maxSize,
24+
}
25+
return m
26+
}
27+
28+
func (m *CacheMap) Add(key interface{}, item interface{}) {
29+
lock.Lock()
30+
defer lock.Unlock()
31+
if _, contains := m.table[key]; !contains {
32+
m.removeExceeded()
33+
m.table[key] = item
34+
m.ordering.Append(key)
35+
m.orderPos[key] = m.ordering.Size() - 1
36+
}
37+
}
38+
39+
func (m *CacheMap) Remove(key interface{}) {
40+
lock.Lock()
41+
defer lock.Unlock()
42+
delete(m.table, key)
43+
index, contains := m.orderPos[key]
44+
if contains {
45+
m.ordering.Remove(index)
46+
delete(m.orderPos, key)
47+
}
48+
}
49+
50+
func (m *CacheMap) removeExceeded() {
51+
for m.ordering.Size() >= m.maxSize {
52+
key, exist := m.ordering.Get(0)
53+
if exist {
54+
m.ordering.Remove(0)
55+
delete(m.orderPos, key)
56+
delete(m.table, key)
57+
}
58+
}
59+
}
60+
61+
func (m *CacheMap) Contains(key interface{}) bool {
62+
lock.RLock()
63+
defer lock.RUnlock()
64+
if _, contains := m.table[key]; !contains {
65+
return false
66+
}
67+
return true
68+
}
69+
70+
func (m *CacheMap) Get(key interface{}) interface{} {
71+
lock.RLock()
72+
defer lock.RUnlock()
73+
return m.table[key]
74+
}
75+
76+
func (m *CacheMap) Empty() bool {
77+
lock.Lock()
78+
defer lock.Unlock()
79+
return m.Size() == 0
80+
}
81+
82+
func (m *CacheMap) Size() int {
83+
return m.ordering.Size()
84+
}
85+
86+
func (m *CacheMap) Clear() {
87+
lock.Lock()
88+
defer lock.Unlock()
89+
m.table = make(map[interface{}]interface{})
90+
m.ordering.Clear()
91+
m.orderPos = make(map[interface{}]int)
92+
}
93+
94+
func (m *CacheMap) String() string {
95+
return "CacheMap[" + strconv.Itoa(m.ordering.Size()) + "]"
96+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package cachemap
2+
3+
import (
4+
"strconv"
5+
"testing"
6+
)
7+
8+
func TestCacheSet(t *testing.T) {
9+
cacheMap := New(10)
10+
11+
//add "1" to "9"
12+
for i := 0; i < 10; i++ {
13+
cacheMap.Add(i, strconv.Itoa(i))
14+
}
15+
16+
if cacheMap.Size() != 10 {
17+
t.Error("size error.")
18+
}
19+
20+
//max size preserved
21+
cacheMap.Add(3, "3")
22+
if cacheMap.Size() != 10 {
23+
t.Error("size error.")
24+
}
25+
26+
//0 exist
27+
if (!cacheMap.Contains(0)) {
28+
t.Error("contains error.")
29+
}
30+
31+
//0 removed
32+
cacheMap.Add(1000, "1000")
33+
if cacheMap.Size() != 10 {
34+
t.Error("size error.")
35+
}
36+
37+
if cacheMap.Contains(0) {
38+
t.Error("contains error: 0 was removed.")
39+
}
40+
41+
//1 exist
42+
if !cacheMap.Contains(1) {
43+
t.Error("contains error: 1 should be exist.")
44+
}
45+
46+
//1 removed
47+
cacheMap.Add(1001, "1001")
48+
if cacheMap.Size() != 10 {
49+
t.Error("size error.")
50+
}
51+
52+
if cacheMap.Contains(1) {
53+
t.Error("contains error: 1 was removed.")
54+
}
55+
56+
if !cacheMap.Contains(1001) {
57+
t.Error("contains error: 1001 should be exist.")
58+
}
59+
60+
if cacheMap.Get(1001) != "1001" {
61+
t.Error("no matched value.")
62+
}
63+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package cacheset
2+
3+
import (
4+
"github.com/emirpasic/gods/lists/singlylinkedlist"
5+
"strconv"
6+
"sync"
7+
)
8+
9+
var lock sync.RWMutex
10+
11+
type CacheSet struct {
12+
table map[interface{}]struct{}
13+
ordering *singlylinkedlist.List
14+
maxSize int
15+
}
16+
17+
var itemExists = struct{}{}
18+
19+
func New(maxSize int) *CacheSet {
20+
set := &CacheSet{
21+
table: make(map[interface{}]struct{}),
22+
ordering: singlylinkedlist.New(),
23+
maxSize: maxSize,
24+
}
25+
return set
26+
}
27+
28+
func (set *CacheSet) Add(item interface{}) {
29+
lock.Lock()
30+
defer lock.Unlock()
31+
var contains bool
32+
if _, contains = set.table[item]; !contains {
33+
set.removeExceeded()
34+
set.table[item] = itemExists
35+
set.ordering.Append(item)
36+
}
37+
}
38+
39+
func (set *CacheSet) removeExceeded() {
40+
for set.ordering.Size() >= set.maxSize {
41+
item, exist := set.ordering.Get(0)
42+
if exist {
43+
set.ordering.Remove(0)
44+
delete(set.table, item)
45+
}
46+
}
47+
}
48+
49+
func (set *CacheSet) Contains(item interface{}) bool {
50+
lock.RLock()
51+
defer lock.RUnlock()
52+
if _, contains := set.table[item]; !contains {
53+
return false
54+
}
55+
return true
56+
}
57+
58+
func (set *CacheSet) Empty() bool {
59+
lock.Lock()
60+
defer lock.Unlock()
61+
return set.Size() == 0
62+
}
63+
64+
func (set *CacheSet) Size() int {
65+
return set.ordering.Size()
66+
}
67+
68+
func (set *CacheSet) Clear() {
69+
lock.Lock()
70+
defer lock.Unlock()
71+
set.table = make(map[interface{}]struct{})
72+
set.ordering.Clear()
73+
}
74+
75+
func (set *CacheSet) String() string {
76+
return "CacheSet[" + strconv.Itoa(set.ordering.Size()) + "]"
77+
}
78+
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package cacheset
2+
3+
import (
4+
"strconv"
5+
"testing"
6+
)
7+
8+
func TestCacheSet(t *testing.T) {
9+
cacheSet := New(10)
10+
11+
//add "1" to "9"
12+
for i := 0; i < 10; i++ {
13+
cacheSet.Add(strconv.Itoa(i))
14+
}
15+
16+
if cacheSet.Size() != 10 {
17+
t.Error("size error.")
18+
}
19+
20+
//max size preserved
21+
cacheSet.Add("3")
22+
if cacheSet.Size() != 10 {
23+
t.Error("size error.")
24+
}
25+
26+
//0 exist
27+
if (!cacheSet.Contains("0")) {
28+
t.Error("contains error.")
29+
}
30+
31+
//0 removed
32+
cacheSet.Add("1000")
33+
if cacheSet.Size() != 10 {
34+
t.Error("size error.")
35+
}
36+
37+
if cacheSet.Contains("0") {
38+
t.Error("contains error: 0 was removed.")
39+
}
40+
41+
//1 exist
42+
if !cacheSet.Contains("1") {
43+
t.Error("contains error: 1 should be exist.")
44+
}
45+
46+
//1 removed
47+
cacheSet.Add("1001")
48+
if cacheSet.Size() != 10 {
49+
t.Error("size error.")
50+
}
51+
52+
if cacheSet.Contains("1") {
53+
t.Error("contains error: 1 was removed.")
54+
}
55+
56+
if !cacheSet.Contains("1001") {
57+
t.Error("contains error: 1001 should be exist.")
58+
}
59+
}

0 commit comments

Comments
 (0)