-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support in-memory telemetry/stats database
- Loading branch information
Showing
4 changed files
with
56 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package memory | ||
|
||
import ( | ||
"errors" | ||
"sync" | ||
"time" | ||
|
||
"github.com/librespeed/speedtest/database/schema" | ||
) | ||
|
||
const ( | ||
// just enough records to return for FetchLast100 | ||
maxRecords = 100 | ||
) | ||
|
||
type Memory struct { | ||
lock sync.RWMutex | ||
records []schema.TelemetryData | ||
} | ||
|
||
func Open(_ string) *Memory { | ||
return &Memory{} | ||
} | ||
|
||
func (mem *Memory) Insert(data *schema.TelemetryData) error { | ||
mem.lock.Lock() | ||
defer mem.lock.Unlock() | ||
data.Timestamp = time.Now() | ||
mem.records = append(mem.records, *data) | ||
if len(mem.records) > maxRecords { | ||
mem.records = mem.records[len(mem.records)-maxRecords:] | ||
} | ||
return nil | ||
} | ||
|
||
func (mem *Memory) FetchByUUID(uuid string) (*schema.TelemetryData, error) { | ||
mem.lock.RLock() | ||
defer mem.lock.RUnlock() | ||
for _, record := range mem.records { | ||
if record.UUID == uuid { | ||
return &record, nil | ||
} | ||
} | ||
return nil, errors.New("record not found") | ||
} | ||
|
||
func (mem *Memory) FetchLast100() ([]schema.TelemetryData, error) { | ||
mem.lock.RLock() | ||
defer mem.lock.RUnlock() | ||
return mem.records, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters