From 589d2ae3d81b9c46645c4519a7c37529712b9691 Mon Sep 17 00:00:00 2001 From: lbenedar Date: Mon, 23 Feb 2026 16:56:00 +0300 Subject: [PATCH] add ch14 --- ch14/1/ch14_1.go | 24 +++++++++++++++++ ch14/1/go.mod | 3 +++ ch14/2/ch14_2.go | 36 ++++++++++++++++++++++++++ ch14/2/go.mod | 3 +++ ch14/3/ch14_3.go | 67 ++++++++++++++++++++++++++++++++++++++++++++++++ ch14/3/go.mod | 3 +++ 6 files changed, 136 insertions(+) create mode 100644 ch14/1/ch14_1.go create mode 100644 ch14/1/go.mod create mode 100644 ch14/2/ch14_2.go create mode 100644 ch14/2/go.mod create mode 100644 ch14/3/ch14_3.go create mode 100644 ch14/3/go.mod diff --git a/ch14/1/ch14_1.go b/ch14/1/ch14_1.go new file mode 100644 index 0000000..2488a28 --- /dev/null +++ b/ch14/1/ch14_1.go @@ -0,0 +1,24 @@ +package main + +import ( + "context" + "net/http" + "time" +) + +type MsReq struct{} + +func CreateTimeoutCtx(msReq time.Duration) func(http.Handler) http.Handler { + return func(h http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + reqCtx := r.Context() + reqCtx = context.WithValue(reqCtx, MsReq{}, msReq) + r.WithContext(reqCtx) + h.ServeHTTP(w, r) + }) + } +} + +func main() { + +} diff --git a/ch14/1/go.mod b/ch14/1/go.mod new file mode 100644 index 0000000..9352bb7 --- /dev/null +++ b/ch14/1/go.mod @@ -0,0 +1,3 @@ +module gitea.local.lab/Lbenedar/learning_go/ch14/1 + +go 1.25.0 diff --git a/ch14/2/ch14_2.go b/ch14/2/ch14_2.go new file mode 100644 index 0000000..3e2b413 --- /dev/null +++ b/ch14/2/ch14_2.go @@ -0,0 +1,36 @@ +package main + +import ( + "context" + "fmt" + "math/rand" + "time" +) + +type RandSum struct{} + +func main() { + sum := 0 + ctx, cancel := context.WithTimeout(context.Background(), time.Duration(2)*time.Second) + defer cancel() + start := time.Now() + i := 0 +outLoop: + for { + select { + case <-ctx.Done(): + ctx = context.WithValue(ctx, RandSum{}, "timeout") + break outLoop + default: + i++ + randNum := rand.Int() % 100_000_000 + //fmt.Printf("Randnum is %d\n", randNum) + sum = sum + randNum + if randNum == 1234 { + ctx = context.WithValue(ctx, RandSum{}, "number reached") + break outLoop + } + } + } + fmt.Printf("Sum is %d after %v; iterations - %d; cause - %s\n", sum, time.Since(start), i, ctx.Value(RandSum{})) +} diff --git a/ch14/2/go.mod b/ch14/2/go.mod new file mode 100644 index 0000000..40b57c6 --- /dev/null +++ b/ch14/2/go.mod @@ -0,0 +1,3 @@ +module gitea.local.lab/Lbenedar/learning_go/ch14/2 + +go 1.25.0 diff --git a/ch14/3/ch14_3.go b/ch14/3/ch14_3.go new file mode 100644 index 0000000..0bab248 --- /dev/null +++ b/ch14/3/ch14_3.go @@ -0,0 +1,67 @@ +package main + +import ( + "context" + "fmt" + "log/slog" + "net/http" + "time" +) + +type TimeHandler struct{} + +func (th TimeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + w.Write([]byte(time.Now().Format(time.RFC3339))) +} + +type Level string + +const ( + Debug Level = "debug" + Info Level = "info" +) + +func ExtractLogLevel(ctx context.Context) Level { + return ctx.Value("log_level").(Level) +} + +func StoreLogLevel(ctx context.Context, level Level) context.Context { + return context.WithValue(ctx, "log_level", level) +} + +func GetLogLevelQuery(http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + logLevel := r.URL.Query().Get("log_level") + if logLevel != "info" && logLevel != "debug" { + slog.Info("Wrong logLevel") + } + Log(StoreLogLevel(r.Context(), Level(logLevel)), Debug, "Test message") + }) +} + +func Log(ctx context.Context, level Level, message string) { + var inLevel Level + inLevel = ExtractLogLevel(ctx) + if level == Debug && inLevel == Debug { + fmt.Println(message) + } + if level == Info && (inLevel == Debug || inLevel == Info) { + fmt.Println(message) + } +} + +func main() { + server := http.Server{ + Addr: ":10100", + ReadTimeout: 30 * time.Second, + WriteTimeout: 90 * time.Second, + IdleTimeout: 120 * time.Second, + Handler: GetLogLevelQuery(TimeHandler{}), + } + err := server.ListenAndServe() + if err != nil { + if err != http.ErrServerClosed { + panic(err) + } + } +} diff --git a/ch14/3/go.mod b/ch14/3/go.mod new file mode 100644 index 0000000..756dcac --- /dev/null +++ b/ch14/3/go.mod @@ -0,0 +1,3 @@ +module gitea.local.lab/Lbenedar/learning_go/ch14/3 + +go 1.25.0