-
Notifications
You must be signed in to change notification settings - Fork 64
/
main.go
206 lines (166 loc) · 6.17 KB
/
main.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
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
199
200
201
202
203
204
205
206
package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/bricks-cloud/bricksllm/internal/config"
"github.com/bricks-cloud/bricksllm/internal/encrypter"
logger "github.com/bricks-cloud/bricksllm/internal/logger/zap"
"github.com/bricks-cloud/bricksllm/internal/manager"
"github.com/bricks-cloud/bricksllm/internal/provider/openai"
"github.com/bricks-cloud/bricksllm/internal/recorder"
"github.com/bricks-cloud/bricksllm/internal/server/web"
"github.com/bricks-cloud/bricksllm/internal/stats"
"github.com/bricks-cloud/bricksllm/internal/storage/memdb"
"github.com/bricks-cloud/bricksllm/internal/storage/postgresql"
redisStorage "github.com/bricks-cloud/bricksllm/internal/storage/redis"
"github.com/bricks-cloud/bricksllm/internal/validator"
"github.com/gin-gonic/gin"
"github.com/redis/go-redis/v9"
)
func main() {
modePtr := flag.String("m", "dev", "select the mode that bricksllm runs in")
privacyPtr := flag.String("p", "strict", "select the privacy mode that bricksllm runs in")
flag.Parse()
log := logger.NewZapLogger(*modePtr)
gin.SetMode(gin.ReleaseMode)
cfg, err := config.ParseEnvVariables()
if err != nil {
log.Sugar().Fatalf("cannot parse environment variables: %v", err)
}
err = stats.InitializeClient(cfg.StatsProvider)
if err != nil {
log.Sugar().Fatalf("cannot connect to telemetry provider: %v", err)
}
store, err := postgresql.NewStore(
fmt.Sprintf("postgresql:///%s?sslmode=%s&user=%s&password=%s&host=%s&port=%s", cfg.PostgresqlDbName, cfg.PostgresqlSslMode, cfg.PostgresqlUsername, cfg.PostgresqlPassword, cfg.PostgresqlHosts, cfg.PostgresqlPort),
cfg.PostgresqlWriteTimeout,
cfg.PostgresqlReadTimeout,
)
if err != nil {
log.Sugar().Fatalf("cannot connect to postgresql: %v", err)
}
err = store.CreateKeysTable()
if err != nil {
log.Sugar().Fatalf("error creating keys table: %v", err)
}
err = store.AlterKeysTable()
if err != nil {
log.Sugar().Fatalf("error altering keys table: %v", err)
}
err = store.CreateEventsTable()
if err != nil {
log.Sugar().Fatalf("error creating events table: %v", err)
}
err = store.AlterEventsTable()
if err != nil {
log.Sugar().Fatalf("error altering events table: %v", err)
}
err = store.CreateProviderSettingsTable()
if err != nil {
log.Sugar().Fatalf("error creating provider settings table: %v", err)
}
err = store.AlterProviderSettingsTable()
if err != nil {
log.Sugar().Fatalf("error altering provider settings table: %v", err)
}
memStore, err := memdb.NewMemDb(store, log, cfg.InMemoryDbUpdateInterval)
if err != nil {
log.Sugar().Fatalf("cannot initialize memdb: %v", err)
}
memStore.Listen()
psMemStore, err := memdb.NewProviderSettingsMemDb(store, log, cfg.InMemoryDbUpdateInterval)
if err != nil {
log.Sugar().Fatalf("cannot initialize provider settings memdb: %v", err)
}
psMemStore.Listen()
rateLimitRedisCache := redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%s", cfg.RedisHosts, cfg.RedisPort),
Password: cfg.RedisPassword,
DB: 0,
})
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
if err := rateLimitRedisCache.Ping(ctx).Err(); err != nil {
log.Sugar().Fatalf("error connecting to rate limit redis cache: %v", err)
}
costLimitRedisCache := redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%s", cfg.RedisHosts, cfg.RedisPort),
Password: cfg.RedisPassword,
DB: 1,
})
ctx, cancel = context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
if err := costLimitRedisCache.Ping(ctx).Err(); err != nil {
log.Sugar().Fatalf("error connecting to cost limit redis cache: %v", err)
}
costRedisStorage := redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%s", cfg.RedisHosts, cfg.RedisPort),
Password: cfg.RedisPassword,
DB: 2,
})
ctx, cancel = context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
if err := costRedisStorage.Ping(ctx).Err(); err != nil {
log.Sugar().Fatalf("error connecting to cost limit redis storage: %v", err)
}
rateLimitCache := redisStorage.NewCache(rateLimitRedisCache, cfg.RedisWriteTimeout, cfg.RedisReadTimeout)
costLimitCache := redisStorage.NewCache(costLimitRedisCache, cfg.RedisWriteTimeout, cfg.RedisReadTimeout)
costStorage := redisStorage.NewStore(costRedisStorage, cfg.RedisWriteTimeout, cfg.RedisReadTimeout)
e := encrypter.NewEncrypter()
m := manager.NewManager(store, e)
krm := manager.NewReportingManager(costStorage, store, store)
psm := manager.NewProviderSettingsManager(store, psMemStore)
as, err := web.NewAdminServer(log, *modePtr, m, krm, psm)
if err != nil {
log.Sugar().Fatalf("error creating admin http server: %v", err)
}
tc := openai.NewTokenCounter()
as.Run()
ce := openai.NewCostEstimator(openai.OpenAiPerThousandTokenCost, tc)
v := validator.NewValidator(costLimitCache, rateLimitCache, costStorage)
rec := recorder.NewRecorder(costStorage, costLimitCache, ce, store)
rlm := manager.NewRateLimitManager(rateLimitCache)
ps, err := web.NewProxyServer(log, *modePtr, *privacyPtr, m, psm, store, memStore, ce, v, rec, cfg.OpenAiKey, e, rlm, cfg.ProxyTimeout)
if err != nil {
log.Sugar().Fatalf("error creating proxy http server: %v", err)
}
ps.Run()
quit := make(chan os.Signal)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
memStore.Stop()
psMemStore.Stop()
log.Sugar().Info("shutting down server...")
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := as.Shutdown(ctx); err != nil {
log.Sugar().Debugf("admin server shutdown: %v", err)
}
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := ps.Shutdown(ctx); err != nil {
log.Sugar().Debugf("proxy server shutdown: %v", err)
}
select {
case <-ctx.Done():
log.Sugar().Infof("timeout of 5 seconds")
}
err = store.DropKeysTable()
if err != nil {
log.Sugar().Fatalf("error dropping keys table: %v", err)
}
err = store.DropEventsTable()
if err != nil {
log.Sugar().Fatalf("error dropping events table: %v", err)
}
err = store.DropProviderSettingsTable()
if err != nil {
log.Sugar().Fatalf("error dropping provider settings table: %v", err)
}
log.Sugar().Infof("server exited")
}