thecodingmachine/gotenberg-go-client

Exposing localhost:3000 behind gotenberg-go-client slows POST requests 5x

nlassaux opened this issue · 1 comments

Expected Behavior

I would expect the speed between doing a direct CURL to localhost:3000 and using gotenberg-go-client on top to be comparable.

Current Behavior

On the same document (.xlsx):

  • CURL: 1.69s
  • Go Equivalent: 9.16s

Possible Reasons

  • Non-equivalent POST requests, but I don't know why
  • Some potential overhead on the go client

Steps to Reproduce (for bugs)

CURL:

curl --request POST \
  --url http://localhost:3000/convert/office \
  --header 'content-type: multipart/form-data; boundary=---011000010111000001101001' \
  --form files=file.xlsx

Go Version:

package main

import (
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"net/http"

	"github.com/gorilla/mux"
	"github.com/thecodingmachine/gotenberg-go-client/v7"
)

// GotembergClient holds everything needed to call the Gotember Server
type GotembergClient struct {
	client *gotenberg.Client
}

func (h *GotembergClient) exportHelper(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/pdf")
	w.Header().Set("Access-Control-Allow-Origin", "*")
	w.Header().Set("Access-Control-Allow-Headers", "content-type")
	w.Header().Set("Content-Disposition", "attachment; filename=response.pdf")

	if r.Method == http.MethodOptions {
		w.WriteHeader(http.StatusOK)
		return
	}

	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		log.Fatal(err)
	}

	index, err := gotenberg.NewDocumentFromBytes("request.xlsx", body)
	if err != nil {
		log.Fatal(err)
	}
	req := gotenberg.NewOfficeRequest(index)

	fmt.Println("Pre-POST")
	resp, err := h.client.Post(req)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("Post-POST")

	if _, err := io.Copy(w, resp.Body); err != nil {
		fmt.Println(err)
		w.WriteHeader(500)
	}
	return
}

func main() {
	client := &gotenberg.Client{Hostname: "http://localhost:3000"}
	exportHelper := GotembergClient{client: client}

	r := mux.NewRouter()
	r.HandleFunc("/export", exportHelper.exportHelper).Methods(http.MethodPost, http.MethodOptions)
	r.Use(mux.CORSMethodMiddleware(r))
	log.Fatal(http.ListenAndServe(":8080", r))
}

Context

I don't want to expose the Gotenberg directly, I read here and there that it'd be better to expose it behind an HTTP server. That's what I did with that server written in Go.

Your Environment

  • Version used: github.com/thecodingmachine/gotenberg-go-client/v7
  • Operating System and version: Running the http server on go1.13.4 darwin/amd64
  • The server is in a GKE cluster.

Problem solved: I was not reading the form correctly, which send huge and incorrect files to the Gotenberg server, resulting in long delays and incorrect outputs.