Skip to content

Commit

Permalink
postIsuConditionをバックグラウンドバルク化
Browse files Browse the repository at this point in the history
  • Loading branch information
tohutohu committed Oct 21, 2023
1 parent 214ef63 commit 168a4dd
Showing 1 changed file with 40 additions and 19 deletions.
59 changes: 40 additions & 19 deletions go/main.go
Expand Up @@ -255,6 +255,8 @@ func main() {
return
}

go postIsuConditionWorker()

serverPort := fmt.Sprintf(":%v", getEnv("SERVER_APP_PORT", "3000"))
e.Logger.Fatal(e.Start(serverPort))
}
Expand Down Expand Up @@ -1154,6 +1156,18 @@ func getTrend(c echo.Context) error {
return c.JSON(http.StatusOK, res)
}

type InsertRow struct {
JIAIsuUUID string `db:"jia_isu_uuid"`
Timestamp time.Time `db:"timestamp"`
IsSitting bool `db:"is_sitting"`
Condition string `db:"condition"`
Message string `db:"message"`
}

var (
insertRowCh = make(chan InsertRow, 10000)
)

// POST /api/condition/:jia_isu_uuid
// ISUからのコンディションを受け取る
func postIsuCondition(c echo.Context) error {
Expand Down Expand Up @@ -1194,37 +1208,20 @@ func postIsuCondition(c echo.Context) error {
return c.String(http.StatusNotFound, "not found: isu")
}

type InsertRow struct {
JIAIsuUUID string `db:"jia_isu_uuid"`
Timestamp time.Time `db:"timestamp"`
IsSitting bool `db:"is_sitting"`
Condition string `db:"condition"`
Message string `db:"message"`
}
rows := make([]InsertRow, 0, len(req))
for _, cond := range req {
timestamp := time.Unix(cond.Timestamp, 0)

if !isValidConditionFormat(cond.Condition) {
return c.String(http.StatusBadRequest, "bad request body")
}

rows = append(rows, InsertRow{
insertRowCh <- InsertRow{
JIAIsuUUID: jiaIsuUUID,
Timestamp: timestamp,
IsSitting: cond.IsSitting,
Condition: cond.Condition,
Message: cond.Message,
})
}
_, err = tx.NamedExec(
"INSERT INTO `isu_condition`"+
" (`jia_isu_uuid`, `timestamp`, `is_sitting`, `condition`, `message`)"+
" VALUES (:jia_isu_uuid, :timestamp, :is_sitting, :condition, :message)",
rows)
if err != nil {
c.Logger().Errorf("db error: %v", err)
return c.NoContent(http.StatusInternalServerError)
}
}

err = tx.Commit()
Expand All @@ -1236,6 +1233,30 @@ func postIsuCondition(c echo.Context) error {
return c.NoContent(http.StatusAccepted)
}

func postIsuConditionWorker() {
rows := []InsertRow{}
ticker := time.NewTicker(100 * time.Millisecond)
for {
select {
case <-ticker.C:
if len(rows) == 0 {
continue
}
_, err := db.NamedExec(
"INSERT INTO `isu_condition`"+
" (`jia_isu_uuid`, `timestamp`, `is_sitting`, `condition`, `message`)"+
" VALUES (:jia_isu_uuid, :timestamp, :is_sitting, :condition, :message)",
rows)
if err != nil {
log.Errorf("db error: %v", err)
}
rows = []InsertRow{}
case row := <-insertRowCh:
rows = append(rows, row)
}
}
}

// ISUのコンディションの文字列がcsv形式になっているか検証
func isValidConditionFormat(conditionStr string) bool {

Expand Down

0 comments on commit 168a4dd

Please sign in to comment.