duandazhen7306 2018-03-27 08:50
浏览 91
已采纳

在我可以使用query.Count()进行验证之前,Mongo编写了query.UpsertId多个文档

Description: I`m using mongoDB on my project. This is short logic for handler when user tries to put his item for sale. Before putting offer to mongo I validate the offer, so there would be no active offers with save assetId

Using:

  • mgo.v2

  • mongo 3.6

  • golang 1.10

Problem: If user clicks really fast sends several requests to my handler (lets say if he double click the mouse fast), validation doesn`t work as it seems like the first offer is not in the collection yet, so as a result I get 2-3 offers with same assetId.

I tried

  • Set mongoUrl?replicaSet=rs0, so our master and slaves would now about each other
  • Set time.Sleep(200 * time.Millisecond) after validation

Question:

Is there any way I can handle this with mongo instruments, or someone would suggest some other workaround?

Thank you in advance!

       count, _ := r.DB.C(sellOfferCollectionName).Find(
         bson.M{
            "state":     someState,
            "asset_id":  assetId,
            "seller_id": seller,
         },
       ).Count()

       if count > 0 {
           return 
       }       

       id := uuid.New().String()
            OfferModel := Offer{
                Id:           id,
                AssetId:      assetId,
                State:        someState,
                SellerId:     sellerId,
                CreatingDate: time.Now(),
            }
        if _, err := r.DB.C(sellOfferCollectionName).UpsertId(offer.Id, offer); err != nil {
            return err
        }

UPDATE

I tried to recreate the problem even more. So I wrote this little test code, so in result managed to write 60 documents before validation (count > 0) worked. This example fully recreates my problem.

type User struct {
  Id        string `bson:"_id"`
  FirstName string `bson:"first_name"`
  LastName  string `bson:"last_name"`
  State     bool   `bson:"state"`
}

func main() {
  mongoSession, mgErr := mgo.Dial("127.0.0.1:27018")

  if mgErr != nil {
    panic(mgErr)
  }

  var mongoDbSession = mongoSession.DB("test_db")

  for i := 0; i < 1000; i++ {
    go func() {
      count, _ := mongoDbSession.C("users").Find(
        bson.M{
          "state":      true,
          "first_name": "testUser",
        },
      ).Count()

      if count > 0 {
        return
      }

      user := User{
        Id:        uuid.New().String(),
        FirstName: "testUser",
        LastName:  "testLastName",
        State:     true,
      }

      if _, err := mongoDbSession.C("users").UpsertId(user.Id, user); err != nil {
        panic(mgErr)
      }
    }()
  }

  count, _ := mongoDbSession.C("users").Find(
    bson.M{
      "state":      true,
      "first_name": "testUser",
    },
  ).Count()

  fmt.Println(count)
  fmt.Scanln()
}
  • 写回答

2条回答 默认 最新

  • douhan6738 2018-03-29 14:28
    关注

    Eventually, after close investigation the bug, we found out that the reason was when user sent request it was handled in goroutine. Means a lot requests = a lot of concurrent goroutines. So, it out validator (check if the offer is in the collection), couldn't find it as it was not in the mongo yet. So, in the end, we decided to use redis as our validator.

    Here is short implementation:

    incr, err := redisClient.Incr(offer.AssetId).Result()
        if err != nil {
            return err
        }
    
        if incr > 1 {
            return errors.New("ASSET_ALREADY_ON_SALE")
        }
    
        redisClient.Expire(offer.AssetId, time.Second*10)
    

    Hope it will help someone facing same issue.

    Link on implementation description:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度