From 3feb4fec4b7b631793168e55ef40013e854bcce9 Mon Sep 17 00:00:00 2001 From: Soulan Saeng Date: Mon, 29 Apr 2024 13:22:24 -0400 Subject: [PATCH 01/11] adding sleep --- main.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/main.go b/main.go index fc3cb3d..0541175 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "os" "strconv" "strings" + "time" "github.com/gofrs/uuid" ) @@ -125,6 +126,7 @@ func main() { fmt.Println(line) } fmt.Println() + time.Sleep(time.Minute * 3) fmt.Printf("==> Server listening at %s 🚀\n", bindAddr) err := http.ListenAndServe(fmt.Sprintf(":%s", port), nil) From fa3fdd5f77c8d842b8d6b6757575f6267bfae251 Mon Sep 17 00:00:00 2001 From: Soulan Saeng Date: Mon, 24 Feb 2025 10:14:22 -0500 Subject: [PATCH 02/11] adding sleep before startup. --- main.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 0541175..3fdfd22 100644 --- a/main.go +++ b/main.go @@ -126,7 +126,8 @@ func main() { fmt.Println(line) } fmt.Println() - time.Sleep(time.Minute * 3) + fmt.Printf("==> Sleeping for 5 minutes to fail healthcheck %s \n", bindAddr) + time.Sleep(time.Minute * 5) fmt.Printf("==> Server listening at %s 🚀\n", bindAddr) err := http.ListenAndServe(fmt.Sprintf(":%s", port), nil) From d9901d9d65af59baa934b992359b55eeaa218df4 Mon Sep 17 00:00:00 2001 From: Soulan Saeng Date: Tue, 25 Feb 2025 14:52:32 -0500 Subject: [PATCH 03/11] remove sleep --- main.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/main.go b/main.go index 3fdfd22..bc29e7c 100644 --- a/main.go +++ b/main.go @@ -2,13 +2,11 @@ package main import ( "fmt" + "github.com/gofrs/uuid" "net/http" "os" "strconv" "strings" - "time" - - "github.com/gofrs/uuid" ) const startupMessage = `                                                                                 @@ -126,8 +124,6 @@ func main() { fmt.Println(line) } fmt.Println() - fmt.Printf("==> Sleeping for 5 minutes to fail healthcheck %s \n", bindAddr) - time.Sleep(time.Minute * 5) fmt.Printf("==> Server listening at %s 🚀\n", bindAddr) err := http.ListenAndServe(fmt.Sprintf(":%s", port), nil) From 55e08c04849df4683f3244152611480609e66818 Mon Sep 17 00:00:00 2001 From: Soulan Saeng Date: Wed, 26 Feb 2025 16:22:57 -0500 Subject: [PATCH 04/11] adding more logs --- main.go | 1 + 1 file changed, 1 insertion(+) diff --git a/main.go b/main.go index bc29e7c..c2fd955 100644 --- a/main.go +++ b/main.go @@ -34,6 +34,7 @@ func logRequest(r *http.Request) { func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + fmt.Println("processing request....") logRequest(r) fmt.Fprintf(w, "Hello! you've requested %s\n", r.URL.Path) }) From ad7e22a2faea553936695b3e4cc5046e562f10a3 Mon Sep 17 00:00:00 2001 From: Soulan Saeng Date: Mon, 3 Mar 2025 14:49:41 -0500 Subject: [PATCH 05/11] break it again --- main.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/main.go b/main.go index c2fd955..5cacd3e 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "os" "strconv" "strings" + "time" ) const startupMessage = `                                                                                 @@ -124,6 +125,8 @@ func main() { for _, line := range lines { fmt.Println(line) } + fmt.Printf("==> sleeping server for 10 minutes \n") + time.Sleep(time.Second * 600) fmt.Println() fmt.Printf("==> Server listening at %s 🚀\n", bindAddr) From 009d74451983e44975a65e46f3a1fc721159c668 Mon Sep 17 00:00:00 2001 From: Soulan Saeng Date: Tue, 18 Mar 2025 13:00:05 -0400 Subject: [PATCH 06/11] try json logging. --- main.go | 50 +++++++++++++------------------------------------- 1 file changed, 13 insertions(+), 37 deletions(-) diff --git a/main.go b/main.go index 5cacd3e..6f4d9db 100644 --- a/main.go +++ b/main.go @@ -2,46 +2,30 @@ package main import ( "fmt" - "github.com/gofrs/uuid" + "log/slog" "net/http" "os" "strconv" "strings" - "time" -) -const startupMessage = `                                                                                 -                                                   �                             -                                                                                 -                                                                                 -                                               ��������                          -                                              �������          �                 -  ��        �� ��   ���  ��                  �������          � ����             -  �          ����       ����          ����          �         �  ���             -  �      �    ��    ���               ����                     �����             -                     �         �����������            �           ���������      -                                            �����   ����        ����������       -                                              �      �������                     -             � �  �                                 �����������       �          -                  �  �        ��  ��  �         � ����������������   ��          -                           ��� �     �� �           �����������������            -` + "github.com/gofrs/uuid" +) -func logRequest(r *http.Request) { +func logRequest(r *http.Request, log *slog.Logger) { uri := r.RequestURI method := r.Method - fmt.Println("Got request!", method, uri) + log.Info("Got request!", "method", method, "uri", uri) } func main() { + + log := slog.New(slog.NewJSONHandler(os.Stdout, nil)) http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - fmt.Println("processing request....") - logRequest(r) - fmt.Fprintf(w, "Hello! you've requested %s\n", r.URL.Path) + logRequest(r, log) }) http.HandleFunc("/cached", func(w http.ResponseWriter, r *http.Request) { - logRequest(r) + logRequest(r, log) maxAgeParams, ok := r.URL.Query()["max-age"] if ok && len(maxAgeParams) > 0 { maxAge, _ := strconv.Atoi(maxAgeParams[0]) @@ -64,7 +48,7 @@ func main() { }) http.HandleFunc("/headers", func(w http.ResponseWriter, r *http.Request) { - logRequest(r) + logRequest(r, log) keys, ok := r.URL.Query()["key"] if ok && len(keys) > 0 { fmt.Fprint(w, r.Header.Get(keys[0])) @@ -79,7 +63,7 @@ func main() { }) http.HandleFunc("/env", func(w http.ResponseWriter, r *http.Request) { - logRequest(r) + logRequest(r, log) keys, ok := r.URL.Query()["key"] if ok && len(keys) > 0 { fmt.Fprint(w, os.Getenv(keys[0])) @@ -91,7 +75,7 @@ func main() { }) http.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) { - logRequest(r) + logRequest(r, log) codeParams, ok := r.URL.Query()["code"] if ok && len(codeParams) > 0 { statusCode, _ := strconv.Atoi(codeParams[0]) @@ -120,15 +104,7 @@ func main() { } bindAddr := fmt.Sprintf(":%s", port) - lines := strings.Split(startupMessage, "\n") - fmt.Println() - for _, line := range lines { - fmt.Println(line) - } - fmt.Printf("==> sleeping server for 10 minutes \n") - time.Sleep(time.Second * 600) - fmt.Println() - fmt.Printf("==> Server listening at %s 🚀\n", bindAddr) + log.Info("server started!! 🚀", "address", bindAddr) err := http.ListenAndServe(fmt.Sprintf(":%s", port), nil) if err != nil { From 103dc97deb44bc81c083d6f363cd6231e650df42 Mon Sep 17 00:00:00 2001 From: Soulan Saeng Date: Tue, 18 Mar 2025 13:12:17 -0400 Subject: [PATCH 07/11] go mod tidy --- go.mod | 4 ++-- go.sum | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index 0a5ec84..ef6003c 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,5 @@ module github.com/digitalocean/sample-golang -go 1.13 +go 1.22.1 -require github.com/gofrs/uuid v3.3.0+incompatible // indirect +require github.com/gofrs/uuid v4.4.0+incompatible diff --git a/go.sum b/go.sum index 4b1328f..c0ad687 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,2 @@ -github.com/gofrs/uuid v1.2.0 h1:coDhrjgyJaglxSjxuJdqQSSdUpG3w6p1OwN2od6frBU= -github.com/gofrs/uuid v3.3.0+incompatible h1:8K4tyRfvU1CYPgJsveYFQMhpFd/wXNM7iK6rR7UHz84= -github.com/gofrs/uuid v3.3.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= +github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1YrTJupqA= +github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= From 5416f5549171fbd0a809898702e4eeb8342d3863 Mon Sep 17 00:00:00 2001 From: Soulan Saeng Date: Wed, 19 Mar 2025 09:18:10 -0400 Subject: [PATCH 08/11] make it oom --- main.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 6f4d9db..e45a22b 100644 --- a/main.go +++ b/main.go @@ -14,13 +14,14 @@ import ( func logRequest(r *http.Request, log *slog.Logger) { uri := r.RequestURI method := r.Method - log.Info("Got request!", "method", method, "uri", uri) + log.Info("Received Request!", "method", method, "uri", uri) } func main() { log := slog.New(slog.NewJSONHandler(os.Stdout, nil)) http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "received request at path %s with method %s", r.URL.Path, r.Method) logRequest(r, log) }) @@ -106,6 +107,13 @@ func main() { bindAddr := fmt.Sprintf(":%s", port) log.Info("server started!! 🚀", "address", bindAddr) + var mem []byte + go func() { + for { + mem = append(mem, 1024*1024) + } + }() + err := http.ListenAndServe(fmt.Sprintf(":%s", port), nil) if err != nil { panic(err) From 4c797eff55abb3dc6dc53542070c5e9d7467074c Mon Sep 17 00:00:00 2001 From: Soulan Saeng Date: Wed, 19 Mar 2025 10:15:53 -0400 Subject: [PATCH 09/11] fix oom code --- main.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index e45a22b..43c49a1 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,10 @@ import ( "github.com/gofrs/uuid" ) +const ( + megabyte = 1024 * 1024 +) + func logRequest(r *http.Request, log *slog.Logger) { uri := r.RequestURI method := r.Method @@ -107,10 +111,12 @@ func main() { bindAddr := fmt.Sprintf(":%s", port) log.Info("server started!! 🚀", "address", bindAddr) - var mem []byte go func() { + // Allocate memory until the limit is reached + memoryDestination := make([]byte, 0, megabyte) for { - mem = append(mem, 1024*1024) + memorySrc := make([]byte, megabyte) // Allocate 1 MB + memoryDestination = append(memoryDestination, memorySrc...) } }() From 5f83872eed7947f1652141539176903219a404e4 Mon Sep 17 00:00:00 2001 From: Soulan Saeng Date: Wed, 19 Mar 2025 13:28:02 -0400 Subject: [PATCH 10/11] remove ooming code. --- main.go | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/main.go b/main.go index 43c49a1..7d002e3 100644 --- a/main.go +++ b/main.go @@ -11,10 +11,6 @@ import ( "github.com/gofrs/uuid" ) -const ( - megabyte = 1024 * 1024 -) - func logRequest(r *http.Request, log *slog.Logger) { uri := r.RequestURI method := r.Method @@ -110,16 +106,6 @@ func main() { bindAddr := fmt.Sprintf(":%s", port) log.Info("server started!! 🚀", "address", bindAddr) - - go func() { - // Allocate memory until the limit is reached - memoryDestination := make([]byte, 0, megabyte) - for { - memorySrc := make([]byte, megabyte) // Allocate 1 MB - memoryDestination = append(memoryDestination, memorySrc...) - } - }() - err := http.ListenAndServe(fmt.Sprintf(":%s", port), nil) if err != nil { panic(err) From 1ed7130c9744d2edc341817137185487fd6a83fb Mon Sep 17 00:00:00 2001 From: Soulan Saeng Date: Wed, 19 Mar 2025 16:29:30 -0400 Subject: [PATCH 11/11] add sleep again --- main.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 7d002e3..3a85353 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "os" "strconv" "strings" + "time" "github.com/gofrs/uuid" ) @@ -55,7 +56,7 @@ func main() { fmt.Fprint(w, r.Header.Get(keys[0])) return } - headers := []string{} + var headers []string headers = append(headers, fmt.Sprintf("host=%s", r.Host)) for key, values := range r.Header { headers = append(headers, fmt.Sprintf("%s=%s", key, strings.Join(values, ","))) @@ -70,7 +71,7 @@ func main() { fmt.Fprint(w, os.Getenv(keys[0])) return } - envs := []string{} + var envs []string envs = append(envs, os.Environ()...) fmt.Fprint(w, strings.Join(envs, "\n")) }) @@ -104,8 +105,7 @@ func main() { }) } - bindAddr := fmt.Sprintf(":%s", port) - log.Info("server started!! 🚀", "address", bindAddr) + time.Sleep(60 * time.Second) err := http.ListenAndServe(fmt.Sprintf(":%s", port), nil) if err != nil { panic(err)