def __init__(self, queue, bidder_id=None, client_id=None, timeout=None): self.queue = queue self.client_id = client_id self.bidder_id = bidder_id if timeout: self.sse = PySse(default_retry=0) spawn(sse_timeout, queue, timeout) else: self.sse = PySse(default_retry=2000)
def auctions_proxy(auction_doc_id, path): auctions_server.logger.debug('Auction_doc_id: {}'.format(auction_doc_id)) proxy_path = auctions_server.proxy_mappings.get(str(auction_doc_id), auctions_server.redis.get, (str(auction_doc_id), ), max_age=60) auctions_server.logger.debug('Proxy path: {}'.format(proxy_path)) if proxy_path: request.environ['PATH_INFO'] = '/' + path auctions_server.logger.debug('Start proxy to path: {}'.format(path)) return StreamProxy( proxy_path, auction_doc_id=str(auction_doc_id), event_sources_pool=auctions_server.event_sources_pool, event_source_connection_limit=auctions_server. config['event_source_connection_limit'], pool=auctions_server.proxy_connection_pool, backend="gevent") elif path == 'login' and auction_doc_id in auctions_server.db: return redirect((url_for('auction_url', auction_doc_id=auction_doc_id, wait=1, **request.args))) elif path == 'event_source': events_close = PySse() events_close.add_message("Close", "Disable") return Response(events_close, mimetype='text/event-stream', content_type='text/event-stream') return abort(404)
def auctions_proxy(auction_doc_id, path): auctions_server.logger.debug('Auction_doc_id: {}'.format(auction_doc_id)) proxy_path = auctions_server.proxy_mappings.get( str(auction_doc_id), get_mapping, (auctions_server.config['REDIS'], str(auction_doc_id), False), max_age=60) auctions_server.logger.debug('Proxy path: {}'.format(proxy_path)) if proxy_path: request.environ['PATH_INFO'] = '/' + path auctions_server.logger.debug('Start proxy to path: {}'.format(path)) return StreamProxy( proxy_path, auction_doc_id=str(auction_doc_id), event_sources_pool=auctions_server.event_sources_pool, event_source_connection_limit=auctions_server. config['event_source_connection_limit'], pool=auctions_server.proxy_connection_pool, backend='gevent') elif path == 'login' and auction_doc_id in auctions_server.db: if 'X-Forwarded-For' in request.headers: url = urlunparse( urlparse(request.url)._replace( netloc=request.headers['Host'])).replace('/login', '') auctions_server.logger.info( 'Redirecting loging path to {}'.format(url)) return redirect(url) elif path == 'event_source': events_close = PySse() events_close.add_message('Close', 'Disable') return Response(events_close, mimetype='text/event-stream', content_type='text/event-stream') return abort(404)
def __iter__(self): sse = PySse() for data in sse: yield str(data) for message in self.pubsub.listen(): if message['type'] == 'message': event, data = json.loads(message['data']) sse.add_message(event, data) for data in sse: yield str(data)
def __iter__(self): sse = PySse() for data in sse: yield data.encode('u8') for message in self.pubsub.listen(): if message['type'] == 'message': if message['data'] != '_flush': event, data = json.loads(message['data']) sse.add_message(event, data) for data in sse: yield data.encode('u8') else: yield ":\n".encode('u8')
def __iter__(self): self.sse = PySse() # TODO: https://app.asana.com/0/17412748309135/22939294056733 yield CHUNK for data in self.sse: yield data.encode('u8') while True: message = self.queue.get() if message["event"] == "StopSSE": return LOGGER.debug(' '.join([ 'Event Message to bidder:', str(self.bidder_id), ' Client:', str(self.client_id), 'MSG:', str(repr(message)) ])) self.sse.add_message(message['event'], json.dumps(message['data'])) for data in self.sse: yield data.encode('u8')
def event_source(): current_app.logger.debug( 'Handle event_source request with session {}'.format( repr(dict(session))), extra=prepare_extra_journal_fields(request.headers)) if 'remote_oauth' in session and 'client_id' in session: bidder_data = get_bidder_id(current_app, session) if bidder_data: valid_bidder = False client_hash = session['client_id'] bidder = bidder_data['bidder_id'] for bidder_info in current_app.config['auction'].bidders_data: if bidder_info['id'] == bidder: valid_bidder = True break if current_app.config['auction'].auction_document.get( 'current_phase', '') in ['dutch', 'pre-started', 'pre-sealedbid']: valid_bidder = True if valid_bidder: if bidder not in current_app.auction_bidders: current_app.auction_bidders[bidder] = { "clients": {}, "channels": {} } if client_hash not in current_app.auction_bidders[bidder]: real_ip = request.environ.get('HTTP_X_REAL_IP', '') if real_ip.startswith('172.'): real_ip = '' current_app.auction_bidders[bidder]["clients"][ client_hash] = { 'ip': ','.join([ request.headers.get('X-Forwarded-For', ''), real_ip ]), 'User-Agent': request.headers.get('User-Agent'), } current_app.auction_bidders[bidder]["channels"][ client_hash] = Queue() current_app.logger.info( 'Send identification for bidder: {} with client_hash {}'. format(bidder, client_hash), extra=prepare_extra_journal_fields(request.headers)) identification_data = { "bidder_id": bidder, "client_id": client_hash, "return_url": session.get('return_url', '') } if current_app.config['auction'].features: identification_data["coeficient"] = str( current_app.config['auction']. bidders_coeficient[bidder]) send_event_to_client(bidder, client_hash, identification_data, "Identification") if 'amount' in session: send_event_to_client(bidder, client_hash, {"last_amount": session['amount']}, "RestoreBidAmount") current_app.logger.debug('Send RestoreBidAmount') del session['amount'] if not session.get("sse_timeout", 0): current_app.logger.debug('Send ClientsList') send_event(bidder, current_app.auction_bidders[bidder]["clients"], "ClientsList") response = Response( SseStream(current_app.auction_bidders[bidder]["channels"] [client_hash], bidder_id=bidder, client_id=client_hash, timeout=session.get("sse_timeout", 0)), direct_passthrough=True, mimetype='text/event-stream', content_type='text/event-stream') response.headers['Cache-Control'] = 'no-cache' response.headers['X-Accel-Buffering'] = 'no' return response else: current_app.logger.info( 'Not valid bidder: bidder_id {} with client_hash {}'. format(bidder, client_hash), extra=prepare_extra_journal_fields(request.headers)) current_app.logger.debug('Disable event_source for unauthorized user.', extra=prepare_extra_journal_fields( request.headers)) events_close = PySse() events_close.add_message("Close", "Disable") response = Response(iter( [bytearray(''.join([x for x in events_close]), 'UTF-8')]), direct_passthrough=True, mimetype='text/event-stream', content_type='text/event-stream') response.headers['Cache-Control'] = 'no-cache' response.headers['X-Accel-Buffering'] = 'no' return response