Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
be58d93
commit 7c22c06
Showing
3 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
chapter-B.23-handle-cancelled-http-request/1-handle-cancelled-request.go.temp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"strings" | ||
"time" | ||
) | ||
|
||
func handleIndex(w http.ResponseWriter, r *http.Request) { | ||
done := make(chan bool) | ||
go func() { | ||
// do the process here | ||
// simulate a long-time request by putting 10 seconds sleep | ||
time.Sleep(10 * time.Second) | ||
|
||
done <- true | ||
}() | ||
|
||
select { | ||
case <-r.Context().Done(): | ||
if err := r.Context().Err(); err != nil { | ||
if strings.Contains(strings.ToLower(err.Error()), "canceled") { | ||
log.Println("request canceled") | ||
} else { | ||
log.Println("unknown error occured.", err.Error()) | ||
} | ||
} | ||
case <-done: | ||
log.Println("done") | ||
} | ||
} | ||
|
||
func main() { | ||
http.HandleFunc("/", handleIndex) | ||
http.ListenAndServe(":8080", nil) | ||
} |
43 changes: 43 additions & 0 deletions
43
chapter-B.23-handle-cancelled-http-request/2-handle-cancelled-request-with-payload.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package main | ||
|
||
import ( | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
"strings" | ||
"time" | ||
) | ||
|
||
func handleIndex(w http.ResponseWriter, r *http.Request) { | ||
done := make(chan bool) | ||
go func() { | ||
// do the process here | ||
// simulate a long-time request by putting 10 seconds sleep | ||
|
||
body, err := ioutil.ReadAll(r.Body) | ||
_ = err | ||
_ = body | ||
|
||
time.Sleep(10 * time.Second) | ||
|
||
done <- true | ||
}() | ||
|
||
select { | ||
case <-r.Context().Done(): | ||
if err := r.Context().Err(); err != nil { | ||
if strings.Contains(strings.ToLower(err.Error()), "canceled") { | ||
log.Println("request canceled") | ||
} else { | ||
log.Println("unknown error occured.", err.Error()) | ||
} | ||
} | ||
case <-done: | ||
log.Println("done") | ||
} | ||
} | ||
|
||
func main() { | ||
http.HandleFunc("/", handleIndex) | ||
http.ListenAndServe(":8080", nil) | ||
} |