func (self *MockListener) OnMessageCheck(tx *protocol.TxResponse) error {
	log.Debug("MockListener|OnMessageCheck|%s\n", tx.MessageId)
	v, _ := tx.GetProperty("tradeno")
	log.DebugLog("kite_client", "MockListener|OnMessageCheck|PROP|%s\n", v)
	tx.Commit()
	return nil
}
Beispiel #2
0
func (self *MessageStore) recoverSnapshot() {
	//current segmentid
	if len(self.segments) > 0 {

		//replay log
		for i, s := range self.segments {
			err := s.Open(self.replay)
			if nil != err {
				log.ErrorLog("kite_store", "MessageStore|recoverSnapshot|Fail|%s", err, s.slog.path)
				panic(err)
			}

			//last segments
			if i == len(self.segments)-1 {
				if nil != err {
					panic("MessageStore|Load Last Segment|FAIL|" + err.Error())
				}

				//set snapshost status
				if len(s.chunks) > 0 {
					self.chunkId = s.chunks[len(s.chunks)-1].id
				}
			}
			log.DebugLog("kite_store", "MessageStore|recoverSnapshot|%s", s.name)
		}
	}
}
Beispiel #3
0
//处理push
func (self *ApnsHttpServer) handlePush(out http.ResponseWriter, req *http.Request) {

	resp := &response{}
	resp.Status = RESP_STATUS_SUCC
	if req.Method == "GET" {
		//返回不支持的请求方式
		resp.Status = RESP_STATUS_INVALID_PROTO
		resp.Error = errors.New("Unsupport Get method Invoke!")

	} else if req.Method == "POST" {

		//pushType
		pushType := req.PostFormValue("pt") //先默认采用Enhanced方式
		//接卸对应的token和payload
		token, payload := self.decodePayload(req, resp)

		trace := req.PostFormValue("trace")

		expiredSeconds := req.PostFormValue("expiredSeconds")
		expiredTime := self.expiredTime
		if len(expiredSeconds) > 0 {
			t, err := strconv.ParseInt(expiredSeconds, 10, 32)
			if nil == err {
				expiredTime = uint32(t)
			}
		}

		//----------------如果依然是成功状态则证明当前可以发送
		if RESP_STATUS_SUCC == resp.Status {

			func() {
				defer func() {
					if re := recover(); nil != re {
						stack := re.(*errors.Error).ErrorStack()
						log.ErrorLog("push_handler", "ApnsHttpServer|handlePush|SEND|FAIL|%s|%s|%s", stack, payload, trace)
						resp.Status = RESP_STATUS_ERROR
						resp.Error = errors.New(fmt.Sprintf("%s", re))
						self.write(out, resp)
					}
				}()
				self.innerSend(pushType, token, payload, resp, expiredTime)
				self.write(out, resp)
				log.DebugLog("push_handler", "ApnsHttpServer|handlePush|SUCC|%s|%s|%s", resp, payload, trace)
			}()

		} else {
			log.WarnLog("push_handler", "ApnsHttpServer|handlePush|FAIL|%s|%s|%s", resp, payload, trace)
			self.write(out, resp)
		}

	}

}
//重发逻辑
func (self *ApnsClient) resend(ch chan *entry.Message) {

	for self.running {
		select {
		case <-time.After(5 * time.Second):
		case msg := <-ch:
			//发送之......
			self.sendMessage(msg)
			self.resendCounter.Incr(1)
			log.DebugLog("push_client", "APNSCLIENT|RESEND|%s\n", msg)
		}
	}

}
Beispiel #5
0
//发送非flymessage
func (self *PersistentHandler) sendUnFlyMessage(ctx *DefaultPipelineContext, pevent *persistentEvent) {
	saveSucc := true

	// log.DebugLog("kite_handler", "PersistentHandler|sendUnFlyMessage|%s", pevent.entity)

	//提交并且开启优化
	if self.fly &&
		pevent.entity.Header.GetCommit() {
		//先投递尝试投递一次再去根据结果写存储
		ch := make(chan []string, 1) //用于返回尝试投递结果
		self.send(ctx, pevent, ch)
		/*如果是成功的则直接返回处理存储成功的
		 *如果失败了,则需要持久化
		 */
		failGroups := <-ch
		//失败或者超时的持久化
		if len(failGroups) > 0 {
			pevent.entity.DeliverCount += 1
			if nil != failGroups {
				pevent.entity.FailGroups = failGroups
			}

			//写入到持久化存储里面
			saveSucc = self.kitestore.Save(pevent.entity)
			//再投递
			self.send(ctx, pevent, nil)
		} else {
			log.DebugLog("kite_handler", "PersistentHandler|sendUnFlyMessage|FLY|%s", pevent.entity)
		}

	} else {
		// now := time.Now().UnixNano()
		//写入到持久化存储里面,再投递
		saveSucc = self.kitestore.Save(pevent.entity)
		// log.DebugLog("kite_handler", "PersistentHandler|sendUnFlyMessage|cost:%d", time.Now().UnixNano()-now)
		if saveSucc && pevent.entity.Commit {
			self.send(ctx, pevent, nil)
		}

	}

	//发送存储结果ack
	remoteEvent := NewRemotingEvent(storeAck(pevent.opaque,
		pevent.entity.Header.GetMessageId(), saveSucc, fmt.Sprintf("Store Result %t", saveSucc)),
		[]string{pevent.remoteClient.RemoteAddr()})
	ctx.SendForward(remoteEvent)

}
Beispiel #6
0
//delete chunk
func (self *Segment) Delete(cid int64) bool {
	idx := sort.Search(len(self.chunks), func(i int) bool {
		return self.chunks[i].id >= cid
	})

	if idx < len(self.chunks) {
		//mark delete
		s := self.chunks[idx]
		if s.flag != DELETE && s.flag != EXPIRED {
			s.flag = DELETE
			return true
		}
	} else {
		log.DebugLog("kite_store", "Segment|Delete|NO Chunk|chunkid:%d|%d", cid, idx, len(self.chunks))
	}

	return true
}
Beispiel #7
0
func loadTomlConf(path, clusterName, bindHost string, pprofPort int, so *ServerOption) error {
	f, err := os.Open(path)
	if err != nil {
		return err
	}
	defer f.Close()
	buff, rerr := ioutil.ReadAll(f)
	if nil != rerr {
		return rerr
	}
	log.DebugLog("kite_server", "ServerConfig|Parse|toml:%s", string(buff))
	//读取配置
	var option Option
	err = toml.Unmarshal(buff, &option)
	if nil != err {
		return err
	}

	cluster, ok := option.Clusters[clusterName]
	if !ok {
		return errors.New("no cluster config for " + clusterName)
	}

	zk, exist := option.Zookeeper[cluster.Env]
	if !exist {
		return errors.New("no zk  for " + clusterName + ":" + cluster.Env)
	}

	//解析
	so.zkhosts = zk.Hosts
	so.topics = cluster.Topics
	so.deliveryFirst = cluster.DeliveryFirst
	so.dlqExecHour = cluster.DlqExecHour
	so.logxml = cluster.Logxml
	so.db = cluster.Db
	so.deliveryTimeout = time.Duration(cluster.DeliverySeconds * int64(time.Second))
	so.maxDeliverWorkers = cluster.MaxDeliverWorkers
	so.recoverPeriod = time.Duration(cluster.RecoverSeconds * int64(time.Second))
	so.bindHost = bindHost
	so.pprofPort = pprofPort
	so.clusterName = clusterName
	so.configPath = path
	return nil
}
Beispiel #8
0
func (self *ApnsConnection) sendMessage(msg *entry.Message) error {
	//将当前的msg强制设置为当前conn的id作为标识
	msg.ProcessId = self.connectionId

	err, packet := msg.Encode()
	if nil != err {
		return err
	}

	length, sendErr := self.conn.Write(packet)
	if nil != err || length != len(packet) {
		log.WarnLog("push_client", "CONNECTION|SEND MESSAGE|FAIL|%s", err)
	} else {
		log.DebugLog("push_client", "CONNECTION|SEND MESSAGE|SUCC")

	}

	return sendErr
}
Beispiel #9
0
//检查idle的数据
func (self *ConnPool) checkIdle() {
	self.mutex.Lock()
	defer self.mutex.Unlock()
	for e := self.idlePool.Back(); nil != e; e = e.Prev() {
		idleconn := e.Value.(*IdleConn)
		//如果当前时间在过期时间之后或者活动的链接大于corepoolsize则关闭
		isExpired := idleconn.expiredTime.Before(time.Now())
		if isExpired ||
			(self.idlePool.Len()+self.workPool.Len()) > self.corepoolSize {
			idleconn.conn.Close()
			idleconn = nil
			self.idlePool.Remove(e)
			log.DebugLog("apns_pool", "POOL_FACTORY|evict|Expired|%d/%d/%d",
				self.workPool.Len(), self.idlePool.Len(), (self.workPool.Len() + self.idlePool.Len()))
		}
	}

	//检查当前的连接数是否满足corepoolSize,不满足则创建
	enhanceSize := self.corepoolSize - (self.idlePool.Len() + self.workPool.Len())
	if enhanceSize > 0 {
		//创建这个数量的连接
		self.enhancedPool(enhanceSize)
	}
}
Beispiel #10
0
//内部发送代码
func (self *ApnsHttpServer) innerSend(pushType string, token string, payload *entry.PayLoad, resp *response, expiredTime uint32) {
	var sendFunc func() error

	if NOTIFY_SIMPLE_FORMAT == pushType {
		//如果为简单
		sendFunc = func() error {
			return self.apnsClient.SendSimpleNotification(token, *payload)
		}
	} else if NOTIFY_ENHANCED_FORMAT == pushType {
		//如果为扩展的
		id := self.identifierId()
		sendFunc = func() error {
			return self.apnsClient.SendEnhancedNotification(id,
				expiredTime, token, *payload)
		}

	} else {
		resp.Status = RESP_STATUS_INVALID_NOTIFY_FORMAT
		resp.Error = errors.New("Invalid notification format " + pushType)
	}

	//能直接放在chan中异步发送
	var err error
	//如果有异常则重试发送
	if RESP_STATUS_SUCC == resp.Status {
		err = sendFunc()
		if nil == err {
			log.DebugLog("push_handler", "ApnsHttpServer|SendNotification|SUCC|FORMAT:%s|%s", pushType, *payload)
		}
	}
	if nil != err {
		log.ErrorLog("push_handler", "ApnsHttpServer|SendNotification|FORMAT:%s|FAIL|IGNORED|%s|%s", pushType, *payload, err)
		resp.Status = RESP_STATUS_SEND_OVER_TRY_ERROR
		resp.Error = err
	}
}
func (self *MockListener) OnMessage(msg *protocol.QMessage) bool {
	log.DebugLog("kite_client", "MockListener|OnMessage", msg.GetHeader(), msg.GetBody())
	return true
}