jacquayj / GoRODS

Golang binding for iRODS C API: An iRODS client library written in Golang + C
https://godoc.org/github.com/jjacquay712/GoRODS
BSD 3-Clause "New" or "Revised" License
17 stars 5 forks source link

Fix memory leak in DataObj.ReadChunk() #21

Closed jjacquay712 closed 8 years ago

jjacquay712 commented 8 years ago

Serving file over HTTP causes massive memory leak:

func main() {

    irods, err := gorods.New(gorods.ConnectionOptions {
        Type: gorods.UserDefined,

        Host: "localhost",
        Port: 1247,
        Zone: "tempZone",

        Username: "admin",
        Password: "-redacted-",
    })

    if err != nil {
        fmt.Printf("Error connecting to server: %v\n", err)
        os.Exit(1)
    }

    homeDir, err := irods.Collection("/tempZone/home/admin", false)
    if err != nil {
        p(err)
        os.Exit(1)
    }

    fileForDownload := homeDir.Get("centos.iso")

    r := mux.NewRouter()

    // Routes consist of a path and a handler function.
    r.HandleFunc("/", func (w http.ResponseWriter, r *http.Request) {

        w.Header().Set("Content-Disposition", "attachment; filename=centos.iso")
        w.Header().Set("Content-type", "application/octet-stream")
        w.Header().Set("Content-Length", strconv.FormatInt(fileForDownload.Size, 10))

        fileForDownload.ReadChunk(4096, func(chunk []byte) {
            w.Write(chunk)
        })

    })

    l, err := net.Listen("tcp4", ":8080")
    if err != nil {
        log.Fatal(err)
    }
    log.Fatal(http.Serve(l, r))

}