Ejemplo n.º 1
0
 def __init__(self, listen_addr_list=[]):
     super(TianQiServer, self).__init__(listen_addr_list)
     self._listen_backlog = 1024
     self._epoll_time_out = 0.1
     self._timer_time_out = 2
     self._max_session_count = 10240
     self._last_update_time = Utils.gettimesamp()
     self._last_flush_time = Utils.gettimesamp()
Ejemplo n.º 2
0
    def onProcessTimerTask(self):
        now = Utils.gettimesamp()
        if now > self._last_update_time + 1800:
            willupdatekeys = KvCache.get_willupdatekeys_from_cache()
            self._last_update_time = now
            for k in willupdatekeys:
                task = Protocol.REQUEST_PACKET()
                task.request_type = "JOB"
                task.request_key = k
                task.request_tick = Utils.gettickcount()
                MsgPipe.MsgPipe.push(fileno=0, sessionid=0, msgdata=task)

        if now > self._last_flush_time + 60:
            self._last_flush_time = now
            KvCache.flush_cache()
Ejemplo n.º 3
0
 def needUpdate(self):
     now = Utils.gettimesamp()
     if now > self.request_time + 86400:
         return False
     if now > self.update_time + 1800:
         return True
     return False
Ejemplo n.º 4
0
    def _dispatch_packet(self, head=Protocol.NET_HEAD(), packet_data=""):
        header = packet_data
        url = Protocol.get_url(header)
        if not url:
            return
        params = Protocol.urlparams2dict(url)
        try:
            service = url[0:8]
            if service != "/chelun?":
                #logger.error("Recv|not chelun service:%s|header:%s",url,header)
                return
            query = params["query"]
            cityid = params["cityid"]
            if not query:
                logger.error("Recv|invalid query:%s", url)
                return

            citykey = DataSource.get_weather_citykey(cityid)
            if not citykey:
                logger.error("Recv|invalid cityid:%s", url)
                return

#logger.debug("Recv|Url:%s",url)
            task = Protocol.REQUEST_PACKET()
            task.request_type = head.request_type
            task.request_key = citykey
            task.request_tick = Utils.gettickcount()

            if query in ["weather", "weather2"]:
                weather_data = KvCache.get_data_from_cache(k=citykey)
                if not weather_data:
                    logger.debug(
                        "Recv|get_data_from_cache failed;then ask worker;%s",
                        url)
                    MsgPipe.MsgPipe.push(fileno=self.client_socket.fileno(),
                                         sessionid=self.client_session_id,
                                         msgdata=task)
                    return
                logger.debug("Recv|get_data_from_cache succ;%s", url)
                self.send_data_bychunked(weather_data)
                return
            elif query == "weather3":
                task.request_type = "TEST"
                logger.debug("Recv|Test;%s", url)
                MsgPipe.MsgPipe.push(fileno=self.client_socket.fileno(),
                                     sessionid=self.client_session_id,
                                     msgdata=task)
                return
            pass

        except:
            pass
        pass
Ejemplo n.º 5
0
 def out(self, msgnode):
     (msgid, tick, fileno, sessionid, msgdata) = msgnode
     request = msgdata
     #request=Protocol.REQUEST_PACKET()
     request.delay_tick = Utils.gettickcount()
     #logger.debug("Worker|PrefetchWeather,key:%s",request.request_key)
     if not request.request_key:
         logger.error("Worker|invalid requestKey")
         return
     data = ""
     if "TEST" == request.request_type:
         data = DataSource.get_weather_data(request.request_key)
     else:
         data = DataSource.get_enc_weather_data(request.request_key)
     request.process_tick = Utils.gettickcount()
     result = Protocol.RESPONSE_PACKET()
     result.copyHeader(request)
     result.data = data
     msgnode = (fileno, sessionid, msgid, result)
     MsgPipe.FeedbackPipe.push(msgnode)
     pass
Ejemplo n.º 6
0
def get_data_from_cache(k=""):
    global weather_data_cache
    global weather_data_lock
    global weather_get_count
    global weather_hit_count
    with weather_data_lock:
        weather_get_count += 1
        if k in weather_data_cache:
            node = weather_data_cache[k]
            if node.isValid():
                # print "GET:",id(node.data)
                weather_data_cache[k].request_time = Utils.gettimesamp()
                weather_hit_count += 1
                return copy.deepcopy(node.data)
            else:
                logger.debug(
                    "KvCache|get_data_from_cache|invalid key:%s,now:%d last:%d id:%d  get:%d hit:%d",
                    node.key, Utils.gettimesamp(), node.update_time, id(node),
                    weather_get_count, weather_hit_count)
                pass

        return None
Ejemplo n.º 7
0
 def send_data_bychunked(self, data=""):
     ss = '''HTTP/1.1 200 OK\r\nServer: nginx/1.6.0\r\nConnection: close\r\nContent-Type: text/html\r\nTransfer-Encoding: chunked\r\n'''
     ss += "\r\n"
     i = 0
     total = len(data)
     while i < total:
         chucked_size = 0
         if total >= i + 512:
             chucked_size = 512
         else:
             chucked_size = total - i
         checked_data = data[i:i + chucked_size]
         ss += Utils.dec2hex(chucked_size) + "\r\n"
         ss += checked_data + "\r\n"
         i += chucked_size
     ss += "0\r\n\r\n"
     self.sendData(ss)
Ejemplo n.º 8
0
def set_data_to_cache(k="", data=""):
    # print "SET:",id(data)
    global weather_data_cache
    global weather_data_lock
    now = Utils.gettimesamp()
    #logger.debug("KvCache|set_data_to_cache|update key:%s t:%d",k,now)
    with weather_data_lock:
        if k not in weather_data_cache:
            node = WEATHER_DATA()
            node.key = k
            node.request_time = now
            node.update_time = node.request_time
            node.data = data
            weather_data_cache[k] = node
            #logger.debug("KvCache|set_data_to_cache|update key:%s t:%d id:%d",k,now,id(node))
            return k

        weather_data_cache[k].update_time = now
        weather_data_cache[k].data = data
        #logger.debug("KvCache|set_data_to_cache|update key:%s t:%d id2:%d",k,now,id(weather_data_cache[k]))
        return k
Ejemplo n.º 9
0
 def isValid(self):
     now = Utils.gettimesamp()
     return now < self.update_time + 3600