Skip to content

Commit

Permalink
Add timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Oct 31, 2015
1 parent 2f211c3 commit a1c93c1
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions cmd/appspot/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"time"

"github.com/moul/wookie"
)
Expand All @@ -27,8 +28,29 @@ func handler(w http.ResponseWriter, r *http.Request) {
return
}
wook := wookie.NewWookie(resolveRequest.Sequences)
wook.Compute()
output := wook.Genome.String()

fmt.Fprintf(w, "%v\n", output)
// Timeout
timeout := make(chan bool, 1)
go func() {
time.Sleep(time.Second * 3)
timeout <- true
}()

computed := make(chan bool, 1)
go func() {
err = wook.Compute()
if err != nil {
fmt.Fprintf(w, "%v", err)
} else {
fmt.Fprintf(w, "%s", wook.Genome.String())
}
computed <- true
}()

select {
case <-computed:
// compute finished
case <-timeout:
fmt.Fprintf(w, "%s", "Timeout, this webapp times out after 3 seconds.\nUse the CLI version if you need to compute bigger genomes.\n\n https://github.com/moul/wookie\n")
}
}

0 comments on commit a1c93c1

Please sign in to comment.