aboutsummaryrefslogtreecommitdiff
path: root/ktable.go
diff options
context:
space:
mode:
Diffstat (limited to 'ktable.go')
-rw-r--r--ktable.go40
1 files changed, 34 insertions, 6 deletions
diff --git a/ktable.go b/ktable.go
index b559648..b009d92 100644
--- a/ktable.go
+++ b/ktable.go
@@ -1,30 +1,58 @@
package main
-import "fmt"
+import (
+ "fmt"
+ "sync"
+)
-const kTableLimit = 5000
+const kTableLimit = 1000
// Keep it simple for now
type kTable struct {
+ sync.Mutex
nodes []*remoteNode
}
func newKTable() kTable {
- return kTable{make([]*remoteNode, 0)}
+ k := kTable{}
+ k.refresh()
+ return k
}
func (k *kTable) add(rn *remoteNode) {
- if len(k.nodes) >= kTableLimit {
- return
- }
+ k.Lock()
+ defer k.Unlock()
if rn == nil || rn.id == "" {
fmt.Println("Trying to add invalid rn")
return
}
+ if k.isFull() {
+ k.refresh()
+ }
k.nodes = append(k.nodes, rn)
}
+func (k *kTable) getNodes() []*remoteNode {
+ k.Lock()
+ defer k.Unlock()
+ return k.nodes
+}
+
+func (k *kTable) isEmpty() bool {
+ k.Lock()
+ defer k.Unlock()
+ return len(k.nodes) == 0
+}
+
+func (k *kTable) isFull() bool {
+ k.Lock()
+ defer k.Unlock()
+ return len(k.nodes) >= kTableLimit
+}
+
// For now
func (k *kTable) refresh() {
+ k.Lock()
+ defer k.Unlock()
k.nodes = make([]*remoteNode, 0)
}