-
Notifications
You must be signed in to change notification settings - Fork 359
Expand file tree
/
Copy pathllama-swap.go
More file actions
352 lines (302 loc) · 9.94 KB
/
Copy pathllama-swap.go
File metadata and controls
352 lines (302 loc) · 9.94 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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package main
import (
"context"
"errors"
"flag"
"fmt"
"log/slog"
"net"
"net/http"
"os"
"os/signal"
"path/filepath"
"strings"
"sync"
"syscall"
"time"
"github.com/mostlygeek/llama-swap/internal/config"
"github.com/mostlygeek/llama-swap/internal/event"
"github.com/mostlygeek/llama-swap/internal/logmon"
"github.com/mostlygeek/llama-swap/internal/perf"
"github.com/mostlygeek/llama-swap/internal/process"
"github.com/mostlygeek/llama-swap/internal/server"
"github.com/mostlygeek/llama-swap/internal/shared"
"github.com/mostlygeek/llama-swap/internal/watcher"
)
var (
version = "0"
commit = "abcd1234"
date = "unknown"
)
const shutdownTimeout = 30 * time.Second
// logTimeFormats maps the cfg.LogTimeFormat value to a Go time layout. An
// unset or unrecognised value yields "" — no timestamp prefix.
var logTimeFormats = map[string]string{
"ansic": time.ANSIC,
"unixdate": time.UnixDate,
"rubydate": time.RubyDate,
"rfc822": time.RFC822,
"rfc822z": time.RFC822Z,
"rfc850": time.RFC850,
"rfc1123": time.RFC1123,
"rfc1123z": time.RFC1123Z,
"rfc3339": time.RFC3339,
"rfc3339nano": time.RFC3339Nano,
"kitchen": time.Kitchen,
"stamp": time.Stamp,
"stampmilli": time.StampMilli,
"stampmicro": time.StampMicro,
"stampnano": time.StampNano,
}
func main() {
flagConfig := flag.String("config", "", "path to config file")
flagConfigDir := flag.String("config-dir", "", "directory of *.yml/*.yaml config files (additive to -config)")
flagListen := flag.String("listen", "", "listen address (default :8080 or :8443 for TLS)")
flagCertFile := flag.String("tls-cert-file", "", "TLS certificate file")
flagKeyFile := flag.String("tls-key-file", "", "TLS key file")
flagVersion := flag.Bool("version", false, "show version and exit")
flagWatchConfig := flag.Bool("watch-config", false, "reload config on file change")
flag.Parse()
if *flagVersion {
fmt.Printf("version: %s (%s), built at %s\n", version, commit, date)
os.Exit(0)
}
if *flagConfig == "" && *flagConfigDir == "" {
slog.Error("at least one of -config or -config-dir must be provided")
os.Exit(1)
}
useTLS := *flagCertFile != "" || *flagKeyFile != ""
if (*flagCertFile != "" && *flagKeyFile == "") || (*flagCertFile == "" && *flagKeyFile != "") {
slog.Error("both -tls-cert-file and -tls-key-file must be provided for TLS")
os.Exit(1)
}
listenAddr := *flagListen
if listenAddr == "" {
if useTLS {
listenAddr = ":8443"
} else {
listenAddr = ":8080"
}
}
cfg, err := config.LoadConfigSources(*flagConfig, *flagConfigDir)
if err != nil {
slog.Error("failed to load config", "config", *flagConfig, "config-dir", *flagConfigDir, "error", err)
os.Exit(1)
}
// Loggers are wired per cfg.LogToStdout: proxy/upstream feed muxLog, which
// owns the combined history served by /logs. They outlive config reloads,
// so a LogToStdout change requires a restart to take effect.
muxLog, proxyLog, upstreamLog := server.NewLoggers(cfg.LogToStdout)
if len(cfg.Profiles) > 0 {
proxyLog.Warn("Profile functionality has been removed in favor of Groups. See the README for more information.")
}
applyLogSettings := func(cfg config.Config) {
level := logmon.LevelInfo
switch strings.ToLower(strings.TrimSpace(cfg.LogLevel)) {
case "debug":
level = logmon.LevelDebug
case "warn":
level = logmon.LevelWarn
case "error":
level = logmon.LevelError
}
timeFormat := logTimeFormats[strings.ToLower(strings.TrimSpace(cfg.LogTimeFormat))]
for _, lg := range []*logmon.Monitor{proxyLog, upstreamLog} {
lg.SetLogLevel(level)
lg.SetLogTimeFormat(timeFormat)
}
}
applyLogSettings(cfg)
proxyLog.Debugf("PID: %d", os.Getpid())
// On Windows, bind the process tree to a Job Object so every upstream
// process is reaped when llama-swap exits — even on a forced kill. No-op
// elsewhere. Non-fatal: a failure just falls back to per-process teardown.
if err := process.SetupTreeCleanup(); err != nil {
proxyLog.Warnf("failed to set up process tree cleanup: %v", err)
}
// perfMon outlives config reloads; its config is updated in place.
var perfMon *perf.Monitor
if !cfg.Performance.Disabled {
perfMon, err = perf.New(cfg.Performance, proxyLog)
if err != nil {
slog.Error("failed to create performance monitor", "error", err)
os.Exit(1)
}
perfMon.Start()
} else {
proxyLog.Info("performance monitoring is disabled")
}
buildInfo := server.BuildInfo{Version: version, Commit: commit, Date: date}
initialSrv, err := server.New(cfg, muxLog, proxyLog, upstreamLog, perfMon, buildInfo)
if err != nil {
slog.Error("failed to create server", "error", err)
os.Exit(1)
}
// activeSrv is swapped atomically during hot reload.
var activeMu sync.RWMutex
activeSrv := initialSrv
httpServer := &http.Server{
Addr: listenAddr,
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
activeMu.RLock()
srv := activeSrv
activeMu.RUnlock()
srv.ServeHTTP(w, r)
}),
}
// reload guards against overlapping reloads triggered by concurrent signals
// or file-watcher callbacks.
var reloading bool
var reloadMu sync.Mutex
reload := func() {
reloadMu.Lock()
if reloading {
reloadMu.Unlock()
return
}
reloading = true
reloadMu.Unlock()
defer func() {
reloadMu.Lock()
reloading = false
reloadMu.Unlock()
}()
proxyLog.Info("reloading configuration")
newCfg, err := config.LoadConfigSources(*flagConfig, *flagConfigDir)
if err != nil {
proxyLog.Warnf("failed to reload config: %v", err)
return
}
if len(newCfg.Profiles) > 0 {
proxyLog.Warn("Profile functionality has been removed in favor of Groups. See the README for more information.")
}
if perfMon != nil {
perfMon.UpdateConfig(newCfg.Performance)
}
newSrv, err := server.New(newCfg, muxLog, proxyLog, upstreamLog, perfMon, buildInfo)
if err != nil {
proxyLog.Warnf("failed to build new server during reload: %v", err)
return
}
activeMu.Lock()
old := activeSrv
activeSrv = newSrv
activeMu.Unlock()
applyLogSettings(newCfg)
if err := old.Shutdown(shutdownTimeout); err != nil {
proxyLog.Warnf("error shutting down old server during reload: %v", err)
}
// Notify UI after a short delay so it can refresh model state.
time.AfterFunc(3*time.Second, func() {
event.Emit(shared.ConfigFileChangedEvent{State: shared.ReloadingStateEnd})
})
proxyLog.Info("configuration reloaded")
}
watcherCtx, watcherCancel := context.WithCancel(context.Background())
defer watcherCancel()
if *flagWatchConfig {
proxyLog.Info("watching configuration for changes (poll-based, 2s interval)")
if *flagConfig != "" {
absConfigPath, err := filepath.Abs(*flagConfig)
if err != nil {
slog.Error("watch-config: failed to resolve config path", "error", err)
os.Exit(1)
}
go func() {
(&configwatcher.Watcher{
Path: absConfigPath,
Interval: configwatcher.DefaultInterval,
OnChange: reload,
}).Run(watcherCtx)
}()
}
if *flagConfigDir != "" {
absConfigDir, err := filepath.Abs(*flagConfigDir)
if err != nil {
slog.Error("watch-config: failed to resolve config-dir path", "error", err)
os.Exit(1)
}
go func() {
(&configwatcher.DirWatcher{
Path: absConfigDir,
Interval: configwatcher.DefaultInterval,
OnChange: reload,
}).Run(watcherCtx)
}()
}
}
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
go func() {
var startErr error
if useTLS {
proxyLog.Infof("llama-swap listening with TLS on https://%s", listenAddr)
startErr = httpServer.ListenAndServeTLS(*flagCertFile, *flagKeyFile)
} else {
proxyLog.Infof("llama-swap listening on http://%s", listenAddr)
startErr = httpServer.ListenAndServe()
}
if startErr != nil && !errors.Is(startErr, http.ErrServerClosed) {
slog.Error("http server error", "error", startErr)
os.Exit(1)
}
}()
if !shared.IsLoopbackAddr(listenAddr) {
_, port, _ := net.SplitHostPort(listenAddr)
proxyLog.Infof("llama-swap is reachable by all hosts on the network, use -listen localhost:%s to restrict to loopback only", port)
}
exitChan := make(chan struct{})
go func() {
for {
sig := <-sigChan
switch sig {
case syscall.SIGHUP:
proxyLog.Info("received SIGHUP, reloading config")
go reload()
case syscall.SIGINT, syscall.SIGTERM:
proxyLog.Infof("received signal %v, shutting down", sig)
watcherCancel()
// Backstop against a stalled shutdown: force the process to
// exit once the whole graceful sequence has had its full budget.
// On Windows the Job Object reaps upstream processes on exit, so
// a forced exit still cleans up rather than orphaning children.
go func() {
time.Sleep(shutdownTimeout + 5*time.Second)
proxyLog.Warnf("graceful shutdown exceeded %v, forcing exit", shutdownTimeout)
os.Exit(1)
}()
activeMu.RLock()
srv := activeSrv
activeMu.RUnlock()
// Close long-lived SSE streams first so httpServer.Shutdown can
// drain without blocking on them for the full timeout.
srv.CloseStreams()
// Both phases share a single deadline so total shutdown is
// bounded by shutdownTimeout rather than 2x it.
deadline := time.Now().Add(shutdownTimeout)
shutdownCtx, cancel := context.WithDeadline(context.Background(), deadline)
defer cancel()
if err := httpServer.Shutdown(shutdownCtx); err != nil {
proxyLog.Warnf("http server shutdown error: %v", err)
}
// Clamp the remaining budget to a small positive value: a
// non-positive timeout makes the router fall back to its own
// healthCheckTimeout, which would defeat the shared deadline.
remaining := time.Until(deadline)
if remaining <= 0 {
remaining = time.Millisecond
}
if err := srv.Shutdown(remaining); err != nil {
proxyLog.Warnf("router shutdown error: %v", err)
}
if perfMon != nil {
perfMon.Stop()
}
close(exitChan)
return
}
}
}()
<-exitChan
proxyLog.Info("shutdown complete")
}