Ejemplo n.º 1
0
func (ctx *PQContext) asyncPop(asyncId string, lockTimeout, popWaitTimeout, limit int64, lock bool) apis.IResponse {
	if len(asyncId) != 0 && popWaitTimeout == 0 {
		return resp.NewAsyncResponse(asyncId, mpqerr.ERR_ASYNC_WAIT)
	}
	go func() {
		ctx.asyncGroup.Add(1)
		res := ctx.pq.Pop(lockTimeout, popWaitTimeout, limit, lock)
		resp := resp.NewAsyncResponse(asyncId, res)
		if err := ctx.responseWriter.WriteResponse(resp); err != nil {
			log.LogConnError(err)
		}
		ctx.asyncGroup.Done()
	}()
	return resp.NewAsyncAccept(asyncId)
}
Ejemplo n.º 2
0
// Push message to the queue.
// Pushing message automatically enables auto expiration.
func (ctx *PQContext) Push(params []string) apis.IResponse {
	var err *mpqerr.ErrorResponse
	var msgId string
	var priority int64 = 0
	var syncWait bool
	var asyncId string
	var payload string

	cfg := ctx.pq.config
	delay := cfg.DeliveryDelay
	msgTtl := cfg.MsgTtl

	for len(params) > 0 {
		switch params[0] {
		case PRM_ID:
			params, msgId, err = mpqproto.ParseUserItemId(params)
		case PRM_PRIORITY:
			params, priority, err = mpqproto.ParseInt64Param(params, 0, math.MaxInt64)
		case PRM_PAYLOAD:
			params, payload, err = mpqproto.ParseStringParam(params, 1, PAYLOAD_LIMIT)
		case PRM_DELAY:
			params, delay, err = mpqproto.ParseInt64Param(params, 0, conf.CFG_PQ.MaxDeliveryDelay)
		case PRM_MSG_TTL:
			params, msgTtl, err = mpqproto.ParseInt64Param(params, 0, conf.CFG_PQ.MaxMessageTTL)
		case PRM_SYNC_WAIT:
			params = params[1:]
			syncWait = true
		case PRM_ASYNC:
			params, asyncId, err = mpqproto.ParseItemId(params)
		default:
			return mpqerr.UnknownParam(params[0])
		}
		if err != nil {
			return err
		}
	}
	if len(msgId) == 0 {
		msgId = ctx.idGen.RandId()
	}

	if syncWait {
		if len(asyncId) == 0 {
			res := ctx.pq.Push(msgId, payload, msgTtl, delay, priority)
			if !res.IsError() {
				ctx.pq.WaitFlush()
			}
			return res
		} else {
			go func() {
				ctx.asyncGroup.Add(1)
				res := ctx.pq.Push(msgId, payload, msgTtl, delay, priority)
				if !res.IsError() {
					ctx.pq.WaitFlush()
				}
				ctx.responseWriter.WriteResponse(resp.NewAsyncResponse(asyncId, res))
				ctx.asyncGroup.Done()
			}()
			return resp.NewAsyncAccept(asyncId)
		}
	}
	if len(asyncId) > 0 {
		return resp.NewAsyncResponse(asyncId, mpqerr.ERR_ASYNC_PUSH)
	}
	return ctx.pq.Push(msgId, payload, msgTtl, delay, priority)
}