fix: regist endpoints outside of goroutine

This commit is contained in:
Minhyeok Park 2024-10-23 19:49:33 +09:00
parent 96368b78f6
commit 2118e0fb86
Signed by: pmh_only
SSH Key Fingerprint: SHA256:g/OyGvi2pcd8ub9mqge/ohmDP0fZX/xOPWPIcM+9XpI

282
main.go
View File

@ -103,36 +103,36 @@ func main() {
DisableStartupMessage: true, DisableStartupMessage: true,
}) })
go func() { app.Post("/api/streams/persist", func(c *fiber.Ctx) error {
app.Post("/api/streams/persist", func(c *fiber.Ctx) error { data := struct {
data := struct { GeneratedUserId string `json:"generated_user_id"`
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 { if err != nil {
log.Println("DATABASE_PREPARE_FAILED") log.Println("DATABASE_PREPARE_FAILED")
} }
stmt.Exec(data.GeneratedUserId, "Test Category", time.Now()) stmt2.Exec(data.GeneratedUserId, gofakeit.UUID(), gofakeit.UUID(), "Test Category", time.Now())
stmt.Close() stmt2.Close()
}
for j := 0; j < 10; j++ { return c.JSON(data)
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)
})
go func() {
for { for {
preferred_categories, _ := weightedrand.NewChooser( preferred_categories, _ := weightedrand.NewChooser(
weightedrand.Choice{Item: "Development", Weight: gofakeit.UintN(10) + 1}, 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") KAFKA_BROKERS, ok := os.LookupEnv("KAFKA_BROKERS")
if !ok { if !ok {
return return
@ -265,136 +265,138 @@ func main() {
return c.JSON(data) return c.JSON(data)
}) })
for { go func() {
is_anon := gofakeit.IntN(10) > 3 for {
preferred_categories, _ := weightedrand.NewChooser( is_anon := gofakeit.IntN(10) > 3
weightedrand.Choice{Item: "Development", Weight: gofakeit.UintN(10) + 1}, preferred_categories, _ := weightedrand.NewChooser(
weightedrand.Choice{Item: "Novel", Weight: gofakeit.UintN(10) + 1}, weightedrand.Choice{Item: "Development", Weight: gofakeit.UintN(10) + 1},
weightedrand.Choice{Item: "Cookbook", Weight: gofakeit.UintN(10) + 1}, weightedrand.Choice{Item: "Novel", Weight: gofakeit.UintN(10) + 1},
weightedrand.Choice{Item: "Journal", Weight: gofakeit.UintN(10) + 1}, weightedrand.Choice{Item: "Cookbook", Weight: gofakeit.UintN(10) + 1},
weightedrand.Choice{Item: "Art", Weight: gofakeit.UintN(10) + 1}, weightedrand.Choice{Item: "Journal", Weight: gofakeit.UintN(10) + 1},
weightedrand.Choice{Item: "History", Weight: gofakeit.UintN(10) + 1}, weightedrand.Choice{Item: "Art", Weight: gofakeit.UintN(10) + 1},
weightedrand.Choice{Item: "Science", 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++ { for i := 1; i <= 100*60*5; i++ {
click_events := []string{} click_events := []string{}
click_event_count := gofakeit.IntRange(1, 5) click_event_count := gofakeit.IntRange(1, 5)
if is_anon { if is_anon {
for j := 0; j < click_event_count; j++ { 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\"}", 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"))) 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(), time.Sleep(10 * time.Millisecond)
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)
} }
} }()
}() }()
go func() { app.Post("/api/streams/log", func(c *fiber.Ctx) error {
app.Post("/api/streams/log", func(c *fiber.Ctx) error { data := struct {
data := struct { GeneratedReferrer string `json:"generated_referrer"`
GeneratedReferrer string `json:"generated_referrer"` GeneratedCount int `json:"generated_count"`
GeneratedCount int `json:"generated_count"` SampledLog string `json:"sampled_log"`
SampledLog string `json:"sampled_log"` }{}
}{}
data.GeneratedCount = 100000 data.GeneratedCount = 100000
data.GeneratedReferrer = gofakeit.Noun() + ".test.com" data.GeneratedReferrer = gofakeit.Noun() + ".test.com"
for i := 0; i < data.GeneratedCount; i++ { for i := 0; i < data.GeneratedCount; i++ {
method := gofakeit.HTTPMethod() 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()) 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" { if method == "GET" || method == "HEAD" || method == "DELETE" {
fmt.Printf("%s - [%s] %s %s %d %s %s %s \"%s\"\n", fmt.Printf("%s - [%s] %s %s %d %s %s %s \"%s\"\n",
gofakeit.IPv4Address(), gofakeit.IPv4Address(),
time.Now().Format("2006-01-02T15:04:05.999999 -07:00"), time.Now().Format("2006-01-02T15:04:05.999999 -07:00"),
method, 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(), gofakeit.HTTPStatusCode(),
"100MB", "100MB",
"100s", "100s",
referrer, referrer,
gofakeit.UserAgent()) gofakeit.UserAgent())
} else { } else {
fmt.Printf("%s - [%s] %s %s %d %s %s %s %s \"%s\"\n", fmt.Printf("%s - [%s] %s %s %d %s %s %s %s \"%s\"\n",
gofakeit.IPv4Address(), gofakeit.IPv4Address(),
time.Now().Format("2006-01-02T15:04:05.999999 -07:00"), time.Now().Format("2006-01-02T15:04:05.999999 -07:00"),
method, method,
"/api/products?_qcw="+gofakeit.UUID()+"&_rf="+gofakeit.UUID(), "/api/products?_qcw="+gofakeit.UUID()+"&_rf="+gofakeit.UUID(),
gofakeit.HTTPStatusCode(), gofakeit.HTTPStatusCode(),
"100MB", "100MB",
"100MB", "100MB",
"100s", "100s",
referrer, referrer,
gofakeit.UserAgent()) gofakeit.UserAgent())
}
} }
}
data.SampledLog = fmt.Sprintf("%s - [%s] %s %s %d %s %s %s %s \"%s\"\n", data.SampledLog = fmt.Sprintf("%s - [%s] %s %s %d %s %s %s %s \"%s\"",
gofakeit.IPv4Address(), gofakeit.IPv4Address(),
time.Now().Format("2006-01-02T15:04:05.999999 -07:00"), time.Now().Format("2006-01-02T15:04:05.999999 -07:00"),
"POST", "POST",
"/test", "/test",
201, 201,
"1MB", "1MB",
"1B", "1B",
"1s", "1s",
"referrer", "http://referrer",
"test/"+gofakeit.UUID()) "test/"+gofakeit.UUID())
fmt.Print(data.SampledLog) fmt.Println(data.SampledLog)
return c.JSON(data) return c.JSON(data)
}) })
go func() {
for { for {
rxSeed := gofakeit.IntN(50) + 1 rxSeed := gofakeit.IntN(50) + 1
txSeed := gofakeit.IntN(100) + 1 txSeed := gofakeit.IntN(100) + 1
@ -417,7 +419,7 @@ func main() {
gofakeit.IPv4Address(), gofakeit.IPv4Address(),
time.Now().Format("2006-01-02T15:04:05.999999 -07:00"), time.Now().Format("2006-01-02T15:04:05.999999 -07:00"),
method, 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(), gofakeit.HTTPStatusCode(),
ByteCountSI(gofakeit.IntN(1000*10*txSeed)), ByteCountSI(gofakeit.IntN(1000*10*txSeed)),
duration, duration,