Skip to content
This repository has been archived by the owner on Apr 4, 2022. It is now read-only.

batchWriter creates buffers as required, without any constraint #3

Open
xiam opened this issue Dec 7, 2015 · 0 comments
Open

batchWriter creates buffers as required, without any constraint #3

xiam opened this issue Dec 7, 2015 · 0 comments

Comments

@xiam
Copy link

xiam commented Dec 7, 2015

Consider the following program that creates 500 concurrent requests to slightly different URLs:

package main

import (
        "crypto/rand"
        "errors"
        "fmt"
        //"github.com/goware/httpcoala"
        "github.com/pressly/chi"
        "github.com/pressly/chi/middleware"
        "log"
        "net/http"
        "sync"
        "time"
)

var mockData []byte

func init() {
        mockData = make([]byte, 1024*1024*20)
        rand.Read(mockData)
}

func doRequest(i int) error {
        res, err := http.Get(fmt.Sprintf("http://127.0.0.1:1111/?_=%d", i))
        if err != nil {
                return err
        }
        defer res.Body.Close()

        if res.StatusCode != 200 {
                return errors.New("Expecting 200 OK.")
        }

        return err
}

func main() {

        r := chi.NewRouter()

        r.Use(middleware.Logger)
        //r.Use(httpcoala.Route("HEAD", "GET")) // or, Route("*")

        r.Get("/", func(w http.ResponseWriter, r *http.Request) {
                time.Sleep(time.Second * 2) // expensive op
                w.WriteHeader(200)
                w.Write(mockData)
        })

        go http.ListenAndServe(":1111", r)

        time.Sleep(time.Second * 1)

        var wg sync.WaitGroup

        for i := 0; i < 500; i++ {
                wg.Add(1)
                go func(i int) {
                        if err := doRequest(i); err != nil {
                                log.Fatal("doRequest: ", err)
                        }
                        wg.Done()
                }(i)
        }

        wg.Wait()
}

If you run this program on a 2G vm nothing interesting happens, but if you remove the commented out lines to use httpcoala and run the program again you'll see something like this:

screen shot 2015-12-07 at 2 45 16 pm

You'll probably have to ^C it to prevent the vm from crashing.

I think the problem is that httpcoala tries to allocate as many buffers as possible, without imposing any hard limit.

# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant