-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
122 lines (72 loc) · 1.98 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
package main
import (
"fmt"
"net/http"
"os"
"runtime"
"strconv"
"sync"
)
type Cushion struct {
wg sync.WaitGroup
MessageQueue chan string
url string
port string
//how much request buffering required
buffer int
cpus int
http *http.Server
mux *http.ServeMux
}
func (this *Cushion) InRequest(w http.ResponseWriter, r *http.Request) {
message := r.URL.RequestURI()
this.MessageQueue <- message
//w.WriteHeader(http.StatusOK)
//w.Write([]byte("Requests on port " + this.port))
}
func (this *Cushion) CallURL(requestURL string) {
defer this.wg.Done()
res, err := http.Get(this.url)
if err == nil {
res.Body.Close()
}
}
func (this *Cushion) OutRequest() {
for {
for loop := 0; loop < this.cpus; loop++ {
requestURL := <-this.MessageQueue
this.wg.Add(1)
go this.CallURL(requestURL)
}
this.wg.Wait()
}
}
func (this *Cushion) QueueSize(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(strconv.Itoa(len(this.MessageQueue)) + " requests in buffer"))
}
func (this *Cushion) Start(cpus int, buffer int) {
this.cpus = cpus
this.buffer = buffer
this.MessageQueue = make(chan string, this.buffer)
this.mux = http.NewServeMux()
this.mux.HandleFunc("/", this.InRequest)
this.mux.HandleFunc("/info", this.QueueSize)
this.http = &http.Server{Addr: this.port, Handler: this.mux}
go this.OutRequest()
go this.http.ListenAndServe()
fmt.Println("Forwarding to ", this.url, " listening on localhost port ", this.port)
}
func main() {
if len(os.Args) < 5 {
fmt.Println("Please pass Redirect URL Listener_Port Number_of_threads Buffer_Size")
os.Exit(0)
}
cpus, _ := strconv.Atoi(os.Args[3])
buffer, _ := strconv.Atoi(os.Args[4])
runtime.GOMAXPROCS(cpus)
//you can create mote objects like this in the next line and call its Start() function
apiservice := Cushion{url: os.Args[1], port: ":" + os.Args[2]}
apiservice.Start(cpus, buffer)
//This should be the last. All to be before this. This line is non-ending loop
select {}
}