chenshijun
2020-03-28 91f7d13bdf60a2df6bedff6467f550c7c643f3fc
shmqueue.go
@@ -12,7 +12,7 @@
)
const (
   TimePeriodPutOrGet = time.Duration(10) * time.Microsecond
   TimePeriodPutOrGet = time.Duration(1) * time.Microsecond
)
//Element info
@@ -218,18 +218,29 @@
}
//PutForce 强制写入,失败则继续重写,直到成功为止
func (eqi *EsQueueInfo) PutForce(val ElemInfo) int {
func (eqi *EsQueueInfo) PutForce(ctx context.Context, val ElemInfo) int {
   ok, qua := eqi.Put(val)
   for !ok {
      time.Sleep(TimePeriodPutOrGet)
      ok, qua = eqi.Put(val)
   loopB:
      for {
         select {
         case <-ctx.Done():
            return 0
         default:
            ok, qua = eqi.Put(val)
            if ok {
               break loopB
            }
            time.Sleep(TimePeriodPutOrGet)
         }
      }
   }
   return qua
}
//PutForceWithinTime 在timeout ms的时间内,强制写入,失败则继续重写,直到成功为止或者超时退出
func (eqi *EsQueueInfo) PutForceWithinTime(val ElemInfo, timeout int) (bool, int) {
func (eqi *EsQueueInfo) PutForceWithinTime(ctx context.Context, val ElemInfo, timeout int) (bool, int) {
   ok, qua := eqi.Put(val)
   if ok {
      return ok, qua
@@ -238,18 +249,21 @@
   to := time.NewTimer(time.Duration(timeout) * time.Millisecond)
   defer to.Stop()
   for {
      select {
      case <-to.C:
         return false, 0
      default:
         ok, qua = eqi.Put(val)
         if ok {
            return ok, qua
   loopB:
      for {
         select {
         case <-ctx.Done():
            return false, 0
         case <-to.C:
            return false, 0
         default:
            ok, qua = eqi.Put(val)
            if ok {
               break loopB
            }
            time.Sleep(TimePeriodPutOrGet)
         }
         time.Sleep(TimePeriodPutOrGet)
      }
   }
   return ok, qua
}
@@ -296,18 +310,29 @@
}
//GetForce 强制获取数据,失败则继续获取,直到成功为止
func (eqi *EsQueueInfo) GetForce() (ElemInfo, int) {
func (eqi *EsQueueInfo) GetForce(ctx context.Context) (ElemInfo, int) {
   val, ok, qua := eqi.Get()
   for !ok {
      time.Sleep(TimePeriodPutOrGet)
      val, ok, qua = eqi.Get()
   loopB:
      for {
         select {
         case <-ctx.Done():
            return ElemInfo{}, 0
         default:
            val, ok, qua = eqi.Get()
            if ok {
               break loopB
            }
            time.Sleep(TimePeriodPutOrGet)
         }
      }
   }
   return val, qua
}
//GetForceWithinTime 在timeout ms的时间内,强制获取数据,失败则继续获取,直到成功为止
func (eqi *EsQueueInfo) GetForceWithinTime(timeout int) (ElemInfo, bool, int) {
func (eqi *EsQueueInfo) GetForceWithinTime(ctx context.Context, timeout int) (ElemInfo, bool, int) {
   val, ok, qua := eqi.Get()
   if ok {
      return val, ok, qua
@@ -316,18 +341,21 @@
   to := time.NewTimer(time.Duration(timeout) * time.Millisecond)
   defer to.Stop()
   for {
      select {
      case <-to.C:
         return ElemInfo{}, false, 0
      default:
         val, ok, qua = eqi.Get()
         if ok {
            return val, ok, qua
   loopB:
      for {
         select {
         case <-ctx.Done():
            return ElemInfo{}, false, 0
         case <-to.C:
            return ElemInfo{}, false, 0
         default:
            val, ok, qua = eqi.Get()
            if ok {
               break loopB
            }
            time.Sleep(TimePeriodPutOrGet)
         }
         time.Sleep(TimePeriodPutOrGet)
      }
   }
   return val, ok, qua
}