/
fast_store.go
146 lines (125 loc) · 4.23 KB
/
fast_store.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package app
import (
"github.com/gomodule/redigo/redis"
"github.com/jitsucom/bulker/base/objects"
jsoniter "github.com/json-iterator/go"
"strings"
)
const fastStoreServiceName = "fast_store"
const fastStoreStreamIdsKey = "streamIds"
const fastStoreApiKeys = "apiKeys"
const fastStoreStreamDomainsKey = "streamDomains"
type FastStore struct {
objects.ServiceBase
redisPool *redis.Pool
}
type DataLayout string
const (
DataLayoutSegmentCompatible = "segment-compatible"
DataLayoutSegmentSingleTable = "segment-single-table"
DataLayoutJitsuLegacy = "jitsu-legacy"
)
type ApiKey struct {
Id string `json:"id"`
Plaintext string `json:"plaintext"`
Hash string `json:"hash"`
Hint string `json:"hint"`
}
type ApiKeyBinding struct {
Hash string `json:"hash"`
KeyType string `json:"keyType"`
StreamId string `json:"streamId"`
}
type StreamConfig struct {
Id string `json:"id"`
Type string `json:"type"`
WorkspaceId string `json:"workspaceId"`
Slug string `json:"slug"`
Name string `json:"name"`
Domains []string `json:"domains"`
AuthorizedJavaScriptDomains string `json:"authorizedJavaScriptDomains"`
PublicKeys []ApiKey `json:"publicKeys"`
PrivateKeys []ApiKey `json:"privateKeys""`
DataLayout string `json:"dataLayout"`
}
type ShortDestinationConfig struct {
TagDestinationConfig
Id string `json:"id"`
ConnectionId string `json:"connectionId"`
DestinationType string `json:"destinationType"`
}
type TagDestinationConfig struct {
Mode string `json:"mode"`
Code string `json:"code"`
}
type StreamWithDestinations struct {
Stream StreamConfig `json:"stream"`
SynchronousDestinations []ShortDestinationConfig `json:"synchronousDestinations"`
AsynchronousDestinations []ShortDestinationConfig `json:"asynchronousDestinations"`
}
func NewFastStore(config *AppConfig) (*FastStore, error) {
base := objects.NewServiceBase(fastStoreServiceName)
base.Debugf("Creating FastStore with redisURL: %s", config.RedisURL)
redisPool := newPool(config.RedisURL, config.RedisTLSCA)
fs := FastStore{
ServiceBase: base,
redisPool: redisPool,
}
return &fs, nil
}
func (fs *FastStore) getStreamByKeyId(keyId string) (*ApiKeyBinding, error) {
connection := fs.redisPool.Get()
defer connection.Close()
keyBytes, err := redis.Bytes(connection.Do("HGET", fastStoreApiKeys, keyId))
if err == redis.ErrNil {
return nil, nil
}
if err != nil {
return nil, fs.NewError("failed to get stream binding by keyId: %w", err)
}
binding := ApiKeyBinding{}
err = jsoniter.Unmarshal(keyBytes, &binding)
if err != nil {
return nil, fs.NewError("failed to unmarshal binding bytes for keyId [%s]: %w: %s", keyId, err, string(keyBytes))
}
return &binding, nil
}
func (fs *FastStore) GetStreamById(slug string) (*StreamWithDestinations, error) {
connection := fs.redisPool.Get()
defer connection.Close()
streamBytes, err := redis.Bytes(connection.Do("HGET", fastStoreStreamIdsKey, slug))
if err == redis.ErrNil {
return nil, nil
}
if err != nil {
return nil, fs.NewError("failed to get stream by slug [%s]: %w", slug, err)
}
stream := StreamWithDestinations{}
err = jsoniter.Unmarshal(streamBytes, &stream)
if err != nil {
return nil, fs.NewError("failed to unmarshal stream bytes for slug [%s]: %w: %s", slug, err, string(streamBytes))
}
return &stream, nil
}
func (fs *FastStore) GetStreamsByDomain(domain string) ([]StreamWithDestinations, error) {
connection := fs.redisPool.Get()
defer connection.Close()
domain = strings.ToLower(domain)
streamBytes, err := redis.Bytes(connection.Do("HGET", fastStoreStreamDomainsKey, domain))
if err == redis.ErrNil {
return nil, nil
}
if err != nil {
return nil, fs.NewError("failed to get stream by domain [%s]: %w", domain, err)
}
stream := make([]StreamWithDestinations, 0, 2)
err = jsoniter.Unmarshal(streamBytes, &stream)
if err != nil {
return nil, fs.NewError("failed to unmarshal stream bytes for domain [%s]: %w: %s", domain, err, string(streamBytes))
}
return stream, nil
}
func (fs *FastStore) Close() error {
fs.redisPool.Close()
return nil
}