예제 #1
0
//内部处理
func (self *DeliverPreHandler) send0(ctx *DefaultPipelineContext, pevent *deliverPreEvent) {
	//如果没有entity则直接查询一下db
	entity := pevent.entity
	if nil == entity {
		//查询消息
		entity = self.kitestore.Query(pevent.messageId)
		if nil == entity {
			self.kitestore.Expired(pevent.messageId)
			// log.Error("DeliverPreHandler|send0|Query|FAIL|%s\n", pevent.messageId)
			return
		}
	}

	//check entity need to deliver
	if !self.checkValid(entity) {
		self.kitestore.Expired(entity.MessageId)
		return
	}

	//统计数据
	//对该消息类型进行统计
	flow, ok := self.flowstat.TopicsFlows[pevent.header.GetTopic()]
	if ok {
		flow.Incr(1)
	}

	// log.Debug("DeliverPreHandler|send0|Query|%s", entity.Header)
	data := protocol.MarshalMessage(entity.Header, entity.MsgType, entity.GetBody())

	//构造deliverEvent
	deliverEvent := newDeliverEvent(pevent.messageId, pevent.header.GetTopic(),
		pevent.header.GetMessageType(), entity.PublishTime, pevent.attemptDeliver)

	//创建不同的packet
	switch entity.MsgType {
	case protocol.CMD_BYTES_MESSAGE:
		deliverEvent.packet = packet.NewPacket(protocol.CMD_BYTES_MESSAGE, data)
	case protocol.CMD_STRING_MESSAGE:
		deliverEvent.packet = packet.NewPacket(protocol.CMD_STRING_MESSAGE, data)
	}

	//填充订阅分组
	self.fillGroupIds(deliverEvent, entity)
	self.fillDeliverExt(deliverEvent, entity)

	//向后投递发送
	ctx.SendForward(deliverEvent)
}
예제 #2
0
func (self *KiteFileStore) Save(entity *MessageEntity) bool {

	lock, link, ol := self.hash(entity.MessageId)
	if len(ol) >= self.maxcap {
		//overflow
		return false
	} else {

		//value
		data := protocol.MarshalMessage(entity.Header, entity.MsgType, entity.GetBody())
		buff := make([]byte, len(data)+1)
		buff[0] = entity.MsgType
		copy(buff[1:], data)

		//create oplog
		ob := &opBody{
			Id:              -1,
			MessageId:       entity.MessageId,
			Topic:           entity.Header.GetTopic(),
			Commit:          entity.Commit,
			FailGroups:      entity.FailGroups,
			SuccGroups:      entity.SuccGroups,
			NextDeliverTime: entity.NextDeliverTime,
			DeliverCount:    0}

		obd, err := json.Marshal(ob)
		if nil != err {
			log.ErrorLog("kite_store", "KiteFileStore|Save|Encode|Op|FAIL|%s", err)
			return false
		}

		cmd := NewCommand(-1, entity.MessageId, buff, obd)

		//append oplog into file
		idchan := self.snapshot.Append(cmd)
		ob.saveDone = idchan
		ob.Add(1)

		//get lock
		lock.Lock()
		//push
		e := link.PushBack(ob)
		ol[entity.MessageId] = e
		lock.Unlock()
		return true
	}

}