Skip to content
Permalink
Browse files

Use database value instead of external services

  • Loading branch information...
takonomura committed Sep 8, 2019
1 parent 8f6d41e commit 1cd1a0eb07e81afa88975f9d58500c7a5518e71f
Showing with 4 additions and 42 deletions.
  1. +4 −42 isucari/webapp/go/main.go
@@ -14,9 +14,7 @@ import (
"os/exec"
"path/filepath"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"

_ "github.com/go-sql-driver/mysql"
@@ -150,7 +148,7 @@ type TransactionEvidence struct {
CreatedAt time.Time `json:"-" db:"created_at"`
UpdatedAt time.Time `json:"-" db:"updated_at"`

ReserveID string `json:"-" db:"reserve_id"` // shipping table
ShippingStatus string `json:"-" db:"shipping_status"` // shipping table
}

type Shipping struct {
@@ -989,7 +987,7 @@ func getTransactions(w http.ResponseWriter, r *http.Request) {
}
var transactionEvidences map[int64]TransactionEvidence
if len(itemIDs) > 0 {
query, args, err := sqlx.In("SELECT c.item_id AS item_id, c.status AS status, s.reserve_id AS reserve_id FROM `transaction_evidences` c LEFT JOIN shippings s ON s.transaction_evidence_id = c.id WHERE c.`item_id` IN (?) ", itemIDs)
query, args, err := sqlx.In("SELECT c.item_id AS item_id, c.status AS status, s.status AS shipping_status FROM `transaction_evidences` c LEFT JOIN shippings s ON s.transaction_evidence_id = c.id WHERE c.`item_id` IN (?) ", itemIDs)
if err != nil {
log.Print(err)
outputErrorMsg(w, http.StatusInternalServerError, "db error")
@@ -1009,41 +1007,6 @@ func getTransactions(w http.ResponseWriter, r *http.Request) {
transactionEvidences[c.ItemID] = c
}
}
var wg sync.WaitGroup
var shipmentStatuses sync.Map
var concurrentError atomic.Value
wg.Add(len(transactionEvidences))
for _, t := range transactionEvidences {
go func(t TransactionEvidence) {
defer wg.Done()
if t.ReserveID == "" {
return
}
ssr, err := APIShipmentStatus(r.Context(), getShipmentServiceURL(r.Context()), &APIShipmentStatusReq{
ReserveID: t.ReserveID,
})
if err != nil && strings.HasPrefix(err.Error(), "status code: 400; body: <html>") {
log.Printf("Retrying (concurrency: %d)", len(transactionEvidences))
time.Sleep(10 * time.Millisecond)
ssr, err = APIShipmentStatus(r.Context(), getShipmentServiceURL(r.Context()), &APIShipmentStatusReq{
ReserveID: t.ReserveID,
})
}
if err != nil {
concurrentError.Store(err)
return
}
shipmentStatuses.Store(t.ItemID, ssr)
}(t)
}
wg.Wait()
cerr := concurrentError.Load()
if cerr != nil {
log.Print(cerr)
outputErrorMsg(w, http.StatusInternalServerError, "failed to request to shipment service")
tx.Rollback()
return
}

itemDetails := []ItemDetail{}
for _, item := range items {
@@ -1093,16 +1056,15 @@ func getTransactions(w http.ResponseWriter, r *http.Request) {

transactionEvidence, ok := transactionEvidences[item.ID]
if ok {
ssr, ok := shipmentStatuses.Load(item.ID)
if !ok {
if transactionEvidence.ShippingStatus == "" {
outputErrorMsg(w, http.StatusNotFound, "shipping not found")
tx.Rollback()
return
}

itemDetail.TransactionEvidenceID = transactionEvidence.ID
itemDetail.TransactionEvidenceStatus = transactionEvidence.Status
itemDetail.ShippingStatus = ssr.(*APIShipmentStatusRes).Status
itemDetail.ShippingStatus = transactionEvidence.ShippingStatus
}

itemDetails = append(itemDetails, itemDetail)

0 comments on commit 1cd1a0e

Please sign in to comment.
You can’t perform that action at this time.