From 9d266a5cb3d1f4d0251e903b7a6f79079774777f Mon Sep 17 00:00:00 2001

From: =?UTF-8?q?Jaakko=20Kera=CC=88nen?= jaakko.keranen@iki.fi

Date: Fri, 16 Jun 2023 13:17:14 +0300

Subject: [PATCH 1/1] Switch back to regular threading; fixed refactoring

issues


gmcapsule/gemini.py | 93 ++++++++++++++++++++++-----------------------

1 file changed, 46 insertions(+), 47 deletions(-)

diff --git a/gmcapsule/gemini.py b/gmcapsule/gemini.py

index dcb17d9..ecddbeb 100644

--- a/gmcapsule/gemini.py

+++ b/gmcapsule/gemini.py

@@ -7,7 +7,9 @@ import importlib

import os.path

import select

import socket

-import multiprocessing as mp

+#import multiprocessing as mp

+import threading

+import queue

import re

import time

from pathlib import Path

@@ -303,12 +305,21 @@ class WorkerContext:

     self.caches = []

     self.is_quiet = False

 def config(self):

     return self.cfg

 def print(self, *args):

     if not self.is_quiet:

         print(*args)

@@ -396,14 +407,24 @@ class WorkerContext:

         self.print(f'Init:', mod.__doc__ if mod.__doc__ else name)

         mod.init(self)

 def call_entrypoint(self, request):

     """

@@ -448,11 +469,12 @@ class WorkerContext:

     raise GeminiError(50, 'Permanent failure')

-class Worker(mp.Process):

+class Worker(threading.Thread):

 def __init__(self, id, cfg, work_queue, shutdown_event):

     self.id = id

     self.cfg = cfg

     self.port = cfg.port()

@@ -460,7 +482,7 @@ class Worker(mp.Process):

     self.context.set_quiet(id > 0)

     self.jobs = work_queue

     try:

         # Extensions are initialized in the worker process.

         self.context.load_modules()

@@ -624,7 +646,7 @@ class Worker(mp.Process):

         # Save to cache.

         if not from_cache and status == 20 and \

                 (type(response_data) == bytes or type(response_data) == bytearray):

                 if cache.save(hostname + path, meta, response_data):

                     break

@@ -639,7 +661,7 @@ class Worker(mp.Process):

class Server:

 def __init__(self, cfg):

     hostname_or_hostnames = cfg.hostnames()

     cert_path = cfg.certs_dir() / 'cert.pem'

@@ -653,10 +675,6 @@ class Server:

         if type(hostname_or_hostnames) == str else hostname_or_hostnames

     self.address = address

     self.port = port

     if not os.path.exists(cert_path):

         raise Exception("certificate file not found: " + str(cert_path))

@@ -673,9 +691,9 @@ class Server:

         self.context.set_session_id(session_id)

     # Spawn the worker threads.

     self.workers = []

     for worker_id in range(max(num_threads, 1)):

         worker = Worker(worker_id, cfg, self.work_queue, self.shutdown_event)

         self.workers.append(worker)

@@ -702,9 +720,9 @@ class Server:

             print('...')

     print(f'Server started on port {self.port}')

         for worker in self.workers:

             worker.start()

         print(len(self.workers), 'worker(s) started')

@@ -718,7 +736,7 @@ class Server:

             stream._socket.settimeout(10)

             self.work_queue.put((stream, from_addr))

                 self.work_queue.put((None, None)) # single iteration only

                 self.workers[0].run()

@@ -739,29 +757,10 @@ class Server:

     # Stop all workers.

     self.shutdown_event.set()

         for i in range(len(self.workers)):

             self.work_queue.put((None, None))

         for worker in self.workers:

             worker.join()

     print('Done')

--

2.25.1

Proxy Information
Original URL
gemini://git.skyjake.fi/gmcapsule/gsorg-style/patch/9d266a5cb3d1f4d0251e903b7a6f79079774777f.patch
Status Code
Success (20)
Meta
text/plain
Capsule Response Time
26.723238 milliseconds
Gemini-to-HTML Time
1.590912 milliseconds

This content has been proxied by September (ba2dc).