Skip to content

Commit

Permalink
update timer calculation in go
Browse files Browse the repository at this point in the history
  • Loading branch information
czhou578 committed Jul 11, 2024
1 parent 9d11ec7 commit 3cc384a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
14 changes: 10 additions & 4 deletions go-backend/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,11 @@ func getEmployeeIDCount(c *gin.Context) {
}

func newCategory(c *gin.Context) {
lastId := models.NewCategoryInsert()
lastId, operation_time := models.NewCategoryInsert()

c.IndentedJSON(http.StatusOK, lastId)
combinedString := fmt.Sprintf("Inserted Id is %d, Backend operation took %dms", lastId, operation_time)
c.Header("Content-Type", "text/plain; charset=utf-8")
c.String(200, combinedString)
}

// func UpdateCustomer(c *gin.Context) {
Expand All @@ -95,12 +97,16 @@ func newCategory(c *gin.Context) {
// }

func UpdateCustomer(c *gin.Context) {
updatedId, err := models.UpdateCustomerWithRetry()
updatedId, operation_time, err := models.UpdateCustomerWithRetry()
fmt.Println("operation time, ", operation_time)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.IndentedJSON(http.StatusOK, updatedId)
fmt.Println(updatedId)
combinedString := fmt.Sprintf("Updated Id. Backend operation took %dms", operation_time)
c.Header("Content-Type", "text/plain; charset=utf-8")
c.String(200, combinedString)
}

func DeleteSalesorder(c *gin.Context) {
Expand Down
28 changes: 19 additions & 9 deletions go-backend/api/models/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ func GetCountNumId() ([]EmployeeId, map[string]int64) {

}

func NewCategoryInsert() int64 {
func NewCategoryInsert() (int64, int64) {
start := time.Now()

db, err := sql.Open("mysql", dbuser+":"+dbpass+"@tcp(127.0.0.1:3306)/"+dbname)

if err != nil {
Expand All @@ -137,17 +139,19 @@ func NewCategoryInsert() int64 {
if err != nil {
log.Fatal(err)
}
operation_time := time.Since(start).Milliseconds()

fmt.Printf("The last inserted row id: %d\n", lastId)
return lastId
return lastId, operation_time
}

func UpdateCustomerWithRetry() (int64, error) {
func UpdateCustomerWithRetry() (int64, int64, error) {

var lastErr error
for i := 0; i < maxRetries; i++ {
lastId, err := UpdateCustomer()
lastId, operation_time, err := UpdateCustomer()
if err == nil {
return lastId, nil
return lastId, operation_time, nil
}

if isLockTimeout(err) {
Expand All @@ -158,12 +162,15 @@ func UpdateCustomerWithRetry() (int64, error) {
continue
}

return 0, err // If it's not a lock timeout, return immediately
return 0, operation_time, err // If it's not a lock timeout, return immediately
}
return 0, fmt.Errorf("failed after %d attempts: %v", maxRetries, lastErr)

return 0, 0, fmt.Errorf("failed after %d attempts: %v", maxRetries, lastErr)
}

func UpdateCustomer() (int64, error) {
func UpdateCustomer() (int64, int64, error) {
start := time.Now()

db, err := sql.Open("mysql", dbuser+":"+dbpass+"@tcp(127.0.0.1:3306)/"+dbname)

if err != nil {
Expand All @@ -180,7 +187,10 @@ func UpdateCustomer() (int64, error) {

lastId, err := results.RowsAffected()

return lastId, err
operation_time := time.Since(start).Milliseconds()
fmt.Println("operation time in database, ", operation_time)

return lastId, operation_time, err
}

func isLockTimeout(err error) bool {
Expand Down

0 comments on commit 3cc384a

Please sign in to comment.