-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
87 lines (73 loc) · 2.64 KB
/
main.go
File metadata and controls
87 lines (73 loc) · 2.64 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
package main
import (
"fmt"
"log"
"net/http"
)
const maxRequestsPerGroup = 1 // Threshold to define how much request forms a group
const numWorkers = 1 // number of preprocess workers
func main() {
// Start worker goroutines for precprocessing and grouping requests
for i := 0; i < numWorkers; i++ {
go worker()
}
// Start a single goroutine for the sequential processing of request groups
go sequentialProcessor()
// Start a Request Handler
http.HandleFunc("/", handleRequest)
log.Fatal(http.ListenAndServe(":8080", nil))
}
// handleRequest processes incoming HTTP requests and enqueues them to request channel
func handleRequest(w http.ResponseWriter, r *http.Request) {
log.Println("Received HTTP request:")
// Print the incoming request information
if r == nil {
http.Error(w, "Request is nil", http.StatusBadRequest)
return
}
log.Printf("Request Content: %v", r)
// Parse and enqueue the request
request := parseRequest(r)
if request.Model == "" {
// Respond with 400 Bad Request if parsing failed or request is empty
http.Error(w, "Invalid request", http.StatusBadRequest)
return
}
requestChannel <- request
// Acknowledge receipt of the request
w.WriteHeader(http.StatusAccepted)
fmt.Fprintf(w, "Request received for model: %s", request.Model)
}
// worker processes requests from the requestChannel in parallel, groups them and do preprocessing
func worker() {
preprocessor := Preprocessor{}
for req := range requestChannel {
model := req.Model
mu.Lock()
group := getOrCreateRequestGroup(model) // check if there is a forming group of this type of request
addRequestToGroup(&group, req) // add to the forming group
modelGroups[model] = group
if len(group.Requests) >= maxRequestsPerGroup { // check if forming group has enough request to form a complete group
delete(modelGroups, model) // pop the group
mu.Unlock()
preprocessor.process(&group) // preprocess the complete group
processChannel <- group // enqueue to prcessChannel for seqential processor
} else {
mu.Unlock()
}
}
}
// sequentialProcessor handles the Decide and Assign steps sequentially
func sequentialProcessor() {
processor := Processor{}
assigner := Assigner{}
for group := range processChannel {
log.Printf("Processing group sequentially for model: %s", group.Requests[0].Model)
serviceSpec := processor.DecideService(group) // decide the service spec
if serviceSpec.CPU == 0 {
log.Println("Failed to decide service spec, skipping request group")
continue // skip this group if service spec is invalid
}
assigner.AssignService(serviceSpec, group) // create the service and forward the request
}
}