Initial commit.
This commit is contained in:
25
internal/datastore/application_store.go
Normal file
25
internal/datastore/application_store.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package datastore
|
||||
|
||||
import "sync"
|
||||
|
||||
// This is application-specific data storage.
|
||||
type applicationStorage struct {
|
||||
metrics map[string]string
|
||||
metricsMutex sync.RWMutex
|
||||
}
|
||||
|
||||
// Creates new application-specific storage.
|
||||
func newApplicationStorage() *applicationStorage {
|
||||
as := &applicationStorage{}
|
||||
as.initialize()
|
||||
|
||||
return as
|
||||
}
|
||||
|
||||
// Initializes internal things.
|
||||
func (as *applicationStorage) initialize() {
|
||||
as.metrics = make(map[string]string)
|
||||
}
|
||||
|
||||
// Starts application-specific things, like goroutine for HTTP requests.
|
||||
func (as *applicationStorage) start() {}
|
||||
56
internal/datastore/datastore.go
Normal file
56
internal/datastore/datastore.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package datastore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"sync"
|
||||
|
||||
"go.dev.pztrn.name/metricator/internal/configuration"
|
||||
)
|
||||
|
||||
// DataStore is a data storage structure. It keeps all gathered metrics and gives
|
||||
// them away on request.
|
||||
type DataStore struct {
|
||||
config *configuration.Config
|
||||
ctx context.Context
|
||||
doneChan chan struct{}
|
||||
|
||||
datas map[string]*applicationStorage
|
||||
datasMutex sync.RWMutex
|
||||
}
|
||||
|
||||
// NewDataStore creates new data storage.
|
||||
func NewDataStore(ctx context.Context, cfg *configuration.Config) (*DataStore, chan struct{}) {
|
||||
ds := &DataStore{
|
||||
config: cfg,
|
||||
ctx: ctx,
|
||||
doneChan: make(chan struct{}),
|
||||
}
|
||||
ds.initialize()
|
||||
|
||||
return ds, ds.doneChan
|
||||
}
|
||||
|
||||
// Internal things initialization.
|
||||
func (ds *DataStore) initialize() {
|
||||
ds.datas = make(map[string]*applicationStorage)
|
||||
|
||||
// Create applications defined in configuration.
|
||||
|
||||
go func() {
|
||||
<-ds.ctx.Done()
|
||||
log.Println("Data storage stopped")
|
||||
|
||||
ds.doneChan <- struct{}{}
|
||||
}()
|
||||
}
|
||||
|
||||
// Start starts data storage asynchronous things.
|
||||
func (ds *DataStore) Start() {
|
||||
log.Println("Starting data storage...")
|
||||
|
||||
ds.datasMutex.RLock()
|
||||
for _, storage := range ds.datas {
|
||||
storage.start()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user