mirror of
https://github.com/zulip/zulip.git
synced 2025-11-13 10:26:28 +00:00
Fix Zephyr mirroring loop detection with new events API.
(imported from commit 79454bb26851b2f4a61d6cdf04350386671618a5)
This commit is contained in:
@@ -16,6 +16,7 @@ import tornado
|
|||||||
import random
|
import random
|
||||||
import zephyr.lib.stats as stats
|
import zephyr.lib.stats as stats
|
||||||
from zephyr.middleware import async_request_restart
|
from zephyr.middleware import async_request_restart
|
||||||
|
from zephyr.models import get_client
|
||||||
|
|
||||||
# The idle timeout used to be a week, but we found that in that
|
# The idle timeout used to be a week, but we found that in that
|
||||||
# situation, queues from dead browser sessions would grow quite large
|
# situation, queues from dead browser sessions would grow quite large
|
||||||
@@ -30,13 +31,15 @@ EVENT_QUEUE_GC_FREQ_MSECS = 1000 * 60 * 5
|
|||||||
HEARTBEAT_MIN_FREQ_SECS = 45
|
HEARTBEAT_MIN_FREQ_SECS = 45
|
||||||
|
|
||||||
class ClientDescriptor(object):
|
class ClientDescriptor(object):
|
||||||
def __init__(self, user_profile_id, id, event_types=None, apply_markdown=True):
|
def __init__(self, user_profile_id, id, event_types, client_type,
|
||||||
|
apply_markdown=True):
|
||||||
self.user_profile_id = user_profile_id
|
self.user_profile_id = user_profile_id
|
||||||
self.current_handler = None
|
self.current_handler = None
|
||||||
self.event_queue = EventQueue(id)
|
self.event_queue = EventQueue(id)
|
||||||
self.event_types = event_types
|
self.event_types = event_types
|
||||||
self.last_connection_time = time.time()
|
self.last_connection_time = time.time()
|
||||||
self.apply_markdown = apply_markdown
|
self.apply_markdown = apply_markdown
|
||||||
|
self.client_type = client_type
|
||||||
self._timeout_handle = None
|
self._timeout_handle = None
|
||||||
|
|
||||||
def prepare_for_pickling(self):
|
def prepare_for_pickling(self):
|
||||||
@@ -123,11 +126,13 @@ def get_client_descriptor(queue_id):
|
|||||||
def get_client_descriptors_for_user(user_profile_id):
|
def get_client_descriptors_for_user(user_profile_id):
|
||||||
return user_clients.get(user_profile_id, [])
|
return user_clients.get(user_profile_id, [])
|
||||||
|
|
||||||
def allocate_client_descriptor(user_profile_id, event_types, apply_markdown):
|
def allocate_client_descriptor(user_profile_id, event_types, client_type,
|
||||||
|
apply_markdown):
|
||||||
global next_queue_id
|
global next_queue_id
|
||||||
id = str(settings.SERVER_GENERATION) + ':' + str(next_queue_id)
|
id = str(settings.SERVER_GENERATION) + ':' + str(next_queue_id)
|
||||||
next_queue_id += 1
|
next_queue_id += 1
|
||||||
client = ClientDescriptor(user_profile_id, id, event_types, apply_markdown)
|
client = ClientDescriptor(user_profile_id, id, event_types, client_type,
|
||||||
|
apply_markdown)
|
||||||
clients[id] = client
|
clients[id] = client
|
||||||
user_clients.setdefault(user_profile_id, []).append(client)
|
user_clients.setdefault(user_profile_id, []).append(client)
|
||||||
return client
|
return client
|
||||||
@@ -181,6 +186,10 @@ def load_event_queues():
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
for client in clients.itervalues():
|
for client in clients.itervalues():
|
||||||
|
# The following client_type block can be dropped once we've
|
||||||
|
# cleared out all our old event queues
|
||||||
|
if not hasattr(client, 'client_type'):
|
||||||
|
client.client_type = get_client("website")
|
||||||
user_clients.setdefault(client.user_profile_id, []).append(client)
|
user_clients.setdefault(client.user_profile_id, []).append(client)
|
||||||
|
|
||||||
logging.info('Tornado loaded %d event queues in %.3fs'
|
logging.info('Tornado loaded %d event queues in %.3fs'
|
||||||
|
|||||||
@@ -256,7 +256,10 @@ def process_new_message(data):
|
|||||||
user_receive_message(user_profile_id, message)
|
user_receive_message(user_profile_id, message)
|
||||||
|
|
||||||
for client in get_client_descriptors_for_user(user_profile_id):
|
for client in get_client_descriptors_for_user(user_profile_id):
|
||||||
if client.accepts_event_type('message'):
|
# The below prevents (Zephyr) mirroring loops.
|
||||||
|
if client.accepts_event_type('message') and not \
|
||||||
|
('mirror' in message.sending_client.name and
|
||||||
|
message.sending_client == client.client_type):
|
||||||
if client.apply_markdown:
|
if client.apply_markdown:
|
||||||
message_dict = message_dict_markdown
|
message_dict = message_dict_markdown
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -218,7 +218,7 @@ def get_events_backend(request, user_profile, handler,
|
|||||||
if queue_id is None:
|
if queue_id is None:
|
||||||
if dont_block:
|
if dont_block:
|
||||||
client = allocate_client_descriptor(user_profile.id, event_types,
|
client = allocate_client_descriptor(user_profile.id, event_types,
|
||||||
apply_markdown)
|
request.client, apply_markdown)
|
||||||
queue_id = client.event_queue.id
|
queue_id = client.event_queue.id
|
||||||
else:
|
else:
|
||||||
return json_error("Missing 'queue_id' argument")
|
return json_error("Missing 'queue_id' argument")
|
||||||
|
|||||||
Reference in New Issue
Block a user