Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
ring
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
xx network
ring
Commits
589b495b
Commit
589b495b
authored
Mar 11, 2020
by
Benjamin Wenger
Browse files
Options
Downloads
Patches
Plain Diff
made the mutex embeeded so it auto intilitalizes
parent
7320eed5
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
ringBuff.go
+21
-21
21 additions, 21 deletions
ringBuff.go
with
21 additions
and
21 deletions
ringBuff.go
+
21
−
21
View file @
589b495b
...
...
@@ -29,7 +29,7 @@ type Buff struct {
buff
[]
interface
{}
count
,
old
,
new
int
id
idFunc
lock
sync
.
RWMutex
sync
.
RWMutex
}
// Initialize a new ring buffer with length n
...
...
@@ -46,40 +46,40 @@ func NewBuff(n int, id idFunc) *Buff {
// Get the ID of the newest item in the buffer
func
(
rb
*
Buff
)
GetNewestId
()
int
{
rb
.
lock
.
RLock
()
defer
rb
.
lock
.
RUnlock
()
rb
.
RLock
()
defer
rb
.
RUnlock
()
return
rb
.
getNewestId
()
}
// Get the IDof the oldest item in the buffer
func
(
rb
*
Buff
)
GetOldestId
()
int
{
rb
.
lock
.
RLock
()
defer
rb
.
lock
.
RUnlock
()
rb
.
RLock
()
defer
rb
.
RUnlock
()
return
rb
.
getOldestId
()
}
// Get the index of the oldest item in the buffer, not including nil values inserted by UpsertById
func
(
rb
*
Buff
)
GetOldestIndex
()
int
{
rb
.
lock
.
RLock
()
defer
rb
.
lock
.
RUnlock
()
rb
.
RLock
()
defer
rb
.
RUnlock
()
return
rb
.
getOldestIndex
()
}
// Push a round to the buffer
func
(
rb
*
Buff
)
Push
(
val
interface
{})
{
rb
.
lock
.
Lock
()
defer
rb
.
lock
.
Unlock
()
rb
.
Lock
()
defer
rb
.
Unlock
()
rb
.
push
(
val
)
}
// push a round to a relative index in the buffer
func
(
rb
*
Buff
)
UpsertById
(
val
interface
{},
comp
compFunc
)
error
{
rb
.
lock
.
Lock
()
defer
rb
.
lock
.
Unlock
()
rb
.
Lock
()
defer
rb
.
Unlock
()
// Make sure the id isn't too old
newId
:=
rb
.
id
(
val
)
...
...
@@ -113,8 +113,8 @@ func (rb *Buff) UpsertById(val interface{}, comp compFunc) error {
// Retreive the most recent entry
func
(
rb
*
Buff
)
Get
()
interface
{}
{
rb
.
lock
.
RLock
()
defer
rb
.
lock
.
RUnlock
()
rb
.
RLock
()
defer
rb
.
RUnlock
()
mostRecentIndex
:=
(
rb
.
new
+
rb
.
count
-
1
)
%
rb
.
count
return
rb
.
buff
[
mostRecentIndex
]
...
...
@@ -122,8 +122,8 @@ func (rb *Buff) Get() interface{} {
// Retrieve an entry with the given ID
func
(
rb
*
Buff
)
GetById
(
id
int
)
(
interface
{},
error
)
{
rb
.
lock
.
RLock
()
defer
rb
.
lock
.
RUnlock
()
rb
.
RLock
()
defer
rb
.
RUnlock
()
// Check it's not before our first known id
firstId
:=
rb
.
id
(
rb
.
buff
[
rb
.
getOldestIndex
()])
...
...
@@ -143,8 +143,8 @@ func (rb *Buff) GetById(id int) (interface{}, error) {
// Retrieve an entry at the given index
func
(
rb
*
Buff
)
GetByIndex
(
i
int
)
(
interface
{},
error
)
{
rb
.
lock
.
RLock
()
defer
rb
.
lock
.
RUnlock
()
rb
.
RLock
()
defer
rb
.
RUnlock
()
if
math
.
Abs
(
float64
(
i
))
>
float64
(
rb
.
Len
())
{
// FIXME: this shouldn't be float but we don't have a package where it's not float
return
nil
,
errors
.
Errorf
(
"Could not get item at index %d: index out of bounds"
,
i
)
...
...
@@ -154,8 +154,8 @@ func (rb *Buff) GetByIndex(i int) (interface{}, error) {
//retrieve all entries newer than the passed one
func
(
rb
*
Buff
)
GetNewerById
(
id
int
)
([]
interface
{},
error
)
{
rb
.
lock
.
RLock
()
defer
rb
.
lock
.
RUnlock
()
rb
.
RLock
()
defer
rb
.
RUnlock
()
new
:=
rb
.
getNewestId
()
old
:=
rb
.
getOldestId
()
...
...
@@ -183,8 +183,8 @@ func (rb *Buff) GetNewerById(id int) ([]interface{}, error) {
// Return length of the structure
func
(
rb
*
Buff
)
Len
()
int
{
rb
.
lock
.
RLock
()
defer
rb
.
lock
.
RUnlock
()
rb
.
RLock
()
defer
rb
.
RUnlock
()
return
rb
.
count
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment