-
Notifications
You must be signed in to change notification settings - Fork 64
/
main.go
160 lines (129 loc) · 4.91 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
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/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)
}
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)
}
memStore, err := memdb.NewMemDb(store, log, cfg.InMemoryDbUpdateInterval)
if err != nil {
log.Sugar().Fatalf("cannot initialize memdb: %v", err)
}
memStore.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)
as, err := web.NewAdminServer(log, *modePtr, m, krm)
if err != nil {
log.Sugar().Fatalf("error creating admin http server: %v", err)
}
tc, err := openai.NewTokenCounter()
if err != nil {
log.Sugar().Fatalf("error creating token counter: %v", err)
}
as.Run()
ce := openai.NewCostEstimator(openai.OpenAiPerThousandTokenCost, tc)
v := validator.NewValidator(costLimitCache, rateLimitCache, costStorage)
rec := recorder.NewRecorder(costStorage, costLimitCache, ce)
rlm := manager.NewRateLimitManager(rateLimitCache)
ps, err := web.NewProxyServer(log, *modePtr, *privacyPtr, m, store, memStore, ce, v, rec, cfg.OpenAiKey, e, rlm)
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()
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)
}
log.Sugar().Infof("server exited")
}