Skip to content

Instantly share code, notes, and snippets.

@myitcv
Created June 6, 2018 08:32
Show Gist options
  • Save myitcv/3d3be2b2647d0b5a2b689e9b5fea078e to your computer and use it in GitHub Desktop.
Save myitcv/3d3be2b2647d0b5a2b689e9b5fea078e to your computer and use it in GitHub Desktop.
GopherJS "leaking" repro
// this program demonstrates how fetches through gopherjs seem to be memory leaking.
package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"github.com/gopherjs/gopherjs/js"
)
func main() {
js.Global.Set("gocode", map[string]interface{}{
"fetch": fetch,
})
}
const (
maxFetch = 10000
fetchURL = "playground_gopherjs.js"
)
func fetch() {
go func() {
for i := 0; i < maxFetch; i++ {
fmt.Printf("Getting\n")
req, err := http.NewRequest("GET", fetchURL, nil)
if err != nil {
log.Fatal(err)
}
res, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
func() {
defer res.Body.Close()
if _, err := io.Copy(ioutil.Discard, res.Body); err != nil {
log.Fatal(err)
}
}()
}
}()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment