-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproxy.go
462 lines (388 loc) · 13.7 KB
/
proxy.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
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"flag"
"fmt"
log "github.com/sirupsen/logrus"
"io"
"math/rand"
"net/http"
"os"
"os/signal"
"sync"
"syscall"
"time"
)
const (
OpenAIBaseURL = "https://api.openai.com/v1"
SleepDuration = 5 * time.Second
)
type batchKey struct {
auth string
endpoint string
}
var (
port = 3030
maxHoldBatchSend = 4 * time.Second
maxBatchSize = 1000 // OpenAI supports 50k, but tail latencies could be massive
maxBatchMb = 25 // OpenAI supports up to 100 MB
reqToBeBatchedMap sync.Map // key: batchKey, value: chan ProxyRequest
shutdownChan = make(chan struct{})
responseChanMap sync.Map // key: customID (id of a request), value: channel for the response
batchMap sync.Map // key: BatchResponse.ID, value: auth. So that we can cancel them on ctrl-c
)
func init() {
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
})
log.SetOutput(os.Stderr)
log.SetLevel(log.InfoLevel)
}
// go run . -port 8080 -max-hold-batch 5s -max-batch-size 500 -max-batch-mb 25
func main() {
flag.IntVar(&port, "port", port, "Port to run the server on")
flag.DurationVar(&maxHoldBatchSend, "max-hold-batch", maxHoldBatchSend, "Maximum time to hold a batch before sending")
flag.IntVar(&maxBatchSize, "max-batch-size", maxBatchSize, "Maximum number of requests in a batch")
flag.IntVar(&maxBatchMb, "max-batch-mb", maxBatchMb, "Maximum size of a batch in bytes")
flag.Parse()
log.Info("Starting server with maxHoldBatchSend: ", maxHoldBatchSend, ", maxBatchSize: ", maxBatchSize, ", maxBatchMb: ", maxBatchMb)
server := &http.Server{
Addr: fmt.Sprintf(":%d", port),
Handler: createMuxServer(),
}
go func() {
log.Infof("Server is running on :%d", port)
if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatalf("Server error: %v", err)
}
}()
// graceful shutdown
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
<-stop
log.Info("Shutting down server...")
// signal all goroutines to stop
close(shutdownChan)
cancelAllOutstandingBatches()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.Fatalf("Server forced to shutdown: %v", err)
}
log.Info("Server exiting")
}
func createMuxServer() *http.ServeMux {
mux := http.NewServeMux()
mux.HandleFunc("/v1/chat/completions", handleOpenaiPostEndpoint)
mux.HandleFunc("/v1/embeddings", handleOpenaiPostEndpoint)
mux.HandleFunc("/stats", handleStats)
mux.HandleFunc("/", handleNoopOpenaiProxy)
return mux
}
func cancelAllOutstandingBatches() {
var wg sync.WaitGroup
batchMap.Range(func(key, value interface{}) bool {
batchID := key.(string)
auth := value.(string)
wg.Add(1)
safeGo2(func(id, auth string) {
defer wg.Done()
log.Printf("Cancelling batch %s", id)
if err := cancelBatch(id, auth); err != nil {
log.Printf("Error cancelling batch %s: %v", id, err)
}
// http requests to this proxy in the batch will error out when the server shuts down
})(batchID, auth)
return true
})
// Wait for all cancellations
wg.Wait()
}
func handleOpenaiPostEndpoint(w http.ResponseWriter, r *http.Request) {
trackRequestStart()
start := time.Now()
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
body, err := io.ReadAll(r.Body)
if err != nil {
log.Errorf("Failed to read request body: %v", err)
http.Error(w, "Failed to read request body", http.StatusBadRequest)
return
}
defer r.Body.Close()
customID := fmt.Sprintf("req_%d", rand.Intn(1000000))
log.WithField("requestID", customID).Debugf("New request received for endpoint: %s", r.URL.Path)
responseChan := make(chan interface{})
responseChanMap.Store(customID, responseChan)
defer responseChanMap.Delete(customID)
var bodyMap map[string]interface{}
if err := json.Unmarshal(body, &bodyMap); err != nil {
http.Error(w, "Failed to parse request body", http.StatusBadRequest)
return
}
key := batchKey{
auth: r.Header.Get("Authorization"),
endpoint: r.URL.Path,
}
req := ProxyRequest{
CustomID: customID,
Method: "POST",
Endpoint: r.URL.Path,
Body: bodyMap,
}
value, loaded := reqToBeBatchedMap.LoadOrStore(key, make(chan ProxyRequest, 1))
ch := value.(chan ProxyRequest)
if !loaded {
log.Printf("[%s] Created a new channel for %+v", customID, key)
safeGo2(processUploadAndCreateBatch)(key, ch)
}
ch <- req
log.WithField("requestID", customID).Debug("Request sent to be batched")
response := <-responseChan
log.WithField("requestID", customID).Debug("Received response from batch")
trackRequestEnd(true, time.Since(start))
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(response)
}
func handleStats(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
stats := getStats()
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(stats)
}
func processUploadAndCreateBatch(key batchKey, reqToBeBatched chan ProxyRequest) {
var batch []ProxyRequest
var jsonlData bytes.Buffer
batchSize := 0
batchBytes := 0
maxBatchBytes := maxBatchMb * 1024 * 1024
batchStart := time.Now()
log.Printf("[Batch] Starting new batch for key %+v", key)
for {
select {
case req := <-reqToBeBatched:
log.WithFields(log.Fields{
"requestID": req.CustomID,
"key": key,
}).Debug("New request added to batch")
jsonReq, err := json.Marshal(req)
if err != nil {
log.Printf("[Batch] Failed to marshal proxy request: %v", err)
continue
}
jsonReq = append(jsonReq, '\n') // JSONL: each JSON in a new line
if batchSize >= maxBatchSize || batchBytes+len(jsonReq) > maxBatchBytes || len(batch) == 0 && len(jsonReq) > maxBatchBytes {
if len(batch) > 0 {
log.Printf("[Batch] Batch full, processing %d requests", len(batch))
safeGo4(processBatch)(jsonlData.Bytes(), key.auth, key.endpoint, outstandingCustomIDs(batch))
batch = nil
jsonlData.Reset()
batchSize = 0
batchBytes = 0
batchStart = time.Now()
}
}
batch = append(batch, req)
jsonlData.Write(jsonReq)
batchSize++
batchBytes += len(jsonReq)
log.WithFields(log.Fields{
"batchSize": batchSize,
"batchBytes": batchBytes,
}).Debug("Current batch status")
case <-time.After(200 * time.Millisecond):
if len(batch) > 0 && (time.Since(batchStart) >= maxHoldBatchSend || batchSize >= maxBatchSize) {
log.WithFields(log.Fields{
"requests": len(batch),
"timeSinceStart": time.Since(batchStart),
}).Info("Processing batch due to time or size limit")
safeGo4(processBatch)(jsonlData.Bytes(), key.auth, key.endpoint, outstandingCustomIDs(batch))
batch = nil
jsonlData.Reset()
batchSize = 0
batchBytes = 0
batchStart = time.Now()
}
case <-shutdownChan:
log.Info("Received shutdown signal")
if len(batch) > 0 {
log.WithField("requests", len(batch)).Info("Processing final batch before shutdown")
safeGo4(processBatch)(jsonlData.Bytes(), key.auth, key.endpoint, outstandingCustomIDs(batch))
}
reqToBeBatchedMap.Delete(key)
return
}
}
}
func processBatch(jsonlData []byte, auth, endpoint string, outstandingCustomIDs map[string]bool) {
trackBatchStart()
start := time.Now()
log.WithField("requests", len(outstandingCustomIDs)).Info("Starting to process batch")
fileID, err := uploadFile(jsonlData, auth)
if err != nil {
log.WithError(err).Error("Failed to upload file to OpenAI")
sendErrorToAllRequests(outstandingCustomIDs, fmt.Sprintf("Failed to upload file: %v", err))
trackBatchEnd(false, time.Since(start))
return
}
log.WithField("fileID", fileID).Info("File uploaded successfully")
batchID, err := createBatch(fileID, auth, endpoint)
if err != nil {
log.Printf("[ProcessBatch] Failed to create batch: %v", err)
if err := deleteFile(fileID, auth); err != nil {
log.Printf("[ProcessBatch] Warning: Failed to delete input file: %v", err)
}
sendErrorToAllRequests(outstandingCustomIDs, fmt.Sprintf("Failed to create batch: %v", err))
trackBatchEnd(false, time.Since(start))
return
}
log.Printf("[ProcessBatch] Batch created successfully, ID: %s", batchID)
// Store the batch ID and headers for potential cancellation
batchMap.Store(batchID, auth)
safeGo4(processBatchResponse)(batchID, auth, outstandingCustomIDs, start)
}
func processBatchResponse(batchID, auth string, outstandingCustomIDs map[string]bool, start time.Time) {
defer batchMap.Delete(batchID)
log.WithField("batchID", batchID).Info("Starting to process batch response")
batchResponse, err := pollBatchStatus(batchID, auth)
if err != nil {
log.WithError(err).Error("Failed batch or batch status")
sendErrorToAllRequests(outstandingCustomIDs, fmt.Sprintf("Batch processing failed: %v", err))
trackBatchEnd(false, time.Since(start))
return
}
log.WithFields(log.Fields{
"batchID": batchID,
"status": batchResponse.Status,
"outputFileID": batchResponse.OutputFileID,
"errorFileID": batchResponse.ErrorFileID,
}).Info("Batch status received")
filesToProcess := []*string{batchResponse.OutputFileID, batchResponse.ErrorFileID}
var waitDelete sync.WaitGroup
defer waitDelete.Wait()
for _, fileID := range filesToProcess {
if fileID == nil {
continue
}
jsonlContent, err := readFile(*fileID, auth)
if err != nil {
log.Printf("[ProcessBatchResponse] Failed to retrieve file %s: %v", *fileID, err)
continue
}
log.Printf("[ProcessBatchResponse] Successfully retrieved file %s. Content length: %d", *fileID, len(jsonlContent))
waitDelete.Add(1)
safeGo1(func(id string) {
defer waitDelete.Done()
if err := deleteFile(id, auth); err != nil {
log.Printf("[ProcessBatchResponse] Warning: Failed to delete file %s: %v", id, err)
}
})(*fileID)
processFileContent(jsonlContent, outstandingCustomIDs)
}
// Send error responses for any remaining outstanding requests. Shouldn't happen
for customID := range outstandingCustomIDs {
log.Printf("[ProcessBatchResponse] Sending error response for outstanding request ID: %s", customID)
sendErrorResponse(customID, "No response received for request ["+customID+"] in the batch")
}
trackBatchEnd(true, time.Since(start))
log.WithField("batchID", batchID).Info("Finished processing batch response")
}
func processFileContent(jsonlContent []byte, outstandingCustomIDs map[string]bool) {
for _, line := range bytes.Split(jsonlContent, []byte("\n")) {
if len(line) == 0 {
continue
}
var reqResponse BatchRequestResponse
if err := json.Unmarshal(line, &reqResponse); err != nil {
log.Printf("[ProcessFileContent] Failed to parse batch output line: %v", err)
continue
}
log.Printf("[ProcessFileContent] Processing response for request ID: %s", reqResponse.CustomID)
if ch, ok := responseChanMap.Load(reqResponse.CustomID); ok {
if reqResponse.Error != nil {
ch.(chan interface{}) <- map[string]interface{}{
"error": reqResponse.Error,
}
} else {
ch.(chan interface{}) <- reqResponse.Response.Body
}
close(ch.(chan interface{}))
delete(outstandingCustomIDs, reqResponse.CustomID)
log.Printf("[ProcessFileContent] Response sent for request ID: %s", reqResponse.CustomID)
} else {
log.Printf("[ProcessFileContent] No waiting request found for CustomID: %s", reqResponse.CustomID)
}
}
}
func outstandingCustomIDs(batch []ProxyRequest) map[string]bool {
m := make(map[string]bool)
for _, req := range batch {
m[req.CustomID] = true
}
return m
}
// Helper function to send error response for an individual request
func sendErrorResponse(customID string, errorMsg string) {
log.Printf("[ErrorResponse] Sending error response for request ID: %s, Error: %s", customID, errorMsg)
if ch, ok := responseChanMap.Load(customID); ok {
ch.(chan interface{}) <- map[string]interface{}{
"error": map[string]string{
"message": errorMsg,
},
}
close(ch.(chan interface{}))
log.Printf("[ErrorResponse] Error response sent and channel closed for request ID: %s", customID)
} else {
log.Printf("[ErrorResponse] No response channel found for request ID: %s\n", customID)
}
trackSynthesizedErrorResponse()
}
// Helper function to send error responses for all requests in a batch
func sendErrorToAllRequests(customIDs map[string]bool, errorMsg string) {
log.Printf("[BatchError] Sending error to %d requests: %s", len(customIDs), errorMsg)
for customID := range customIDs {
sendErrorResponse(customID, errorMsg)
}
}
// any other endpoint we don't handle, forward transparently
func handleNoopOpenaiProxy(w http.ResponseWriter, r *http.Request) {
log.WithField("path", r.URL.Path).Info("Forwarding request to OpenAI")
openAIURL := "https://api.openai.com" + r.URL.Path
proxyReq, err := http.NewRequest(r.Method, openAIURL, r.Body)
if err != nil {
log.Printf("[NoopProxy] Error creating proxy request: %v", err)
http.Error(w, "Error creating proxy request", http.StatusInternalServerError)
return
}
for name, values := range r.Header {
for _, value := range values {
proxyReq.Header.Add(name, value)
}
}
resp, err := httpClient.Do(proxyReq)
if err != nil {
log.Printf("[NoopProxy] Error forwarding request to OpenAI: %v", err)
http.Error(w, "Error forwarding request to OpenAI", http.StatusBadGateway)
return
}
defer resp.Body.Close()
for name, values := range resp.Header {
for _, value := range values {
w.Header().Add(name, value)
}
}
w.WriteHeader(resp.StatusCode)
if _, err := io.Copy(w, resp.Body); err != nil {
log.Printf("[NoopProxy] Error copying response body: %v", err)
}
log.WithField("path", r.URL.Path).Info("Successfully forwarded request and received response")
}