示例#1
0
文件: pqctx.go 项目: vburenin/firempq
// UpdateLockById sets a user defined message lock timeout.
// It works only for locked messages.
func (ctx *PQContext) UpdateLockById(params []string) apis.IResponse {
	var err *mpqerr.ErrorResponse
	var msgId string
	var lockTimeout int64 = -1

	for len(params) > 0 {
		switch params[0] {
		case PRM_ID:
			params, msgId, err = mpqproto.ParseItemId(params)
		case PRM_LOCK_TIMEOUT:
			params, lockTimeout, err = mpqproto.ParseInt64Param(params, 0, conf.CFG_PQ.MaxLockTimeout)
		default:
			return mpqerr.UnknownParam(params[0])
		}
		if err != nil {
			return err
		}
	}

	if len(msgId) == 0 {
		return mpqerr.ERR_MSG_ID_NOT_DEFINED
	}

	if lockTimeout < 0 {
		return mpqerr.ERR_MSG_TIMEOUT_NOT_DEFINED
	}
	return ctx.pq.UpdateLockById(msgId, lockTimeout)
}
示例#2
0
文件: pqctx.go 项目: vburenin/firempq
// UpdateLockByRcpt updates message lock according to provided receipt.
func (ctx *PQContext) UpdateLockByRcpt(params []string) apis.IResponse {
	var err *mpqerr.ErrorResponse
	var rcpt string
	var lockTimeout int64 = -1

	for len(params) > 0 {
		switch params[0] {
		case PRM_RECEIPT:
			params, rcpt, err = mpqproto.ParseReceiptParam(params)
		case PRM_LOCK_TIMEOUT:
			params, lockTimeout, err = mpqproto.ParseInt64Param(params, 0, conf.CFG_PQ.MaxLockTimeout)
		default:
			return mpqerr.UnknownParam(params[0])
		}
		if err != nil {
			return err
		}
	}

	if len(rcpt) == 0 {
		return mpqerr.ERR_NO_RECEIPT
	}

	if lockTimeout < 0 {
		return mpqerr.ERR_MSG_TIMEOUT_NOT_DEFINED
	}
	return ctx.pq.UpdateLockByRcpt(rcpt, lockTimeout)
}
示例#3
0
文件: pqctx.go 项目: vburenin/firempq
// Pop message from queue completely removing it.
func (ctx *PQContext) Pop(params []string) apis.IResponse {
	var err *mpqerr.ErrorResponse
	var limit int64 = 1
	var asyncId string

	popWaitTimeout := ctx.pq.config.PopWaitTimeout

	for len(params) > 0 {
		switch params[0] {
		case PRM_LIMIT:
			params, limit, err = mpqproto.ParseInt64Param(params, 1, conf.CFG_PQ.MaxPopBatchSize)
		case PRM_POP_WAIT:
			params, popWaitTimeout, err = mpqproto.ParseInt64Param(params, 0, conf.CFG_PQ.MaxPopWaitTimeout)
		case PRM_ASYNC:
			params, asyncId, err = mpqproto.ParseItemId(params)
		default:
			return mpqerr.UnknownParam(params[0])
		}
		if err != nil {
			return err
		}
	}
	if len(asyncId) > 0 {
		return ctx.asyncPop(asyncId, 0, popWaitTimeout, limit, false)
	} else {
		return ctx.pq.Pop(0, popWaitTimeout, limit, false)
	}
}
示例#4
0
文件: pqctx.go 项目: vburenin/firempq
func ParsePQConfig(params []string) (*conf.PQConfig, apis.IResponse) {
	var err *mpqerr.ErrorResponse

	cfg := DefaultPQConfig()
	for len(params) > 0 {
		switch params[0] {
		case CPRM_MSG_TTL:
			params, cfg.MsgTtl, err = mpqproto.ParseInt64Param(params, 0, conf.CFG_PQ.MaxMessageTTL)
		case CPRM_MAX_MSG_SIZE:
			params, cfg.MaxMsgSize, err = mpqproto.ParseInt64Param(params, 1024, conf.CFG_PQ.MaxMessageSize)
		case CPRM_MAX_MSGS_IN_QUEUE:
			params, cfg.MaxMsgsInQueue, err = mpqproto.ParseInt64Param(params, 0, math.MaxInt64)
		case CPRM_DELIVERY_DELAY:
			params, cfg.DeliveryDelay, err = mpqproto.ParseInt64Param(params, 0, conf.CFG_PQ.MaxDeliveryDelay)
		case CPRM_POP_LIMIT:
			params, cfg.PopCountLimit, err = mpqproto.ParseInt64Param(params, 0, math.MaxInt64)
		case CPRM_LOCK_TIMEOUT:
			params, cfg.PopLockTimeout, err = mpqproto.ParseInt64Param(params, 0, conf.CFG_PQ.MaxLockTimeout)
		case CPRM_FAIL_QUEUE:
			params, cfg.PopLimitQueueName, err = mpqproto.ParseItemId(params)
		case CPRM_POP_WAIT:
			params, cfg.PopWaitTimeout, err = mpqproto.ParseInt64Param(params, 0, conf.CFG_PQ.MaxPopWaitTimeout)
		default:
			return nil, mpqerr.UnknownParam(params[0])
		}
		if err != nil {
			return nil, err
		}
	}
	return cfg, resp.OK_RESPONSE
}
示例#5
0
文件: pqctx.go 项目: vburenin/firempq
func (ctx *PQContext) SetParamValue(params []string) apis.IResponse {
	var err *mpqerr.ErrorResponse
	cfg := ctx.pq.config
	msgTtl := cfg.MsgTtl
	maxMsgsInQueue := cfg.MaxMsgsInQueue
	maxMsgSize := cfg.MaxMsgSize
	popLimit := cfg.PopCountLimit
	deliveryDelay := cfg.DeliveryDelay
	lockTimeout := cfg.PopLockTimeout
	failQueue := ""

	if len(params) == 0 {
		return mpqerr.ERR_CMD_PARAM_NOT_PROVIDED
	}

	pqParams := &PQueueParams{}

	for len(params) > 0 {
		switch params[0] {
		case CPRM_MSG_TTL:
			params, msgTtl, err = mpqproto.ParseInt64Param(params, 1, conf.CFG_PQ.MaxMessageTTL)
			pqParams.MsgTTL = &msgTtl
		case CPRM_MAX_MSG_SIZE:
			params, maxMsgSize, err = mpqproto.ParseInt64Param(params, 1024, conf.CFG_PQ.MaxMessageSize)
			pqParams.MaxMsgSize = &maxMsgSize
		case CPRM_MAX_MSGS_IN_QUEUE:
			params, maxMsgsInQueue, err = mpqproto.ParseInt64Param(params, 0, math.MaxInt64)
			pqParams.MaxMsgsInQueue = &maxMsgsInQueue
		case CPRM_DELIVERY_DELAY:
			params, deliveryDelay, err = mpqproto.ParseInt64Param(params, 0, conf.CFG_PQ.MaxDeliveryDelay)
			pqParams.DeliveryDelay = &deliveryDelay
		case CPRM_POP_LIMIT:
			params, popLimit, err = mpqproto.ParseInt64Param(params, 0, math.MaxInt64)
			pqParams.PopCountLimit = &popLimit
		case CPRM_LOCK_TIMEOUT:
			params, lockTimeout, err = mpqproto.ParseInt64Param(params, 0, conf.CFG_PQ.MaxLockTimeout)
			pqParams.PopLockTimeout = &lockTimeout
		case CPRM_FAIL_QUEUE:
			params, pqParams.FailQueue, err = mpqproto.ParseItemId(params)
			pqParams.FailQueue = failQueue
		default:
			return mpqerr.UnknownParam(params[0])
		}
		if err != nil {
			return err
		}
	}
	return ctx.pq.SetParams(pqParams)
}
示例#6
0
文件: pqctx.go 项目: vburenin/firempq
func (ctx *PQContext) CheckTimeouts(params []string) apis.IResponse {
	var err *mpqerr.ErrorResponse
	var ts int64 = -1
	for len(params) > 0 {
		switch params[0] {
		case PRM_TIMESTAMP:
			params, ts, err = mpqproto.ParseInt64Param(params, 0, math.MaxInt64)
		default:
			return mpqerr.UnknownParam(params[0])
		}
		if err != nil {
			return err
		}
	}
	if ts < 0 {
		return mpqerr.ERR_TS_PARAMETER_NEEDED
	}
	return ctx.pq.CheckTimeouts(ts)
}
示例#7
0
文件: pqctx.go 项目: vburenin/firempq
// 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)
}