Question

The problem is this: There is a web server. I figured that it would be beneficial to use goroutines in page loading, so I went ahead and did: called loadPage function as a goroutine. However, when doing this, the server simply stops working without errors. It prints a blank, white page. The problem has to be in the function itself- something there is conflicting with the goroutine somehow.

These are the relevant functions:

func loadPage(w http.ResponseWriter, path string) {
   s := GetFileContent(path)
   w.Header().Add("Content-Type", getHeader(path))
   w.Header().Add("Content-Length", GetContentLength(path))
   fmt.Fprint(w, s)
}
func GetFileContent(path string) string {
   cont, err := ioutil.ReadFile(path)
   e(err)
   aob := len(cont)
   s := string(cont[:aob])
   return s
}


func GetFileContent(path string) string {
   cont, err := ioutil.ReadFile(path)
   e(err)
   aob := len(cont)
   s := string(cont[:aob])
   return s
}

func getHeader(path string) string {
   images := []string{".jpg", ".jpeg", ".gif", ".png"}
   readable := []string{".htm", ".html", ".php", ".asp", ".js", ".css"}
   if ArrayContainsSuffix(images, path) {
      return "image/jpeg"
   }
   if ArrayContainsSuffix(readable, path) {
      return "text/html"
   }
   return "file/downloadable"
}


func ArrayContainsSuffix(arr []string, c string) bool {
   length := len(arr)
   for i := 0; i < length; i++ {
      s := arr[i]
      if strings.HasSuffix(c, s) {
         return true
      }
   }
return false
}
Was it helpful?

Solution

The reason why this happens is because your HandlerFunc which calls "loadPage" is called synchronously with the request. When you call it in a go routine the Handler is actually returning immediately, causing the response to be sent immediately. That's why you get a blank page.

You can see this in server.go (line 1096):

serverHandler{c.server}.ServeHTTP(w, w.req)
if c.hijacked() {
    return
}
w.finishRequest()

The ServeHTTP function calls your handler, and as soon as it returns it calls "finishRequest". So your Handler function must block as long as it wants to fulfill the request.

Using a go routine will actually not make your page any faster. Synchronizing a singe go routine with a channel, as Philip suggests, will also not help you in this case as that would be the same as not having the go routine at all.

The root of your problem is actually ioutil.ReadFile, which buffers the entire file into memory before sending it.

If you want to stream the file you need to use os.Open. You can use io.Copy to stream the contents of the file to the browser, which will used chunked encoding.

That would look something like this:

f, err := os.Open(path)
if err != nil {
    http.Error(w, "Not Found", http.StatusNotFound)
    return
}
n, err := io.Copy(w, f)
if n == 0 && err != nil {
    http.Error(w, "Error", http.StatusInternalServerError)
    return
}

If for some reason you need to do work in multiple go routines, take a look at sync.WaitGroup. Channels can also work.

If you are trying to just serve a file, there are other options that are optimized for this, such as FileServer or ServeFile.

OTHER TIPS

In the typical web framework implementations in Go, the route handlers are invoked as Goroutines. I.e. at some point the web framework will say go loadPage(...).

So if you call a Go routine from inside loadPage, you have two levels of Goroutines.

The Go scheduler is really lazy and will not execute the second level if it's not forced to. So you need to enforce it through synchronization events. E.g. by using channels or the sync package. Example:

func loadPage(w http.ResponseWriter, path string) {
  s := make(chan string)
  go GetFileContent(path, s)
  fmt.Fprint(w, <-s)
}

The Go documentation says this:

If the effects of a goroutine must be observed by another goroutine, use a synchronization mechanism such as a lock or channel communication to establish a relative ordering.

Why is this actually a smart thing to do? In larger projects you may deal with a large number of Goroutines that need to be coordinated somehow efficiently. So why call a Goroutine if it's output is used nowhere? A fun fact: I/O operations like fmt.Printf do trigger synchronization events too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top