Skip to content

Commit

Permalink
Merge pull request #13 from jamesmoriarty/refinement/cleanup-shutdown
Browse files Browse the repository at this point in the history
refinement: cleanup shutdown code.
  • Loading branch information
jamesmoriarty authored Jan 6, 2021
2 parents 650b2ee + 69bad46 commit e4edd09
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
goforward.exe
goforward.test.exe
goforward
12 changes: 3 additions & 9 deletions cmd/goforward.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,8 @@ func main() {

flag.Parse()

shutdown := make(chan bool, 1)

shutdown := make(chan os.Signal, 1)
signal.Notify(shutdown, syscall.SIGINT, syscall.SIGTERM)
go goforward.Listen(port, rate, shutdown)

wait := make(chan os.Signal, 1)
signal.Notify(wait, syscall.SIGINT, syscall.SIGTERM)

<-wait

shutdown <- true
<-shutdown
}
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e h1:N7DeIrjYszNmSW409R3frPPwglRwMkXSBzwVbkOjLLA=
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200501145240-bc7a7d42d5c3 h1:5B6i6EAiSYyejWfvc5Rc9BbI3rzIsrrXfAQBWnYfn+w=
golang.org/x/sys v0.0.0-20200501145240-bc7a7d42d5c3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
3 changes: 2 additions & 1 deletion goforward.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io"
"net"
"net/http"
"os"
"time"
)

Expand Down Expand Up @@ -114,7 +115,7 @@ func copyHeader(dst, src http.Header) {
}
}

func Listen(port string, rate int, shutdown <-chan bool) {
func Listen(port string, rate int, shutdown <-chan os.Signal) {
log.Info("Goforward listening on :" + port + " with ratelimit " + bytefmt.ByteSize(uint64(rate)))

bucket := ratelimit.NewBucketWithRate(float64(rate), int64(rate))
Expand Down
16 changes: 9 additions & 7 deletions goforward_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"net/url"
"os"
"syscall"
"testing"
"time"
)
Expand Down Expand Up @@ -54,14 +55,15 @@ func TestBenchmarks(t *testing.T) {
}

with("8080", ".", func() {
shutdown := make(chan bool, 1)
shutdown := make(chan os.Signal, 1)

for _, b := range benchmarks {
go Listen("8888", b.Rate, shutdown)

fileName := "goforward.exe"
proxyURL, _ := url.Parse("http://127.0.0.1:8888")
client := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)}}
response, err := client.Get("http://127.0.0.1:8080/goforward.exe")
response, err := client.Get("http://127.0.0.1:8080/" + fileName)

if err != nil {
t.Errorf(err.Error())
Expand All @@ -79,19 +81,19 @@ func TestBenchmarks(t *testing.T) {
t.Errorf(err.Error())
}

DurationExpected := (float64)(bytes("./goforward.exe") / b.Rate)
DurationExpected := (float64)(bytes("./"+fileName) / b.Rate)

fmt.Printf("for %v@%v took %v expected %v\n", bytes("./goforward.exe")/1024, b.Rate/1024, duration.Seconds(), DurationExpected)
fmt.Printf("for %v@%v took %v expected %v\n", bytes("./"+fileName)/1024, b.Rate/1024, duration.Seconds(), DurationExpected)

if duration.Seconds() > (DurationExpected * 1.2) {
t.Errorf("for %v@%v took %v expected <%v", bytes("./goforward.exe")/1024, b.Rate/1024, duration.Seconds(), DurationExpected)
t.Errorf("for %v@%v took %v expected <%v", bytes("./"+fileName)/1024, b.Rate/1024, duration.Seconds(), DurationExpected)
}

if duration.Seconds() < (DurationExpected * 0.8) {
t.Errorf("for %v@%v took %v expected >%v", bytes("./goforward.exe")/1024, b.Rate/1024, duration.Seconds(), DurationExpected)
t.Errorf("for %v@%v took %v expected >%v", bytes("./"+fileName)/1024, b.Rate/1024, duration.Seconds(), DurationExpected)
}

shutdown <- true
shutdown <- syscall.SIGKILL

time.Sleep(3)
}
Expand Down

0 comments on commit e4edd09

Please sign in to comment.