コード例 #1
0
ファイル: sync.py プロジェクト: Abchrisabc/rainwave
	def process_throttle(self):
		if not len(self.throttled_msgs):
			self.throttled = False
			return
		self.throttled_msgs.sort()
		# log.debug("throttle", "Throttled with %s messages" % len(self.throttled_msgs))
		action = self.throttled_msgs[0]['action']
		msg = None
		if not action in nonunique_actions:
			msgs = [ m for m in self.throttled_msgs if m['action'] == action ]
			msg = msgs.pop()
			for m in msgs:
				if "message_id" in m and fieldtypes.zero_or_greater_integer(m['message_id']):
					self.write_message({
						"wsthrottle": { "tl_key": "websocket_throttle", "text": self.locale.translate("websocket_throttle") },
						"message_id": { "message_id": fieldtypes.zero_or_greater_integer(m['message_id']), "success": False, "tl_key": "websocket_throttle" }
					})
			self.throttled_msgs = [ m for m in self.throttled_msgs if m['action'] != action ]
			# log.debug("throttle", "Handling last throttled %s message." % action)
		else:
			msg = self.throttled_msgs.pop(0)
			# log.debug("throttle", "Handling last throttled %s message." % action)
		if msg:
			self._process_message(msg, is_throttle_process=True)
		tornado.ioloop.IOLoop.instance().add_timeout(datetime.timedelta(seconds=0.5), self.process_throttle)
コード例 #2
0
	def process_throttle(self):
		if not len(self.throttled_msgs):
			self.throttled = False
			return
		self.throttled_msgs.sort()
		# log.debug("throttle", "Throttled with %s messages" % len(self.throttled_msgs))
		action = self.throttled_msgs[0]['action']
		msg = None
		if not action in nonunique_actions:
			msgs = [ m for m in self.throttled_msgs if m['action'] == action ]
			msg = msgs.pop()
			for m in msgs:
				if "message_id" in m and fieldtypes.zero_or_greater_integer(m['message_id']):
					self.write_message({
						"wsthrottle": { "tl_key": "websocket_throttle", "text": self.locale.translate("websocket_throttle") },
						"message_id": { "message_id": fieldtypes.zero_or_greater_integer(m['message_id']), "success": False, "tl_key": "websocket_throttle" }
					})
			self.throttled_msgs = [ m for m in self.throttled_msgs if m['action'] != action ]
			# log.debug("throttle", "Handling last throttled %s message." % action)
		else:
			msg = self.throttled_msgs.pop(0)
			# log.debug("throttle", "Handling last throttled %s message." % action)
		if msg:
			self._process_message(msg, is_throttle_process=True)
		tornado.ioloop.IOLoop.instance().add_timeout(datetime.timedelta(seconds=0.5), self.process_throttle)
コード例 #3
0
ファイル: sync.py プロジェクト: Abchrisabc/rainwave
	def _process_message(self, message, is_throttle_process=False):
		message_id = None
		if "message_id" in message:
			message_id = fieldtypes.zero_or_greater_integer(message['message_id'])

		throt_t = timestamp() - 3
		self.msg_times = [ t for t in self.msg_times if t > throt_t ]

		if not is_throttle_process:
			self.msg_times.append(timestamp())
			# log.debug("throttle", "%s - %s" % (len(self.msg_times), message['action']))
			if self.throttled:
				# log.debug("throttle", "Currently throttled, adding to queue.")
				self.throttled_msgs.append(message)
				return
			elif len(self.msg_times) >= 5:
				# log.debug("throttle", "Too many messages, throttling.")
				self.throttled = True
				self.throttled_msgs.append(message)
				tornado.ioloop.IOLoop.instance().add_timeout(datetime.timedelta(seconds=0.5), self.process_throttle)
				return

		if message['action'] == "ping":
			self.write_message({ "pong": { "timestamp": timestamp() } })
			return

		if message['action'] == "pong":
			self.write_message({ "pongConfirm": { "timestamp": timestamp() } })
			return

		if message['action'] == "vote":
			zeromq.publish({ "action": "vote_by", "by": self.votes_by_key })

		if message['action'] == "check_sched_current_id":
			self._do_sched_check(message)
			return

		message['action'] = "/api4/%s" % message['action']
		if not message['action'] in api_endpoints:
			self.write_message({ "wserror": { "tl_key": "websocket_404", "text": self.locale.translate("websocket_404") } })
			return

		endpoint = api_endpoints[message['action']](websocket=True)
		endpoint.locale = self.locale
		endpoint.request = FakeRequestObject(message, self.request.cookies)
		endpoint.sid = message['sid'] if ('sid' in message and message['sid']) else self.sid
		endpoint.user = self.user
		#pylint: disable=W0212
		try:
			startclock = timestamp()
			# TODO: this should be a part of prepare_standalone!
			# it's required to see if another person on the same IP address has overriden the vote
			# for the in-memory user here, so it requires a DB fetch.
			if message['action'] == "/api4/vote" and self.user.is_anonymous():
				self.user.refresh(self.sid)
			if "message_id" in message:
				if message_id == None:
					endpoint.prepare_standalone()
					raise APIException("invalid_argument", argument="message_id", reason=fieldtypes.zero_or_greater_integer_error, http_code=400)
				endpoint.prepare_standalone(message_id)
			else:
				endpoint.prepare_standalone()
			endpoint.post()
			endpoint.append("api_info", { "exectime": timestamp() - startclock, "time": round(timestamp()) })
			if endpoint.sync_across_sessions:
				if endpoint.return_name in endpoint._output and isinstance(endpoint._output[endpoint.return_name], dict) and not endpoint._output[endpoint.return_name]['success']:
					pass
				else:
					zeromq.publish({ "action": "result_sync", "sid": self.sid, "user_id": self.user.id, "data": endpoint._output, "uuid_exclusion": self.uuid })
			if message['action'] == "/api4/vote" and endpoint.return_name in endpoint._output and isinstance(endpoint._output[endpoint.return_name], dict) and endpoint._output[endpoint.return_name]['success']:
				live_voting = rainwave.schedule.update_live_voting(self.sid)
				endpoint.append("live_voting", live_voting)
				if self.should_vote_throttle():
					zeromq.publish({ "action": "delayed_live_voting", "sid": self.sid, "uuid_exclusion": self.uuid, "data": { "live_voting": live_voting } })
				else:
					zeromq.publish({ "action": "live_voting", "sid": self.sid, "uuid_exclusion": self.uuid, "data": { "live_voting": live_voting } })
		except Exception as e:
			endpoint.write_error(500, exc_info=sys.exc_info(), no_finish=True)
			log.exception("websocket", "API Exception during operation.", e)
		finally:
			self.write_message(endpoint._output)
