Initial commit.

This commit is contained in:
2020-11-28 23:34:20 +05:00
commit cdf9997cfe
20 changed files with 475 additions and 0 deletions

View 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() {}

View 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()
}
}