-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathserver.go
More file actions
223 lines (186 loc) · 5.75 KB
/
Copy pathserver.go
File metadata and controls
223 lines (186 loc) · 5.75 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
package internal
import (
"context"
"fmt"
"net"
"net/http"
"os"
"os/signal"
"runtime"
"sync"
"syscall"
"time"
)
// Constants for timeout values
const (
shutdownTimeout = 10 * time.Second
// HTTP client configuration
maxIdleConns = 100
maxIdleConnsPerHost = 20
workerMultiplier = 2
)
// Server represents the HTTP server and its dependencies
type Server struct {
config *Config
httpServer *http.Server
httpClient *http.Client
workerPool *WorkerPool
}
// WorkerPool handles background processing
type WorkerPool struct {
workers int
jobQueue chan func()
quit chan bool
wg sync.WaitGroup
}
// NewWorkerPool creates a new worker pool
func NewWorkerPool(workers int) *WorkerPool {
if workers <= 0 {
workers = runtime.NumCPU()
}
wp := &WorkerPool{
workers: workers,
jobQueue: make(chan func(), workers*workerMultiplier), // Buffer for burst traffic
quit: make(chan bool),
}
wp.start()
return wp
}
func (wp *WorkerPool) start() {
for i := 0; i < wp.workers; i++ {
wp.wg.Add(1)
go func() {
defer wp.wg.Done()
for {
select {
case job := <-wp.jobQueue:
job()
case <-wp.quit:
return
}
}
}()
}
}
// Submit adds a job to the worker pool
func (wp *WorkerPool) Submit(job func()) {
wp.jobQueue <- job
}
// Stop gracefully stops the worker pool
func (wp *WorkerPool) Stop() {
close(wp.quit)
wp.wg.Wait()
}
// CreateHTTPClient creates a configured HTTP client
func CreateHTTPClient(cfg *Config) *http.Client {
return &http.Client{
Timeout: time.Duration(cfg.Timeouts.HTTPClient) * time.Second,
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
MaxIdleConns: maxIdleConns,
MaxIdleConnsPerHost: maxIdleConnsPerHost,
IdleConnTimeout: time.Duration(cfg.Timeouts.IdleConnTimeout) * time.Second,
DialContext: (&net.Dialer{
Timeout: time.Duration(cfg.Timeouts.DialTimeout) * time.Second,
KeepAlive: time.Duration(cfg.Timeouts.KeepAlive) * time.Second,
}).DialContext,
TLSHandshakeTimeout: time.Duration(cfg.Timeouts.TLSHandshake) * time.Second,
},
}
}
// NewServer creates a new server instance
func NewServer(cfg *Config, httpClient *http.Client) *Server {
workerPool := NewWorkerPool(runtime.NumCPU() * workerMultiplier)
// Create auth service
authService := NewAuthService(httpClient)
// Create coalescing cache for models
coalescingCache := NewCoalescingCache()
modelsService := NewModelsService(coalescingCache, httpClient)
// Create proxy service
proxyService := NewProxyService(cfg, httpClient, authService, workerPool)
// Create health checker
healthChecker := NewHealthChecker(httpClient, "dev") // TODO: get version from build
mux := http.NewServeMux()
mux.HandleFunc("/v1/models", modelsService.Handler())
mux.HandleFunc("/v1/chat/completions", proxyService.Handler())
mux.HandleFunc("/v1/responses", proxyService.Handler())
mux.HandleFunc("/health", healthChecker.Handler())
// Add pprof endpoints for profiling
mux.HandleFunc("/debug/pprof/", http.DefaultServeMux.ServeHTTP)
mux.HandleFunc("/debug/pprof/cmdline", http.DefaultServeMux.ServeHTTP)
mux.HandleFunc("/debug/pprof/profile", http.DefaultServeMux.ServeHTTP)
mux.HandleFunc("/debug/pprof/symbol", http.DefaultServeMux.ServeHTTP)
mux.HandleFunc("/debug/pprof/trace", http.DefaultServeMux.ServeHTTP)
port := cfg.Port
if port == 0 {
port = 8081 // default port
}
// Build middleware chain
var handler http.Handler = mux
// Apply middleware in reverse order (last applied = first executed)
handler = SecurityHeadersMiddleware(handler)
handler = CORSMiddleware(cfg)(handler)
handler = LoggingMiddleware(handler)
handler = RecoveryMiddleware(handler)
// Note: TimeoutMiddleware could be added here if needed per-request timeouts
// handler = TimeoutMiddleware(time.Duration(cfg.Timeouts.ProxyContext) * time.Second)(handler)
httpServer := &http.Server{
Addr: fmt.Sprintf(":%d", port),
Handler: handler,
ReadTimeout: time.Duration(cfg.Timeouts.ServerRead) * time.Second,
WriteTimeout: time.Duration(cfg.Timeouts.ServerWrite) * time.Second,
IdleTimeout: time.Duration(cfg.Timeouts.ServerIdle) * time.Second,
}
return &Server{
config: cfg,
httpServer: httpServer,
httpClient: httpClient,
workerPool: workerPool,
}
}
// Start starts the HTTP server with graceful shutdown
func (s *Server) Start() error {
s.setupGracefulShutdown()
port := s.config.Port
if port == 0 {
port = 8081
}
fmt.Printf("Starting GitHub Copilot proxy server on port %d...\n", port)
fmt.Printf("Endpoints:\n")
fmt.Printf(" - Models: http://localhost:%d/v1/models\n", port)
fmt.Printf(" - Chat: http://localhost:%d/v1/chat/completions\n", port)
fmt.Printf(" - Responses: http://localhost:%d/v1/responses\n", port)
fmt.Printf(" - Health: http://localhost:%d/health\n", port)
if err := s.httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
return fmt.Errorf("server failed: %v", err)
}
return nil
}
// Stop gracefully stops the server
func (s *Server) Stop() error {
ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
defer cancel()
fmt.Println("Stopping worker pool...")
s.workerPool.Stop()
fmt.Println("Worker pool stopped.")
fmt.Println("Shutting down HTTP server...")
err := s.httpServer.Shutdown(ctx)
if err != nil {
fmt.Printf("Error during HTTP server shutdown: %v\n", err)
return err
}
fmt.Println("HTTP server shutdown complete.")
return nil
}
func (s *Server) setupGracefulShutdown() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
fmt.Println("\nGracefully shutting down...")
if err := s.Stop(); err != nil {
Error("Server shutdown error", "error", err)
}
}()
}
// healthHandler is now replaced by the comprehensive HealthChecker