コード例 #4
0
	def _process_message(self, message, is_throttle_process=False):
		message_id = None
		if "message_id" in message:
			message_id = fieldtypes.zero_or_greater_integer(message['message_id'])

		throt_t = timestamp() - 3
		self.msg_times = [ t for t in self.msg_times if t > throt_t ]

		if not is_throttle_process:
			self.msg_times.append(timestamp())
			# log.debug("throttle", "%s - %s" % (len(self.msg_times), message['action']))
			if self.throttled:
				# log.debug("throttle", "Currently throttled, adding to queue.")
				self.throttled_msgs.append(message)
				return
			elif len(self.msg_times) >= 5:
				# log.debug("throttle", "Too many messages, throttling.")
				self.throttled = True
				self.throttled_msgs.append(message)
				tornado.ioloop.IOLoop.instance().add_timeout(datetime.timedelta(seconds=0.5), self.process_throttle)
				return

		if message['action'] == "ping":
			self.write_message({ "pong": { "timestamp": timestamp() } })
			return

		if message['action'] == "pong":
			self.write_message({ "pongConfirm": { "timestamp": timestamp() } })
			return

		if message['action'] == "vote":
			zeromq.publish({ "action": "vote_by", "by": self.votes_by_key })

		if message['action'] == "check_sched_current_id":
			self._do_sched_check(message)
			return

		message['action'] = "/api4/%s" % message['action']
		if not message['action'] in api_endpoints:
			self.write_message({ "wserror": { "tl_key": "websocket_404", "text": self.locale.translate("websocket_404") } })
			return

		endpoint = api_endpoints[message['action']](websocket=True)
		endpoint.locale = self.locale
		endpoint.request = FakeRequestObject(message, self.request.cookies)
		endpoint.sid = message['sid'] if ('sid' in message and message['sid']) else self.sid
		endpoint.user = self.user
		#pylint: disable=W0212
		try:
			startclock = timestamp()
			# TODO: this should be a part of prepare_standalone!
			# it's required to see if another person on the same IP address has overriden the vote
			# for the in-memory user here, so it requires a DB fetch.
			if message['action'] == "/api4/vote" and self.user.is_anonymous():
				self.user.refresh(self.sid)
			if "message_id" in message:
				if message_id == None:
					endpoint.prepare_standalone()
					raise APIException("invalid_argument", argument="message_id", reason=fieldtypes.zero_or_greater_integer_error, http_code=400)
				endpoint.prepare_standalone(message_id)
			else:
				endpoint.prepare_standalone()
			endpoint.post()
			endpoint.append("api_info", { "exectime": timestamp() - startclock, "time": round(timestamp()) })
			if endpoint.sync_across_sessions:
				if endpoint.return_name in endpoint._output and isinstance(endpoint._output[endpoint.return_name], dict) and not endpoint._output[endpoint.return_name]['success']:
					pass
				else:
					zeromq.publish({ "action": "result_sync", "sid": self.sid, "user_id": self.user.id, "data": endpoint._output, "uuid_exclusion": self.uuid })
			if message['action'] == "/api4/vote" and endpoint.return_name in endpoint._output and isinstance(endpoint._output[endpoint.return_name], dict) and endpoint._output[endpoint.return_name]['success']:
				live_voting = rainwave.schedule.update_live_voting(self.sid)
				endpoint.append("live_voting", live_voting)
				if self.should_vote_throttle():
					zeromq.publish({ "action": "delayed_live_voting", "sid": self.sid, "uuid_exclusion": self.uuid, "data": { "live_voting": live_voting } })
				else:
					zeromq.publish({ "action": "live_voting", "sid": self.sid, "uuid_exclusion": self.uuid, "data": { "live_voting": live_voting } })
		except Exception as e:
			endpoint.write_error(500, exc_info=sys.exc_info(), no_finish=True)
			log.exception("websocket", "API Exception during operation.", e)
		finally:
			self.write_message(endpoint._output)