コード例 #1
0
func (p *ClassicCounter) Consume(count int64, dimensions ...string) (err error) {

	defer func() {
		go p.updateQPSCounter(err != nil, dimensions...)
	}()

	if p.isReachedQPSUpperLimit(dimensions...) {
		err = ERR_QPS_REACHED_UPPER_LIMIT.New(errors.Params{"counter": p.name, "dimensions": strings.Join(dimensions, ":")})
		return
	}

	var maxQuota int64 = 0
	if v, exist := p.getDimensionOption(LimitQuotaOption, dimensions...); exist {
		maxQuota, _ = strconv.ParseInt(v, 10, 64)
	}

	if e := p.storage.Increase(p.name+_CONSUME_, count, maxQuota, dimensions...); e != nil {
		if errors.IsErrCode(e) {
			err = e
		} else {
			err = ERR_INCREASE_COUNT_FAILED.New(errors.Params{"counter": p.name, "err": e})
		}
		return
	}

	return
}
コード例 #2
0
ファイル: base_component.go プロジェクト: jmptrader/spirit
func (p *BaseComponent) hookMessagesAfter(inPortName string, message *ComponentMessage) (metadatas []MessageHookMetadata, err error) {
	var hooks []string
	var exist bool

	if hooks, exist = p.inPortHooks[inPortName]; !exist {
		return
	}

	newMetadatas := []MessageHookMetadata{}
	for index, hookName := range hooks {
		var hook MessageHook
		if hook, err = hookFactory.Get(hookName); err != nil {
			return
		}

		if ignored, matadata, e := hook.HookAfter(message.hooksMetaData, newMetadatas, &message.payload); e != nil {
			if !errors.IsErrCode(e) {
				e = ERR_MESSAGE_HOOK_ERROR.New(errors.Params{"err": e, "name": p.name, "port": inPortName, "index": index, "count": len(hooks), "hookName": hook.Name(), "event": "after"})
			}
			logs.Error(e)

			if !ignored {
				err = e
				return
			}
		} else if !ignored {
			matadata.HookName = hookName
			newMetadatas = append(newMetadatas, matadata)
		}
	}
	metadatas = newMetadatas
	return
}
コード例 #3
0
ファイル: base_component.go プロジェクト: jmptrader/spirit
func (p *BaseComponent) hookMessagesBefore(inPortName string, message *ComponentMessage) (metadatas []MessageHookMetadata, err error) {
	preMetadata := message.hooksMetaData
	if preMetadata == nil || len(preMetadata) == 0 {
		return
	}

	var hooks []string
	var exist bool

	if hooks, exist = p.inPortHooks[inPortName]; !exist {
		err = ERR_INPORT_NOT_BIND_HOOK.New(errors.Params{"inPortName": inPortName})
		return
	}

	newMetadatas := []MessageHookMetadata{}
	for index, metadata := range preMetadata {
		var hook MessageHook
		if hook, err = hookFactory.Get(metadata.HookName); err != nil {
			return
		}

		if ignored, matadata, e := hook.HookBefore(metadata, message.hooksMetaData, newMetadatas, &message.payload); e != nil {
			if !errors.IsErrCode(e) {
				e = ERR_MESSAGE_HOOK_ERROR.New(errors.Params{"err": e, "name": p.name, "port": inPortName, "index": index, "count": len(hooks), "hookName": hook.Name(), "event": "before"})
			}
			logs.Error(e)

			if !ignored {
				err = e
				return
			}
		} else if !ignored {
			newMetadatas = append(newMetadatas, matadata)
		}
	}
	metadatas = newMetadatas
	return
}
コード例 #4
0
ファイル: base_component.go プロジェクト: jmptrader/spirit
func (p *BaseComponent) handleComponentMessage(inPortName string, context interface{}, message ComponentMessage, callbacks ...OnReceiverMessageProcessed) {
	atomic.AddInt64(&p.inboxMessage, 1)
	defer atomic.AddInt64(&p.inboxMessage, -1)

	defer func() {
		if callbacks != nil {
			for _, callback := range callbacks {
				callback(context)
			}
		}
	}()

	var handler ComponentHandler
	var err error
	var exist bool

	if message.graph == nil {
		logs.Error(ERR_MESSAGE_GRAPH_IS_NIL.New())
		return
	}

	if handler, exist = p.inPortHandler[inPortName]; !exist {
		logs.Error(fmt.Sprintf("in port of %s not exist", inPortName))
	}

	var address MessageAddress
	var nextGraphIndex int32 = 0
	var content interface{}

	var newMetadatas []MessageHookMetadata
	if _, err = p.hookMessages(inPortName, HookEventBeforeCallHandler, &message); err != nil {
		if address, exist = message.graph[ERROR_MSG_ADDR]; exist {
			errCode := err.(errors.ErrCode)
			message.payload.err.AddressId = message.currentGraphIndex
			message.payload.err.Id = errCode.Id()
			message.payload.err.Namespace = errCode.Namespace()
			message.payload.err.Code = errCode.Code()
			message.payload.err.Message = errCode.Error()

			nextGraphIndex = ERROR_MSG_ADDR_INT //forword to the error port
		} else {
			return
		}
	} else {
		if content, err = p.callHandlerWithRecover(handler, &message.payload); err != nil {
			if !errors.IsErrCode(err) {
				err = ERR_COMPONENT_HANDLER_RETURN_ERROR.New(errors.Params{"err": err, "name": p.name, "port": inPortName})
			}

			logs.Error(err)

			if address, exist = message.graph[ERROR_MSG_ADDR]; exist {
				errCode := err.(errors.ErrCode)
				message.payload.err.AddressId = message.currentGraphIndex
				message.payload.err.Id = errCode.Id()
				message.payload.err.Namespace = errCode.Namespace()
				message.payload.err.Code = errCode.Code()
				message.payload.err.Message = errCode.Error()

				nextGraphIndex = ERROR_MSG_ADDR_INT //forword to the error port
			} else {
				return
			}
		} else {
			message.payload.SetContent(content)

			if newMetadatas, err = p.hookMessages(inPortName, HookEventAfterCallHandler, &message); err != nil {
				if address, exist = message.graph[ERROR_MSG_ADDR]; exist {
					errCode := err.(errors.ErrCode)
					message.payload.err.AddressId = message.currentGraphIndex
					message.payload.err.Id = errCode.Id()
					message.payload.err.Namespace = errCode.Namespace()
					message.payload.err.Code = errCode.Code()
					message.payload.err.Message = errCode.Error()

					nextGraphIndex = ERROR_MSG_ADDR_INT //forword to the error port
				} else {
					return
				}
			}

			if address, exist = message.graph[strconv.Itoa(int(message.currentGraphIndex)+1)]; exist {
				nextGraphIndex = message.currentGraphIndex + 1 //forword the next component
			} else {
				return
			}
		}
	}

	message.hooksMetaData = newMetadatas
	message.currentGraphIndex = nextGraphIndex

	if nextInPortName, exist := p.inPortUrlIndex[address.Type+"|"+address.Url]; exist {
		p.handleComponentMessage(nextInPortName, nil, message)
		EventCenter.PushEvent(EVENT_BASE_COMPONENT_INTERNAL_MESSAGE, p.name, address, message)
	} else {
		p.sendMessage(address, message)
	}

	return
}