Skip to content

Commit

Permalink
DOn't make underlying SyncArray object accessible
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon Berhent committed Aug 11, 2022
1 parent 67c9d70 commit 555d7da
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
22 changes: 14 additions & 8 deletions apps/server/src/models/sync_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ type ActiveChannelObject struct {
// SyncArray builds an thread-safe array with some handy methods
type SyncArray struct {
mu sync.Mutex
Channels []ActiveChannelObject
channels []ActiveChannelObject
}

func NewSyncArray() *SyncArray {
return &SyncArray{
Channels: []ActiveChannelObject{},
channels: []ActiveChannelObject{},
}
}

// See if element exists
func (r *SyncArray) Exists(requestID string) bool {
for _, v := range r.Channels {
for _, v := range r.channels {
if v.RequestID == requestID {
return true
}
Expand All @@ -35,7 +35,7 @@ func (r *SyncArray) Exists(requestID string) bool {
}

func (r *SyncArray) HashExists(hash string) bool {
for _, v := range r.Channels {
for _, v := range r.channels {
if v.Hash == hash {
return true
}
Expand All @@ -48,7 +48,7 @@ func (r *SyncArray) Put(value ActiveChannelObject) {
r.mu.Lock()
defer r.mu.Unlock()
if !r.Exists(value.RequestID) {
r.Channels = append(r.Channels, value)
r.channels = append(r.channels, value)
}
}

Expand All @@ -57,7 +57,7 @@ func (r *SyncArray) Get(requestID string) *ActiveChannelObject {
r.mu.Lock()
defer r.mu.Unlock()
if r.Exists(requestID) {
return &r.Channels[r.IndexOf(requestID)]
return &r.channels[r.IndexOf(requestID)]
}

return nil
Expand All @@ -69,19 +69,25 @@ func (r *SyncArray) Delete(requestID string) {
defer r.mu.Unlock()
index := r.IndexOf(requestID)
if index > -1 {
r.Channels = remove(r.Channels, r.IndexOf(requestID))
r.channels = remove(r.channels, r.IndexOf(requestID))
}
}

func (r *SyncArray) IndexOf(requestID string) int {
for i, v := range r.Channels {
for i, v := range r.channels {
if v.RequestID == requestID {
return i
}
}
return -1
}

func (r *SyncArray) Len() int {
r.mu.Lock()
defer r.mu.Unlock()
return len(r.channels)
}

// NOT thread safe, must be called from within a locked section
func remove(s []ActiveChannelObject, i int) []ActiveChannelObject {
s[i] = s[len(s)-1]
Expand Down
4 changes: 2 additions & 2 deletions apps/server/src/models/sync_array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ func TestSyncArray(t *testing.T) {
Chan: make(chan []byte),
})

utils.AssertEqual(t, 3, len(array.Channels))
utils.AssertEqual(t, 3, array.Len())
utils.AssertEqual(t, true, array.Exists("1"))
utils.AssertEqual(t, "1", array.Get("1").Hash)
utils.AssertEqual(t, true, array.HashExists("2"))
array.Delete("1")
utils.AssertEqual(t, (*ActiveChannelObject)(nil), array.Get("1"))
utils.AssertEqual(t, 2, len(array.Channels))
utils.AssertEqual(t, 2, array.Len())
utils.AssertEqual(t, 0, array.IndexOf("3"))
}

0 comments on commit 555d7da

Please sign in to comment.