-
Notifications
You must be signed in to change notification settings - Fork 6
/
storage.go
47 lines (41 loc) · 1.94 KB
/
storage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package storage
import (
"context"
"fmt"
"golang.org/x/xerrors"
"github.com/saiya/dsps/server/config"
"github.com/saiya/dsps/server/domain"
"github.com/saiya/dsps/server/logger"
"github.com/saiya/dsps/server/storage/deps"
"github.com/saiya/dsps/server/storage/multiplex"
"github.com/saiya/dsps/server/storage/onmemory"
"github.com/saiya/dsps/server/storage/redis"
"github.com/saiya/dsps/server/storage/tracing"
)
// NewStorage initialize Storage instance as per given config
func NewStorage(ctx context.Context, config *config.StoragesConfig, systemClock domain.SystemClock, channelProvider domain.ChannelProvider, deps deps.StorageDeps) (domain.Storage, error) {
children := map[domain.StorageID]domain.Storage{}
for id, subConfig := range *config {
storage, err := newSubStorage(ctx, id, subConfig, systemClock, channelProvider, deps)
if err != nil {
return nil, fmt.Errorf("Failed to initialize storage \"%s\": %w", id, err)
}
children[id] = tracing.NewTracingStorage(storage, id, deps)
}
storage, err := multiplex.NewStorageMultiplexer(children)
if err != nil {
return nil, fmt.Errorf("Failed to initialize storage multiplexer: %w", err)
}
return tracing.NewTracingStorage(storage, "#root", deps), nil
}
func newSubStorage(ctx context.Context, id domain.StorageID, config *config.StorageConfig, systemClock domain.SystemClock, channelProvider domain.ChannelProvider, deps deps.StorageDeps) (domain.Storage, error) {
if config.Onmemory != nil {
logger.Of(ctx).Warnf(logger.CatStorage, "Starting onmemory storage \"%s\", ** DO NOT USE onmemory storage on production environment **", id)
return onmemory.NewOnmemoryStorage(ctx, config.Onmemory, systemClock, channelProvider, deps)
}
if config.Redis != nil {
logger.Of(ctx).Debugf(logger.CatStorage, "Starting Redis storage \"%s\"", id)
return redis.NewRedisStorage(ctx, config.Redis, systemClock, channelProvider, deps)
}
return nil, xerrors.New("Empty storage configuration given")
}