diff options
| author | Felix Hanley <felix@userspace.com.au> | 2017-06-12 10:33:02 +0000 |
|---|---|---|
| committer | Felix Hanley <felix@userspace.com.au> | 2017-06-12 10:33:02 +0000 |
| commit | 6bd6035f8356e8c374385ea8ec02e042ec46cda5 (patch) | |
| tree | 501c7e836e406fabc7c72018b2bd92d79364bc52 /ktable.go | |
| parent | 0d8fb6ef4e85dc113c89e9b137465a1033fa414e (diff) | |
| download | dhtsearch-6bd6035f8356e8c374385ea8ec02e042ec46cda5.tar.gz dhtsearch-6bd6035f8356e8c374385ea8ec02e042ec46cda5.tar.bz2 | |
Make kTable thread safe
Diffstat (limited to 'ktable.go')
| -rw-r--r-- | ktable.go | 40 |
1 files changed, 34 insertions, 6 deletions
@@ -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) } |
