From 2118e0fb86d945a04d7a0d0c5f558310f8002f3d Mon Sep 17 00:00:00 2001 From: Minhyeok Park Date: Wed, 23 Oct 2024 19:49:33 +0900 Subject: [PATCH] fix: regist endpoints outside of goroutine --- main.go | 282 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 142 insertions(+), 140 deletions(-) diff --git a/main.go b/main.go index fe7c09c..9e4859e 100644 --- a/main.go +++ b/main.go @@ -103,36 +103,36 @@ func main() { DisableStartupMessage: true, }) - go func() { - app.Post("/api/streams/persist", func(c *fiber.Ctx) error { - data := struct { - GeneratedUserId string `json:"generated_user_id"` - }{} + app.Post("/api/streams/persist", func(c *fiber.Ctx) error { + data := struct { + GeneratedUserId string `json:"generated_user_id"` + }{} - data.GeneratedUserId = gofakeit.UUID() + data.GeneratedUserId = gofakeit.UUID() + + stmt, err := db.Prepare("INSERT INTO user (user_id, preferred_category, created_at) VALUES (?, ?, ?)") + if err != nil { + log.Println("DATABASE_PREPARE_FAILED") + } + + stmt.Exec(data.GeneratedUserId, "Test Category", time.Now()) + stmt.Close() + + for j := 0; j < 10; j++ { + stmt2, err := db.Prepare("INSERT INTO purchase (user_id, purchase_id, item_id, item_category, created_at) VALUES (?, ?, ?, ?, ?)") - stmt, err := db.Prepare("INSERT INTO user (user_id, preferred_category, created_at) VALUES (?, ?, ?)") if err != nil { log.Println("DATABASE_PREPARE_FAILED") } - stmt.Exec(data.GeneratedUserId, "Test Category", time.Now()) - stmt.Close() + stmt2.Exec(data.GeneratedUserId, gofakeit.UUID(), gofakeit.UUID(), "Test Category", time.Now()) + stmt2.Close() + } - for j := 0; j < 10; j++ { - stmt2, err := db.Prepare("INSERT INTO purchase (user_id, purchase_id, item_id, item_category, created_at) VALUES (?, ?, ?, ?, ?)") - - if err != nil { - log.Println("DATABASE_PREPARE_FAILED") - } - - stmt2.Exec(data.GeneratedUserId, gofakeit.UUID(), gofakeit.UUID(), "Test Category", time.Now()) - stmt2.Close() - } - - return c.JSON(data) - }) + return c.JSON(data) + }) + go func() { for { preferred_categories, _ := weightedrand.NewChooser( weightedrand.Choice{Item: "Development", Weight: gofakeit.UintN(10) + 1}, @@ -173,7 +173,7 @@ func main() { } }() - go func() { + func() { KAFKA_BROKERS, ok := os.LookupEnv("KAFKA_BROKERS") if !ok { return @@ -265,136 +265,138 @@ func main() { return c.JSON(data) }) - for { - is_anon := gofakeit.IntN(10) > 3 - preferred_categories, _ := weightedrand.NewChooser( - weightedrand.Choice{Item: "Development", Weight: gofakeit.UintN(10) + 1}, - weightedrand.Choice{Item: "Novel", Weight: gofakeit.UintN(10) + 1}, - weightedrand.Choice{Item: "Cookbook", Weight: gofakeit.UintN(10) + 1}, - weightedrand.Choice{Item: "Journal", Weight: gofakeit.UintN(10) + 1}, - weightedrand.Choice{Item: "Art", Weight: gofakeit.UintN(10) + 1}, - weightedrand.Choice{Item: "History", Weight: gofakeit.UintN(10) + 1}, - weightedrand.Choice{Item: "Science", Weight: gofakeit.UintN(10) + 1}, - ) + go func() { + for { + is_anon := gofakeit.IntN(10) > 3 + preferred_categories, _ := weightedrand.NewChooser( + weightedrand.Choice{Item: "Development", Weight: gofakeit.UintN(10) + 1}, + weightedrand.Choice{Item: "Novel", Weight: gofakeit.UintN(10) + 1}, + weightedrand.Choice{Item: "Cookbook", Weight: gofakeit.UintN(10) + 1}, + weightedrand.Choice{Item: "Journal", Weight: gofakeit.UintN(10) + 1}, + weightedrand.Choice{Item: "Art", Weight: gofakeit.UintN(10) + 1}, + weightedrand.Choice{Item: "History", Weight: gofakeit.UintN(10) + 1}, + weightedrand.Choice{Item: "Science", Weight: gofakeit.UintN(10) + 1}, + ) - for i := 1; i <= 100*60*5; i++ { - click_events := []string{} - click_event_count := gofakeit.IntRange(1, 5) - if is_anon { - for j := 0; j < click_event_count; j++ { - click_events = append(click_events, fmt.Sprintf("{\"type\":\"click\",\"details\":{\"element\":\".%s\",\"content\":{\"item_name\":\"%s\",\"item_category\":\"%s\",\"item_location\":\"%s\"}},\"credentials\":{\"session_id\":null,\"session_issuer\":null,\"sys_trace_id\":\"ubks__mainsvc__vNeptune_1_%s\"},\"event_at\":\"%s\"}", - gofakeit.RandomString([]string{"bookItem", "audioItem"}), gofakeit.Adjective()+" "+gofakeit.Noun(), preferred_categories.Pick().(string), gofakeit.Street(), gofakeit.UUID(), time.Now().Format("2006-01-02T15:04:05.999999-07:00"))) + for i := 1; i <= 100*60*5; i++ { + click_events := []string{} + click_event_count := gofakeit.IntRange(1, 5) + if is_anon { + for j := 0; j < click_event_count; j++ { + click_events = append(click_events, fmt.Sprintf("{\"type\":\"click\",\"details\":{\"element\":\".%s\",\"content\":{\"item_name\":\"%s\",\"item_category\":\"%s\",\"item_location\":\"%s\"}},\"credentials\":{\"session_id\":null,\"session_issuer\":null,\"sys_trace_id\":\"ubks__mainsvc__vNeptune_1_%s\"},\"event_at\":\"%s\"}", + gofakeit.RandomString([]string{"bookItem", "audioItem"}), gofakeit.Adjective()+" "+gofakeit.Noun(), preferred_categories.Pick().(string), gofakeit.Street(), gofakeit.UUID(), time.Now().Format("2006-01-02T15:04:05.999999-07:00"))) + } + + writer.WriteMessages(context.Background(), + kafka.Message{ + Value: []byte("[" + strings.Join(click_events, ",") + "]"), + }, + kafka.Message{ + Value: []byte(fmt.Sprintf("{\"type\":\"select\",\"details\":{\"element\":\"#%s\",\"value\":\"%s\"},\"credentials\":{\"session_id\":null,\"session_issuer\":null,\"sys_trace_id\":\"ubks__mainsvc__vNeptune_1_%s\"},\"event_at\":\"%s\"}", + gofakeit.RandomString([]string{"categorySelector", "searchFilterSelector"}), preferred_categories.Pick(), gofakeit.UUID(), time.Now().Format("2006-01-02T15:04:05.999999-07:00"))), + }, + kafka.Message{ + Value: []byte(fmt.Sprintf("{\"type\":\"move\",\"details\":{\"page\":\"/%s/%s\"},\"event_at\":\"%s\"}", + gofakeit.Adjective(), gofakeit.Noun(), time.Now().Format("2006-01-02T15:04:05.999999-07:00"))), + }, + kafka.Message{ + Value: []byte(fmt.Sprintf("{\"type\":\"error\",\"details\":{\"page\":\"/%s/%s\",\"message\":\"%s\"},\"event_at\":\"%s\"}", + gofakeit.Adjective(), gofakeit.Noun(), gofakeit.LoremIpsumSentence(100), time.Now().Format("2006-01-02T15:04:05.999999-07:00"))), + }, + ) + } else { + session_id := gofakeit.UUID() + for j := 0; j < click_event_count; j++ { + click_events = append(click_events, fmt.Sprintf("{\"type\":\"click\",\"details\":{\"element\":\".%s\",\"content\":{\"item_name\":\"%s\",\"item_category\":\"%s\",\"item_location\":\"%s\"}},\"credentials\":{\"session_id\":\"ubks_%s\",\"session_issuer\":\"mainsvc__vNeptune_1\",\"sys_trace_id\":\"ubks__mainsvc__vNeptune_1_%s\"},\"event_at\":\"%s\"}", + gofakeit.RandomString([]string{"bookItem", "audioItem"}), gofakeit.Adjective()+" "+gofakeit.Noun(), preferred_categories.Pick().(string), gofakeit.Street(), session_id, gofakeit.UUID(), time.Now().Format("2006-01-02T15:04:05.999999-07:00"))) + } + + writer.WriteMessages(context.Background(), + kafka.Message{ + Value: []byte(fmt.Sprintf("{\"type\":\"login\",\"details\":{\"user_id\":\"%s\",\"user_name\":\"%s\",\"user_age\":%d},\"credentials\":{\"session_id\":\"ubks_%s\",\"session_issuer\":\"mainsvc__vNeptune_1\",\"sys_trace_id\":\"ubks__mainsvc__vNeptune_1_%s\"},\"event_at\":\"%s\"}", + gofakeit.Username(), gofakeit.Name(), gofakeit.IntRange(16, 65), session_id, gofakeit.UUID(), time.Now().Format("2006-01-02T15:04:05.999999-07:00"))), + }, + kafka.Message{ + Value: []byte("[" + strings.Join(click_events, ",") + "]"), + }, + kafka.Message{ + Value: []byte(fmt.Sprintf("{\"type\":\"select\",\"details\":{\"element\":\"#%s\",\"value\":\"%s\"},\"credentials\":{\"session_id\":\"ubks_%s\",\"session_issuer\":\"mainsvc__vNeptune_1\",\"sys_trace_id\":\"ubks__mainsvc__vNeptune_1_%s\"},\"event_at\":\"%s\"}", + gofakeit.RandomString([]string{"categorySelector", "searchFilterSelector"}), preferred_categories.Pick(), session_id, gofakeit.UUID(), time.Now().Format("2006-01-02T15:04:05.999999-07:00"))), + }, + kafka.Message{ + Value: []byte(fmt.Sprintf("{\"type\":\"move\",\"details\":{\"page\":\"/%s/%s\"},\"event_at\":\"%s\"}", + gofakeit.Adjective(), gofakeit.Noun(), time.Now().Format("2006-01-02T15:04:05.999999-07:00"))), + }, + kafka.Message{ + Value: []byte(fmt.Sprintf("{\"type\":\"error\",\"details\":{\"page\":\"/%s/%s\",\"message\":\"%s\"},\"event_at\":\"%s\"}", + gofakeit.Adjective(), gofakeit.Noun(), gofakeit.LoremIpsumSentence(100), time.Now().Format("2006-01-02T15:04:05.999999-07:00"))), + }, + ) } - writer.WriteMessages(context.Background(), - kafka.Message{ - Value: []byte("[" + strings.Join(click_events, ",") + "]"), - }, - kafka.Message{ - Value: []byte(fmt.Sprintf("{\"type\":\"select\",\"details\":{\"element\":\"#%s\",\"value\":\"%s\"},\"credentials\":{\"session_id\":null,\"session_issuer\":null,\"sys_trace_id\":\"ubks__mainsvc__vNeptune_1_%s\"},\"event_at\":\"%s\"}", - gofakeit.RandomString([]string{"categorySelector", "searchFilterSelector"}), preferred_categories.Pick(), gofakeit.UUID(), time.Now().Format("2006-01-02T15:04:05.999999-07:00"))), - }, - kafka.Message{ - Value: []byte(fmt.Sprintf("{\"type\":\"move\",\"details\":{\"page\":\"/%s/%s\"},\"event_at\":\"%s\"}", - gofakeit.Adjective(), gofakeit.Noun(), time.Now().Format("2006-01-02T15:04:05.999999-07:00"))), - }, - kafka.Message{ - Value: []byte(fmt.Sprintf("{\"type\":\"error\",\"details\":{\"page\":\"/%s/%s\",\"message\":\"%s\"},\"event_at\":\"%s\"}", - gofakeit.Adjective(), gofakeit.Noun(), gofakeit.LoremIpsumSentence(100), time.Now().Format("2006-01-02T15:04:05.999999-07:00"))), - }, - ) - } else { - session_id := gofakeit.UUID() - for j := 0; j < click_event_count; j++ { - click_events = append(click_events, fmt.Sprintf("{\"type\":\"click\",\"details\":{\"element\":\".%s\",\"content\":{\"item_name\":\"%s\",\"item_category\":\"%s\",\"item_location\":\"%s\"}},\"credentials\":{\"session_id\":\"ubks_%s\",\"session_issuer\":\"mainsvc__vNeptune_1\",\"sys_trace_id\":\"ubks__mainsvc__vNeptune_1_%s\"},\"event_at\":\"%s\"}", - gofakeit.RandomString([]string{"bookItem", "audioItem"}), gofakeit.Adjective()+" "+gofakeit.Noun(), preferred_categories.Pick().(string), gofakeit.Street(), session_id, gofakeit.UUID(), time.Now().Format("2006-01-02T15:04:05.999999-07:00"))) - } - - writer.WriteMessages(context.Background(), - kafka.Message{ - Value: []byte(fmt.Sprintf("{\"type\":\"login\",\"details\":{\"user_id\":\"%s\",\"user_name\":\"%s\",\"user_age\":%d},\"credentials\":{\"session_id\":\"ubks_%s\",\"session_issuer\":\"mainsvc__vNeptune_1\",\"sys_trace_id\":\"ubks__mainsvc__vNeptune_1_%s\"},\"event_at\":\"%s\"}", - gofakeit.Username(), gofakeit.Name(), gofakeit.IntRange(16, 65), session_id, gofakeit.UUID(), time.Now().Format("2006-01-02T15:04:05.999999-07:00"))), - }, - kafka.Message{ - Value: []byte("[" + strings.Join(click_events, ",") + "]"), - }, - kafka.Message{ - Value: []byte(fmt.Sprintf("{\"type\":\"select\",\"details\":{\"element\":\"#%s\",\"value\":\"%s\"},\"credentials\":{\"session_id\":\"ubks_%s\",\"session_issuer\":\"mainsvc__vNeptune_1\",\"sys_trace_id\":\"ubks__mainsvc__vNeptune_1_%s\"},\"event_at\":\"%s\"}", - gofakeit.RandomString([]string{"categorySelector", "searchFilterSelector"}), preferred_categories.Pick(), session_id, gofakeit.UUID(), time.Now().Format("2006-01-02T15:04:05.999999-07:00"))), - }, - kafka.Message{ - Value: []byte(fmt.Sprintf("{\"type\":\"move\",\"details\":{\"page\":\"/%s/%s\"},\"event_at\":\"%s\"}", - gofakeit.Adjective(), gofakeit.Noun(), time.Now().Format("2006-01-02T15:04:05.999999-07:00"))), - }, - kafka.Message{ - Value: []byte(fmt.Sprintf("{\"type\":\"error\",\"details\":{\"page\":\"/%s/%s\",\"message\":\"%s\"},\"event_at\":\"%s\"}", - gofakeit.Adjective(), gofakeit.Noun(), gofakeit.LoremIpsumSentence(100), time.Now().Format("2006-01-02T15:04:05.999999-07:00"))), - }, - ) + time.Sleep(10 * time.Millisecond) } - - time.Sleep(10 * time.Millisecond) } - } + }() }() - go func() { - app.Post("/api/streams/log", func(c *fiber.Ctx) error { - data := struct { - GeneratedReferrer string `json:"generated_referrer"` - GeneratedCount int `json:"generated_count"` - SampledLog string `json:"sampled_log"` - }{} + app.Post("/api/streams/log", func(c *fiber.Ctx) error { + data := struct { + GeneratedReferrer string `json:"generated_referrer"` + GeneratedCount int `json:"generated_count"` + SampledLog string `json:"sampled_log"` + }{} - data.GeneratedCount = 100000 - data.GeneratedReferrer = gofakeit.Noun() + ".test.com" + data.GeneratedCount = 100000 + data.GeneratedReferrer = gofakeit.Noun() + ".test.com" - for i := 0; i < data.GeneratedCount; i++ { - method := gofakeit.HTTPMethod() - referrer := gofakeit.RandomString([]string{"http://", "https://"}) + data.GeneratedReferrer + ":" + gofakeit.RandomString([]string{"80", "443", "8080", "8443", "8000", "5000"}) + "/" + url.PathEscape(gofakeit.Adjective()) + "/" + url.PathEscape(gofakeit.Noun()) + for i := 0; i < data.GeneratedCount; i++ { + method := gofakeit.HTTPMethod() + referrer := gofakeit.RandomString([]string{"http://", "https://"}) + data.GeneratedReferrer + ":" + gofakeit.RandomString([]string{"80", "443", "8080", "8443", "8000", "5000"}) + "/" + url.PathEscape(gofakeit.Adjective()) + "/" + url.PathEscape(gofakeit.Noun()) - if method == "GET" || method == "HEAD" || method == "DELETE" { - fmt.Printf("%s - [%s] %s %s %d %s %s %s \"%s\"\n", - gofakeit.IPv4Address(), - time.Now().Format("2006-01-02T15:04:05.999999 -07:00"), - method, - "/api/qry/"+gofakeit.Noun()+"?_qtt="+gofakeit.UUID()+"&_st="+url.QueryEscape(gofakeit.StreetName()), - gofakeit.HTTPStatusCode(), - "100MB", - "100s", - referrer, - gofakeit.UserAgent()) - } else { - fmt.Printf("%s - [%s] %s %s %d %s %s %s %s \"%s\"\n", - gofakeit.IPv4Address(), - time.Now().Format("2006-01-02T15:04:05.999999 -07:00"), - method, - "/api/products?_qcw="+gofakeit.UUID()+"&_rf="+gofakeit.UUID(), - gofakeit.HTTPStatusCode(), - "100MB", - "100MB", - "100s", - referrer, - gofakeit.UserAgent()) - } + if method == "GET" || method == "HEAD" || method == "DELETE" { + fmt.Printf("%s - [%s] %s %s %d %s %s %s \"%s\"\n", + gofakeit.IPv4Address(), + time.Now().Format("2006-01-02T15:04:05.999999 -07:00"), + method, + "/api/qry/"+url.PathEscape(gofakeit.Noun())+"?_qtt="+gofakeit.UUID()+"&_st="+url.QueryEscape(gofakeit.StreetName()), + gofakeit.HTTPStatusCode(), + "100MB", + "100s", + referrer, + gofakeit.UserAgent()) + } else { + fmt.Printf("%s - [%s] %s %s %d %s %s %s %s \"%s\"\n", + gofakeit.IPv4Address(), + time.Now().Format("2006-01-02T15:04:05.999999 -07:00"), + method, + "/api/products?_qcw="+gofakeit.UUID()+"&_rf="+gofakeit.UUID(), + gofakeit.HTTPStatusCode(), + "100MB", + "100MB", + "100s", + referrer, + gofakeit.UserAgent()) } + } - data.SampledLog = fmt.Sprintf("%s - [%s] %s %s %d %s %s %s %s \"%s\"\n", - gofakeit.IPv4Address(), - time.Now().Format("2006-01-02T15:04:05.999999 -07:00"), - "POST", - "/test", - 201, - "1MB", - "1B", - "1s", - "referrer", - "test/"+gofakeit.UUID()) + data.SampledLog = fmt.Sprintf("%s - [%s] %s %s %d %s %s %s %s \"%s\"", + gofakeit.IPv4Address(), + time.Now().Format("2006-01-02T15:04:05.999999 -07:00"), + "POST", + "/test", + 201, + "1MB", + "1B", + "1s", + "http://referrer", + "test/"+gofakeit.UUID()) - fmt.Print(data.SampledLog) - return c.JSON(data) - }) + fmt.Println(data.SampledLog) + return c.JSON(data) + }) + go func() { for { rxSeed := gofakeit.IntN(50) + 1 txSeed := gofakeit.IntN(100) + 1 @@ -417,7 +419,7 @@ func main() { gofakeit.IPv4Address(), time.Now().Format("2006-01-02T15:04:05.999999 -07:00"), method, - "/api/qry/"+gofakeit.Noun()+"?_qtt="+gofakeit.UUID()+"&_st="+url.QueryEscape(gofakeit.StreetName()), + "/api/qry/"+url.PathEscape(gofakeit.Noun())+"?_qtt="+gofakeit.UUID()+"&_st="+url.QueryEscape(gofakeit.StreetName()), gofakeit.HTTPStatusCode(), ByteCountSI(gofakeit.IntN(1000*10*txSeed)), duration,