Skip to content

Commit ded57b1

Browse files
committed
Adding a new flag to customize endpoint to send GCI commands
1 parent 426b0f3 commit ded57b1

File tree

3 files changed

+31
-23
lines changed

3 files changed

+31
-23
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ GCI has two main parts: i) the GCI-Proxy -- a multiplatform, runtime-agnostic HT
1212
## Running GCI Proxy
1313

1414
```bash
15-
./gci-proxy --port 3000 --url http://localhost:8080 --ygen=67108864 --tgen=6710886
15+
./gci-proxy --port 3000 --url http://localhost:8080 --ygen=67108864 --tgen=6710886 --endpoint="/gci"
1616
```
1717

1818
Where the flags:
@@ -21,6 +21,7 @@ Where the flags:
2121
* --tgen: size in bytes of the tenured generation, e.g. 67108864 (64MB)
2222
* --port: port which the gci-proxy will listen, e.g. 3000
2323
* --url: URL of the server being proxied, e.g. --url http://localhost:8080
24+
* --endpoint: (optional) custom HTTP endpoint to send GCI-specific commands, e.g. --endpoint=/__gci
2425

2526

2627
## GCI Proxy Protocol

main.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,16 @@ func main() {
3232
// flags
3333
port := flag.String("port", defaultPort, defaultPortUsage)
3434
redirectURL := flag.String("url", defaultTarget, defaultTargetUsage)
35-
yGen := flag.Uint64("ygen", 0, "Invalid young generation size.")
36-
tGen := flag.Uint64("tgen", 0, "Invalid tenured generation size.")
35+
cmdEndpoint := flag.String("endpoint", "/", "Custom endpoint to send GCI-specific commands. For example, '/gci'")
36+
yGen := flag.Uint64("ygen", 0, "Young generation size, in bytes.")
37+
tGen := flag.Uint64("tgen", 0, "Tenured generation size, in bytes.")
3738
flag.Parse()
3839

3940
if *yGen == 0 || *tGen == 0 {
4041
log.Fatalf("Neither ygen nor tgen can be 0. ygen:%d tgen:%d", *yGen, *tGen)
4142
}
4243

43-
proxy := newProxy(*redirectURL, *yGen, *tGen)
44+
proxy := newProxy(*redirectURL, *cmdEndpoint, *yGen, *tGen)
4445
c := make(chan struct{}, 1)
4546
router := httprouter.New()
4647
router.HandlerFunc("GET", "/", func(w http.ResponseWriter, r *http.Request) {
@@ -72,6 +73,7 @@ type transport struct {
7273
isAvailable int32
7374
shed uint64
7475
target string
76+
cmdEndpoint string
7577
waiter pendingWaiter
7678
window sampleWindow
7779
stGen1 sheddingThreshold
@@ -139,7 +141,7 @@ func (t *transport) checkHeap() {
139141
arrived, finished := t.waiter.waitPending()
140142
t.window.update(finished)
141143

142-
req, err := http.NewRequest("GET", fmt.Sprintf("%s/", t.target), nil)
144+
req, err := http.NewRequest("GET", fmt.Sprintf("%s%s", t.target, t.cmdEndpoint), nil)
143145
if err != nil {
144146
panic(fmt.Sprintf("Err trying to build heap check request: %q\n", err))
145147
}
@@ -189,7 +191,7 @@ func shouldGC(pending, usedBytes, st uint64) bool {
189191
}
190192

191193
func (t *transport) gc(gen generation) {
192-
req, err := http.NewRequest("GET", fmt.Sprintf("%s/", t.target), nil)
194+
req, err := http.NewRequest("GET", fmt.Sprintf("%s%s", t.target, t.cmdEndpoint), nil)
193195
if err != nil {
194196
panic(fmt.Sprintf("Err trying to build gc request: %q\n", err))
195197
}
@@ -222,19 +224,20 @@ func (p *proxy) handle(w http.ResponseWriter, r *http.Request) {
222224
p.proxy.ServeHTTP(w, r)
223225
}
224226

225-
func newProxy(target string, yGen, tGen uint64) *proxy {
227+
func newProxy(target, cmdEndpoint string, yGen, tGen uint64) *proxy {
226228
url, _ := url.Parse(target)
227229
p := httputil.NewSingleHostReverseProxy(url)
228-
p.Transport = newTransport(target, yGen, tGen)
230+
p.Transport = newTransport(target, cmdEndpoint, yGen, tGen)
229231
return &proxy{target: url, proxy: p}
230232
}
231233

232-
func newTransport(target string, yGen, tGen uint64) *transport {
234+
func newTransport(target, cmdEndpoint string, yGen, tGen uint64) *transport {
233235
return &transport{
234-
target: target,
235-
window: newSampleWindow(),
236-
stGen1: newSheddingThreshold(time.Now().UnixNano(), yGen),
237-
stGen2: newSheddingThreshold(time.Now().UnixNano(), tGen),
236+
target: target,
237+
cmdEndpoint: cmdEndpoint,
238+
window: newSampleWindow(),
239+
stGen1: newSheddingThreshold(time.Now().UnixNano(), yGen),
240+
stGen2: newSheddingThreshold(time.Now().UnixNano(), tGen),
238241
}
239242
}
240243

main_test.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func TestProxyHandle(t *testing.T) {
6060
}))
6161
defer target.Close()
6262

63-
p := newProxy(target.URL, 1024, 1024)
63+
p := newProxy(target.URL, "/", 1024, 1024)
6464

6565
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
6666
p.handle(w, r)
@@ -85,24 +85,28 @@ func TestTransport_CheckHeapSize(t *testing.T) {
8585
data := []struct {
8686
msg string
8787
response string
88+
endpoint string
8889
}{
89-
{"onlyGen1", "1"},
90-
{"bothGens", fmt.Sprintf("1%s1", genSeparator)},
90+
{"onlyGen1_defaultEndpoint", "1", "/"},
91+
{"bothGens_defaultEndpoint", fmt.Sprintf("1%s1", genSeparator), "/"},
92+
{"onlyGen1_customEndpoint", "1", "/gci"},
93+
{"bothGens_customEndpoint", fmt.Sprintf("1%s1", genSeparator), "/gci"},
9194
}
9295
for _, d := range data {
9396
t.Run(d.msg, func(t *testing.T) {
9497
var wg sync.WaitGroup
9598
gotGCIHeapCheck := false
9699
target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
97-
if r.Header.Get(gciHeader) == heapCheckHeader {
100+
fmt.Println(r.URL.Path)
101+
if r.Header.Get(gciHeader) == heapCheckHeader && r.URL.Path == d.endpoint {
98102
fmt.Fprintf(w, d.response)
99103
gotGCIHeapCheck = true
100104
}
101105
wg.Done()
102106
}))
103107
defer target.Close()
104108

105-
server := proxyServer(target.URL, 1024, 1024)
109+
server := proxyServer(target.URL, d.endpoint, 1024, 1024)
106110
defer server.Close()
107111

108112
fireReqs(t, &wg, server.URL)
@@ -129,7 +133,7 @@ func TestTransport_GC(t *testing.T) {
129133
var wg sync.WaitGroup
130134
gcRan := false
131135
target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
132-
if r.Header.Get(gciHeader) == heapCheckHeader {
136+
if r.Header.Get(gciHeader) == heapCheckHeader && r.URL.Path == "/gci" {
133137
fmt.Fprintf(w, d.response)
134138
}
135139
if r.Header.Get(gciHeader) == d.gen {
@@ -139,7 +143,7 @@ func TestTransport_GC(t *testing.T) {
139143
}))
140144
defer target.Close()
141145

142-
server := proxyServer(target.URL, 1024, 1024)
146+
server := proxyServer(target.URL, "/gci", 1024, 1024)
143147
defer server.Close()
144148

145149
wg.Add(1)
@@ -152,8 +156,8 @@ func TestTransport_GC(t *testing.T) {
152156
}
153157
}
154158

155-
func proxyServer(target string, gen1, gen2 uint64) *httptest.Server {
156-
p := newProxy(target, gen1, gen2)
159+
func proxyServer(target, cmdEndpoint string, gen1, gen2 uint64) *httptest.Server {
160+
p := newProxy(target, cmdEndpoint, gen1, gen2)
157161
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
158162
p.handle(w, r)
159163
}))
@@ -178,7 +182,7 @@ func BenchmarkProxyHandle(b *testing.B) {
178182
}))
179183
defer target.Close()
180184

181-
p := newProxy(target.URL, 1024, 1024)
185+
p := newProxy(target.URL, "/", 1024, 1024)
182186

183187
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
184188
p.handle(w, r)

0 commit comments

Comments
 (0)