-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.go
253 lines (209 loc) · 5.71 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package main
import (
"context"
"errors"
"flag"
"fmt"
stdlog "log"
"net/http"
"os"
"os/signal"
"runtime"
"sync"
"syscall"
"time"
"github.com/gin-gonic/gin"
_ "github.com/joho/godotenv/autoload"
"github.com/labring/aiproxy/common"
"github.com/labring/aiproxy/common/balance"
"github.com/labring/aiproxy/common/config"
"github.com/labring/aiproxy/common/consume"
"github.com/labring/aiproxy/common/notify"
"github.com/labring/aiproxy/controller"
"github.com/labring/aiproxy/middleware"
"github.com/labring/aiproxy/model"
"github.com/labring/aiproxy/router"
log "github.com/sirupsen/logrus"
)
var listen string
func init() {
flag.StringVar(&listen, "listen", ":3000", "http server listen")
}
func initializeServices() error {
setLog(log.StandardLogger())
initializeNotifier()
if err := initializeBalance(); err != nil {
return err
}
if err := initializeDatabases(); err != nil {
return err
}
return initializeCaches()
}
func initializeBalance() error {
sealosJwtKey := os.Getenv("SEALOS_JWT_KEY")
if sealosJwtKey == "" {
log.Info("SEALOS_JWT_KEY is not set, balance will not be enabled")
return nil
}
log.Info("SEALOS_JWT_KEY is set, balance will be enabled")
return balance.InitSealos(sealosJwtKey, os.Getenv("SEALOS_ACCOUNT_URL"))
}
func initializeNotifier() {
feishuWh := os.Getenv("NOTIFY_FEISHU_WEBHOOK")
if feishuWh != "" {
notify.SetDefaultNotifier(notify.NewFeishuNotify(feishuWh))
log.Info("NOTIFY_FEISHU_WEBHOOK is set, notifier will be use feishu")
}
}
var logCallerIgnoreFuncs = map[string]struct{}{
"github.com/labring/aiproxy/middleware.logColor": {},
}
func setLog(l *log.Logger) {
gin.ForceConsoleColor()
if config.DebugEnabled {
l.SetLevel(log.DebugLevel)
l.SetReportCaller(true)
gin.SetMode(gin.DebugMode)
} else {
l.SetLevel(log.InfoLevel)
l.SetReportCaller(false)
gin.SetMode(gin.ReleaseMode)
}
l.SetOutput(os.Stdout)
stdlog.SetOutput(l.Writer())
l.SetFormatter(&log.TextFormatter{
ForceColors: true,
DisableColors: false,
ForceQuote: config.DebugEnabled,
DisableQuote: !config.DebugEnabled,
DisableSorting: false,
FullTimestamp: true,
TimestampFormat: time.DateTime,
QuoteEmptyFields: true,
CallerPrettyfier: func(f *runtime.Frame) (function string, file string) {
if _, ok := logCallerIgnoreFuncs[f.Function]; ok {
return "", ""
}
return f.Function, fmt.Sprintf("%s:%d", f.File, f.Line)
},
})
if common.NeedColor() {
gin.ForceConsoleColor()
}
}
func initializeDatabases() error {
model.InitDB()
model.InitLogDB()
return common.InitRedisClient()
}
func initializeCaches() error {
if err := model.InitOption2DB(); err != nil {
return err
}
return model.InitModelConfigAndChannelCache()
}
func startSyncServices(ctx context.Context, wg *sync.WaitGroup) {
wg.Add(2)
go model.SyncOptions(ctx, wg, time.Second*5)
go model.SyncModelConfigAndChannelCache(ctx, wg, time.Second*10)
}
func setupHTTPServer() (*http.Server, *gin.Engine) {
server := gin.New()
w := log.StandardLogger().Writer()
server.
Use(gin.RecoveryWithWriter(w)).
Use(middleware.NewLog(log.StandardLogger())).
Use(middleware.RequestIDMiddleware, middleware.CORS())
router.SetRouter(server)
listenEnv := os.Getenv("LISTEN")
if listenEnv != "" {
listen = listenEnv
}
return &http.Server{
Addr: listen,
ReadHeaderTimeout: 10 * time.Second,
Handler: server,
}, server
}
func autoTestBannedModels(ctx context.Context) {
log.Info("auto test banned models start")
ticker := time.NewTicker(time.Second * 30)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
controller.AutoTestBannedModels()
}
}
}
func cleanLog(ctx context.Context) {
log.Info("clean log start")
// the interval should not be too large to avoid cleaning too much at once
ticker := time.NewTicker(time.Second * 15)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
err := model.CleanLog(1000)
if err != nil {
notify.ErrorThrottle("cleanLog", time.Minute, "clean log failed", err.Error())
}
}
}
}
// @securityDefinitions.apikey ApiKeyAuth
// @in header
// @name Authorization
func main() {
flag.Parse()
if err := initializeServices(); err != nil {
log.Fatal("failed to initialize services: " + err.Error())
}
defer func() {
if err := model.CloseDB(); err != nil {
log.Fatal("failed to close database: " + err.Error())
}
}()
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
var wg sync.WaitGroup
startSyncServices(ctx, &wg)
srv, _ := setupHTTPServer()
go func() {
log.Infof("server started on %s", srv.Addr)
log.Infof("swagger server started on %s/swagger/index.html", srv.Addr)
if err := srv.ListenAndServe(); err != nil &&
!errors.Is(err, http.ErrServerClosed) {
log.Fatal("failed to start HTTP server: " + err.Error())
}
}()
go autoTestBannedModels(ctx)
go cleanLog(ctx)
go controller.UpdateChannelsBalance(time.Minute * 10)
batchProcessorCtx, batchProcessorCancel := context.WithCancel(context.Background())
wg.Add(1)
go model.StartBatchProcessor(batchProcessorCtx, &wg)
<-ctx.Done()
shutdownCtx, cancel := context.WithTimeout(context.Background(), 600*time.Second)
defer cancel()
log.Info("shutting down http server...")
log.Info("max wait time: 600s")
if err := srv.Shutdown(shutdownCtx); err != nil {
log.Error("server forced to shutdown: " + err.Error())
} else {
log.Info("server shutdown successfully")
}
log.Info("shutting down consumer...")
consume.Wait()
batchProcessorCancel()
log.Info("shutting down sync services...")
wg.Wait()
log.Info("shutting down batch processor...")
model.ProcessBatchUpdates()
log.Info("server exiting")
}