-
-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathblnk.go
More file actions
198 lines (169 loc) · 5.7 KB
/
blnk.go
File metadata and controls
198 lines (169 loc) · 5.7 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
Copyright 2024 Blnk Finance Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package blnk
import (
"embed"
"net/http"
"time"
"github.com/hibiken/asynq"
"github.com/blnkfinance/blnk/config"
"github.com/blnkfinance/blnk/database"
"github.com/blnkfinance/blnk/internal/cache"
"github.com/blnkfinance/blnk/internal/hooks"
"github.com/blnkfinance/blnk/internal/hotpairs"
"github.com/blnkfinance/blnk/internal/notification"
redis_db "github.com/blnkfinance/blnk/internal/redis-db"
"github.com/blnkfinance/blnk/internal/search"
"github.com/blnkfinance/blnk/internal/tokenization"
"github.com/blnkfinance/blnk/model"
"github.com/redis/go-redis/v9"
)
// Blnk represents the main struct for the Blnk application.
type Blnk struct {
queue *Queue
search *search.TypesenseClient
redis redis.UniversalClient
asynqClient *asynq.Client
datasource database.IDataSource
bt *model.BalanceTracker
tokenizer *tokenization.TokenizationService
httpClient *http.Client
Hooks hooks.HookManager
config *config.Configuration
cache cache.Cache
hotPairs *hotpairs.Manager
}
const (
GeneralLedgerID = "general_ledger_id"
)
//go:embed sql/*.sql
var SQLFiles embed.FS
// initializeRedisClients sets up both the Redis client and Asynq client
func initializeRedisClients(config *config.Configuration) (redis.UniversalClient, *asynq.Client, error) {
redisClient, err := redis_db.NewRedisClient([]string{config.Redis.Dns}, config.Redis.SkipTLSVerify, &redis_db.PoolConfig{
PoolSize: config.Redis.PoolSize,
MinIdleConns: config.Redis.MinIdleConns,
})
if err != nil {
return nil, nil, err
}
redisOption, err := redis_db.ParseRedisURL(config.Redis.Dns, config.Redis.SkipTLSVerify)
if err != nil {
return nil, nil, err
}
asynqClient := asynq.NewClient(asynq.RedisClientOpt{
Addr: redisOption.Addr,
Password: redisOption.Password,
DB: redisOption.DB,
TLSConfig: redisOption.TLSConfig,
PoolSize: config.Redis.PoolSize,
})
return redisClient.Client(), asynqClient, nil
}
// initializeTokenizationService creates and configures the tokenization service
func initializeTokenizationService(config *config.Configuration) *tokenization.TokenizationService {
if config.TokenizationSecret == "" {
return tokenization.NewTokenizationService(nil)
}
key := []byte(config.TokenizationSecret)
return tokenization.NewTokenizationService(key)
}
// initializeHTTPClient creates and configures the HTTP client for webhook requests
func initializeHTTPClient() *http.Client {
return &http.Client{
Timeout: 30 * time.Second,
Transport: &http.Transport{
MaxIdleConns: 100,
MaxIdleConnsPerHost: 10,
IdleConnTimeout: 90 * time.Second,
},
}
}
// NewBlnk initializes a new instance of Blnk with the provided database datasource.
// It fetches the configuration, initializes Redis client, balance tracker, queue, and search client.
//
// Parameters:
// - db database.IDataSource: The datasource for database operations.
//
// Returns:
// - *Blnk: A pointer to the newly created Blnk instance.
// - error: An error if any of the initialization steps fail.
func NewBlnk(db database.IDataSource) (*Blnk, error) {
configuration, err := config.Fetch()
if err != nil {
return nil, err
}
redisClient, asynqClient, err := initializeRedisClients(configuration)
if err != nil {
return nil, err
}
bt := NewBalanceTracker()
hotPairManager := hotpairs.NewManager(redisClient, hotpairs.Config{
Enabled: configuration.Queue.EnableHotLane,
HotQueueName: configuration.Queue.HotQueueName,
HotPairTTL: configuration.Queue.HotPairTTL,
LockContentionThreshold: configuration.Queue.HotPairLockContentionThreshold,
})
newQueue := NewQueue(configuration, asynqClient)
newSearch := search.NewTypesenseClient(configuration.TypeSenseKey, []string{configuration.TypeSense.Dns})
hookManager := hooks.NewHookManager(redisClient, asynqClient)
tokenizer := initializeTokenizationService(configuration)
httpClient := initializeHTTPClient()
newCache := cache.NewCacheWithClient(redisClient)
b := &Blnk{
datasource: db,
bt: bt,
queue: newQueue,
redis: redisClient,
asynqClient: asynqClient,
search: newSearch,
tokenizer: tokenizer,
httpClient: httpClient,
Hooks: hookManager,
config: configuration,
cache: newCache,
hotPairs: hotPairManager,
}
notification.RegisterWebhookSender(func(event string, payload interface{}) error {
return b.SendWebhook(NewWebhook{
Event: event,
Payload: payload,
})
})
return b, nil
}
// Close properly closes all connections and resources used by the Blnk instance.
func (b *Blnk) Close() error {
if b.asynqClient != nil {
return b.asynqClient.Close()
}
return nil
}
// Config returns the cached configuration for the Blnk instance.
// Falls back to config.Fetch() if not initialized (for backward compatibility with tests).
func (b *Blnk) Config() *config.Configuration {
if b.config != nil {
return b.config
}
cfg, err := config.Fetch()
if err != nil {
return &config.Configuration{}
}
return cfg
}
func (b *Blnk) GetSearchClient() *search.TypesenseClient {
return b.search
}
func (b *Blnk) GetDataSource() database.IDataSource {
return b.datasource
}