I created a REST API in GoLang (with gorillamux), and in one request from my API, I process an XMLFile. The problem is, this file is big, 5, 6, 10 MB. I can't process in postfile request, because the time for process. Each node of xml is an http request to another API (very time).
The question is.
In GoLang, its possible receive the request, return response (200), and process file out of request?
In GoLang, its possible receive the request, return response (200), and process file out of request?
Of course. You can use a goroutine:
func myHandler(w http.ResponseWriter, r *http.Request) {
go func() {
// Process the stuff in a goroutine
}()
w.WriteHeader(200) // but send the response immediately
}
Yes. Go provides very convenient tools for it. One of classical approaches to this task is to make a channel which will serve as a task queue. Your API function will write to the channel new tasks and another worker goroutine will read and process them :
type Task struct {...} // some fields to describe you task - may be XML document
var TaskQueue chan Task
func worker() {
for task := range TaskQueue {
// process task
}
}
func handler(w http.ResponseWriter, r*http.Request) {
task := Task{...} // put here some values you need
TaskQueue <- task
w.WriteHeader(200)
}
func main() {
TaskQueue = make(chan Task, 1)
go worker()
http.Handle("/", handler)
http.ListenAndServe(":8080", nil)
}
This way gives you some flexibility:
make
sets queue lengthhandler
and return an error (not shown here)