summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Xu (Hello71) <alex_y_xu@yahoo.ca>2020-03-07 12:10:38 -0500
committerAlex Xu (Hello71) <alex_y_xu@yahoo.ca>2020-03-07 12:10:38 -0500
commit0e2d936d09ca6d4e75286a9ebaf16363d179f9fa (patch)
tree9a88e15ddf7138b4fc1d3a478c7f47ab6c56a153
parent8d1d15c0cbedce68488c2f9cade3f50ba0eefc32 (diff)
downloadcgit-syntax-highlighting-0e2d936d09ca6d4e75286a9ebaf16363d179f9fa.tar.xz
cgit-syntax-highlighting-0e2d936d09ca6d4e75286a9ebaf16363d179f9fa.zip
fix setting style, set request_queue_size=64
-rwxr-xr-xsyntax-highlighting-server.py24
1 files changed, 13 insertions, 11 deletions
diff --git a/syntax-highlighting-server.py b/syntax-highlighting-server.py
index e093a9a..5aad034 100755
--- a/syntax-highlighting-server.py
+++ b/syntax-highlighting-server.py
@@ -26,15 +26,17 @@ from pygments.lexers import LEXERS, _load_lexers, guess_lexer, guess_lexer_for_f
from pygments.lexers.special import TextLexer
from pygments.util import ClassNotFound
-class BaseHTTPServer(HTTPServer):
+class HighlightingHTTPServer(HTTPServer):
# set SO_REUSEADDR
allow_reuse_address = True
# socketserver defaults to 5. especially in listen mode 'single', a
# sudden surge can easily overwhelm that.
- request_queue_size = 128
+ request_queue_size = 64
-formatter = HtmlFormatter(style='pastie', nobackground=True, encoding='utf-8')
-style_defs = ('<style>' + formatter.get_style_defs('.highlight') + '</style>').encode('utf-8')
+ def __init__(self, *args, style='pastie', **kwargs):
+ super().__init__(*args, **kwargs)
+ self.formatter = HtmlFormatter(style=style, nobackground=True, encoding='utf-8')
+ self.style_defs = ('<style>' + self.formatter.get_style_defs('.highlight') + '</style>').encode('utf-8')
class HighlightingHandler(BaseHTTPRequestHandler):
# read by BaseHTTPRequestHandler. need this so that curl doesn't delay
@@ -70,8 +72,8 @@ class HighlightingHandler(BaseHTTPRequestHandler):
lexer = guess_lexer(data)
except ClassNotFound:
lexer = TextLexer()
- self.wfile.write(style_defs)
- highlight(data, lexer, formatter, outfile=self.wfile)
+ self.wfile.write(self.server.style_defs)
+ highlight(data, lexer, self.server.formatter, outfile=self.wfile)
def main():
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO)
@@ -121,17 +123,17 @@ def main():
__import__('pygments.styles.' + args.style)
def start_server(MyHTTPServer):
- with MyHTTPServer((args.host, args.port), HighlightingHandler) as server:
+ with MyHTTPServer((args.host, args.port), HighlightingHandler, style=args.style) as server:
logging.info('started syntax-highlighting-server')
server.serve_forever()
if args.listen_mode == 'single':
- start_server(BaseHTTPServer)
+ start_server(HighlightingHTTPServer)
elif args.listen_mode == 'forking':
# note: Threading isn't useful for performance because of the GIL
from socketserver import ForkingMixIn
- class ForkingHTTPServer(ForkingMixIn, BaseHTTPServer):
+ class ForkingHTTPServer(ForkingMixIn, HighlightingHTTPServer):
pass
start_server(ForkingHTTPServer)
@@ -142,7 +144,7 @@ def main():
if not hasattr(socket, 'SO_REUSEPORT'):
raise Exception('SO_REUSEPORT not available on this platform')
- class ReusePortHTTPServer(BaseHTTPServer):
+ class ReusePortHTTPServer(HighlightingHTTPServer):
def server_bind(self):
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
super().server_bind()
@@ -161,7 +163,7 @@ def main():
pid = os.fork()
if pid == 0:
os.close(pipe[1])
- with ReusePortHTTPServer((args.host, args.port), HighlightingHandler) as server:
+ with ReusePortHTTPServer((args.host, args.port), HighlightingHandler, style=args.style) as server:
with selectors.DefaultSelector() as selector:
selector.register(server, selectors.EVENT_READ)
selector.register(pipe[0], selectors.EVENT_READ)