This commit is contained in:
unintendedfraud
2024-03-10 02:08:55 +01:00
parent 848b9fc51a
commit b638ed612b
2 changed files with 75 additions and 29 deletions
+66 -12
View File
@@ -3,7 +3,6 @@ package main
import ( import (
"context" "context"
"fmt" "fmt"
"math/rand"
"net/http" "net/http"
"time" "time"
) )
@@ -11,12 +10,12 @@ import (
const PORT = 8080 const PORT = 8080
func main() { func main() {
server := NewServer(3 * time.Second) server := NewServer(1 * time.Second)
server.Handle( server.Handle(
"/get-random-number", "/get-value",
addValueToContext, addValueToContext,
handleGetRandomNumber, handleGetValue,
) )
if err := server.Listen(PORT); err != nil { if err := server.Listen(PORT); err != nil {
@@ -25,21 +24,76 @@ func main() {
fmt.Println("listening on :", PORT) fmt.Println("listening on :", PORT)
} }
func handleGetRandomNumber(next http.Handler) http.Handler { func handleGetValue(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: ", r.Context().Value("number")) fmt.Println("starting handleGetValue")
w.Write([]byte(`{"error": "process timeout"}`)) defer fmt.Println("finished executing handleGetNumber")
message, err := executeHandleGetValue(r.Context(), r)
if err != nil {
w.Write([]byte(fmt.Sprintf("\nError happened: %s", err.Error())))
fmt.Println("Error happened: ", err)
return
}
w.Write([]byte(message))
}) })
} }
func executeHandleGetValue(ctx context.Context, r *http.Request) (string, error) {
chanErr := make(chan error, 1)
go func() {
fmt.Println("executeHandleGetValue started")
time.Sleep(2 * time.Second)
// simulate errors
shouldError := true
if shouldError {
chanErr <- fmt.Errorf("an error happened during goroutine")
} else {
chanErr <- nil
}
fmt.Println("executeHandleGetValue ended")
}()
select {
case <-ctx.Done():
<-chanErr
return "", ctx.Err()
case err := <-chanErr:
return "finished successfully", err
}
}
func addValueToContext(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) {
n := rand.Intn(50) // Add simple values
ctx := context.WithValue(r.Context(), "number", n) ctx1 := context.WithValue(r.Context(), "number", 420)
fmt.Println("add value to context: ", n) ctx2 := context.WithValue(ctx1, "sad_message", "RIP Toriyama :(")
time.Sleep(10 * time.Second) // Add a more complex one
cs := ComplexStruct{
question: "What is your favourite Dragon Ball character?",
possibleAnswers: []string{
"Goku",
"Gohan",
"Vegeta",
"You get the idea zz",
},
}
next.ServeHTTP(w, r.WithContext(ctx)) ctx3 := context.WithValue(ctx2, "complex_struct", cs)
next.ServeHTTP(w, r.WithContext(ctx3))
}) })
} }
type ComplexStruct struct {
question string
possibleAnswers []string
}
+9 -17
View File
@@ -3,7 +3,6 @@ package main
import ( import (
"context" "context"
"fmt" "fmt"
"log"
"net/http" "net/http"
"time" "time"
) )
@@ -21,7 +20,13 @@ func NewServer(timeout time.Duration) *Server {
} }
func (s *Server) Handle(addr string, handlers ...ServerHandler) { func (s *Server) Handle(addr string, handlers ...ServerHandler) {
middlewares := append([]ServerHandler{s.ctxMiddleware}, handlers...) var middlewares []ServerHandler
if s.timeout == 0 {
middlewares = handlers
} else {
middlewares = append([]ServerHandler{s.timeoutMiddleware}, handlers...)
}
http.Handle(addr, handleMiddlewares(middlewares)) http.Handle(addr, handleMiddlewares(middlewares))
} }
@@ -34,25 +39,12 @@ func (s *Server) Listen(port int) error {
return nil return nil
} }
func (s *Server) ctxMiddleware(next http.Handler) http.Handler { func (s *Server) timeoutMiddleware(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) {
ctx, cancel := context.WithTimeout(r.Context(), s.timeout) ctx, cancel := context.WithTimeout(r.Context(), s.timeout)
defer cancel() defer cancel()
processDone := make(chan bool) next.ServeHTTP(w, r.WithContext(ctx))
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")
}
}) })
} }