experiment
This commit is contained in:
+13
-6
@@ -1,19 +1,21 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const PORT = 8080
|
const PORT = 8080
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
server := Server{}
|
server := NewServer(3 * time.Second)
|
||||||
|
|
||||||
server.Handle(
|
server.Handle(
|
||||||
"/get-random-number",
|
"/get-random-number",
|
||||||
printHello,
|
addValueToContext,
|
||||||
handleGetRandomNumber,
|
handleGetRandomNumber,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -25,14 +27,19 @@ func main() {
|
|||||||
|
|
||||||
func handleGetRandomNumber(next http.Handler) http.Handler {
|
func handleGetRandomNumber(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Println("Random number: ", rand.Int())
|
fmt.Println("Random number: ", r.Context().Value("number"))
|
||||||
|
w.Write([]byte(`{"error": "process timeout"}`))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func printHello(next http.Handler) http.Handler {
|
func addValueToContext(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Println("hello")
|
n := rand.Intn(50)
|
||||||
|
ctx := context.WithValue(r.Context(), "number", n)
|
||||||
|
fmt.Println("add value to context: ", n)
|
||||||
|
|
||||||
next.ServeHTTP(w, r)
|
time.Sleep(10 * time.Second)
|
||||||
|
|
||||||
|
next.ServeHTTP(w, r.WithContext(ctx))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
+37
-3
@@ -1,20 +1,32 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ServerHandler func(http.Handler) http.Handler
|
type ServerHandler func(http.Handler) http.Handler
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
|
timeout time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Server) Handle(addr string, handlers ...ServerHandler) {
|
func NewServer(timeout time.Duration) *Server {
|
||||||
http.Handle(addr, handleMiddlewares(handlers))
|
return &Server{
|
||||||
|
timeout,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Server) Listen(port int) error {
|
func (s *Server) Handle(addr string, handlers ...ServerHandler) {
|
||||||
|
middlewares := append([]ServerHandler{s.ctxMiddleware}, handlers...)
|
||||||
|
|
||||||
|
http.Handle(addr, handleMiddlewares(middlewares))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) Listen(port int) error {
|
||||||
if err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil); err != nil {
|
if err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil); err != nil {
|
||||||
return fmt.Errorf("server broke: %s", err.Error())
|
return fmt.Errorf("server broke: %s", err.Error())
|
||||||
}
|
}
|
||||||
@@ -22,6 +34,28 @@ func (s Server) Listen(port int) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Server) ctxMiddleware(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
ctx, cancel := context.WithTimeout(r.Context(), s.timeout)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
processDone := make(chan bool)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
next.ServeHTTP(w, r.WithContext(ctx))
|
||||||
|
processDone <- true
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
w.Write([]byte(`{"error": "context expired"}`))
|
||||||
|
log.Panicln("context expired")
|
||||||
|
case <-processDone:
|
||||||
|
fmt.Println("process done")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func handleMiddlewares(handlers []ServerHandler) http.Handler {
|
func handleMiddlewares(handlers []ServerHandler) http.Handler {
|
||||||
var handler http.Handler
|
var handler http.Handler
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user