Commit Graph

67 Commits

Author SHA1 Message Date
Joris Vink b07cc0237c Support recvfrom()/sendto() on kore python sockets. 2019-01-08 17:49:00 +01:00
Joris Vink ce3b60b8c8 keep a reference around for tracer. 2018-11-29 09:53:26 +01:00
Joris Vink d9f543ef5b Allow user-supplied tracer callback. 2018-11-29 09:51:24 +01:00
Joris Vink b400fdcd9f include sys/param.h in python.c 2018-11-29 09:11:31 +01:00
Joris Vink c431c2bf72 Add support to obtain peer certificate from Python
This will return the DER encoded bytes representing the peer certificate.
2018-11-28 11:28:07 +01:00
Joris Vink 5c8efde841 allow foo.method symbols in python.
this way you can create page handlers that reside inside
of other objects.

eg:

static / restapi.index
2018-11-16 11:07:21 +01:00
Joris Vink cb482d8e8f Always use http_response_stream() in req.response.
Keep around the python bytes object while we stream the contents
of it out over the wire. This avoids an entire copy of the data.
2018-11-14 10:03:32 +01:00
Joris Vink 3925ba60ad don't call close() no underlying socket. 2018-11-12 09:00:36 +01:00
Joris Vink 4cc9e216bb Don't call close() on the python socket on dealloc 2018-11-06 16:54:26 +01:00
Joris Vink 2dd66586ff several python improvements.
- add kore.time() as equivalent for kore_time_ms().
- call waitpid() until no more children are available for reaping otherwise
  we risk missing a process if several die at the same time and only one
  SIGCHLD is delivered to us.
- drain a RECV socket operation if eof is set but no exception was given.
2018-10-30 20:28:27 +01:00
Joris Vink 5456f2e1d5 save/restore coro_running in pygather_op_dealloc(). 2018-10-30 12:37:30 +01:00
Joris Vink bb00deca2c don't leak op in error path 2018-10-29 21:18:38 +01:00
Joris Vink 1c30da855c Add kore.gather() to the python api.
Allows one to run coroutines concurrently and gather all their
results in a single returned list.

If any of the coroutines throw an exception the exception is
returned as the value of that coroutine in the returned list.
2018-10-29 21:16:08 +01:00
Joris Vink e2651889e0 Add asynchronous subprocess support.
This adds kore.proc to the python runtime allowing async processing
handling:

The kore.proc method takes the command to run and an optional timeout
parameter in milliseconds. If the process did not exit normally after
that amount of time a TimeoutError exception is raised.

For instance:

async def run(cmd):
	proc = kore.proc(cmd, 1000)

	try:
		await proc.send("hello")
		proc.close_stdin()
	except TimeoutError:
		proc.kill()

	retcode = await proc.reap()

	return retcode
2018-10-26 19:19:47 +02:00
Joris Vink fc5fc4f4ab make sure timers don't go out of scope. 2018-10-23 21:36:19 +02:00
Joris Vink 8ea32983ae Add kore.suspend(delay) to python.
Will suspend the coroutine for a number of milliseconds.

Example:

async def page(req):
	await kore.suspend(1000)
	req.response(200, b'')
2018-10-23 21:32:08 +02:00
Joris Vink 47776a9fbb Hook kore timers into python. 2018-10-23 20:44:43 +02:00
Joris Vink c41c1db303 Add kore_shutdown().
Allows workers to cleanly initiate a shutdown of the
entire server process.
2018-10-23 19:49:42 +02:00
Joris Vink b70d1ee80f Add a locking mechanism in pykore.
Support the async with syntax:

	lock = kore.lock()

	async with lock:
		# your block

Fix some small issues with other parts of the python system.
2018-10-22 20:09:23 +02:00
Joris Vink fad5c6ea6f Give pyqueues "popnow". 2018-10-22 08:28:03 +02:00
Joris Vink c8795b7d7f pyqueue improvements.
- cleanup queue if it gets deallocated.
- make sure waitables on a queue get removed if their pyqueue_op dies.
2018-10-21 21:58:34 +02:00
Joris Vink 4ae3d23c7e s/INCREF/DECREF 2018-10-18 22:24:58 +02:00
Joris Vink 0cda9ecfb0 Add an asynchronous queue mechanism.
This allows coroutines to submit messages to and pop
messages from a queue in an asynchronous way.
2018-10-18 22:15:21 +02:00
Joris Vink 300daeadef fix comment 2018-10-16 13:17:44 +02:00
Joris Vink 29202d7330 Make kore_python_log_error() public.
While here also make kore_module_load() return the
kore_module data structure pointer it just added
to the modules list.
2018-10-16 13:16:36 +02:00
Joris Vink 20a0103f1e Add async/await support for socket i/o in python.
This means you can now do things like:

	resp = await koresock.recv(1024)
	await koresock.send(resp)

directly from page handlers if they are defined as async.

Adds lots more to the python goo such as fatalx(), bind_unix(),
task_create() and socket_wrap().
2018-10-15 20:18:54 +02:00
Joris Vink 2449a86085 missing addrtype -> family renames 2018-10-08 20:12:25 +02:00
Joris Vink 71659ab197 correct includes 2018-07-07 13:23:43 +02:00
Joris Vink 92bd546935 Remove unused argument. 2018-04-13 16:04:33 +02:00
Joris Vink f7678946a1 don't set our own exception on invalid parameters. 2018-04-11 13:32:56 +02:00
Joris Vink 4ab028633a If a python handler failed, don't fatal on purpose.
Instead log and send an internal error status back to the client.

This should be OK as long as the exception doesn't happen after
the caller called req.response() already.
2018-04-11 13:00:30 +02:00
Joris Vink 43a0aef29f prefix HTTP defines when exporting them to python. 2018-04-10 14:35:52 +02:00
Joris Vink 9c337ded1e Change kore_parent_configure() for single binaries.
This function now takes any remaining arguments passed on the command line
after kore parsed its own.

For C the new prototype looks like this:

void kore_parent_configure(int argc, char **argv);

For python code, kore will pass each argument to the function so you
can do things like:

def kore_parent_configure(arg1, arg2):
2018-04-09 12:51:20 +02:00
Joris Vink ff21fd330f Add python_path configuration option.
Allows adding more items to the python path so imports from inside
python scripts work more sensible depending on how you configure your
app.
2018-03-01 12:55:33 +01:00
Joris Vink dd2dff2318 Rework HTTP and worker processes.
The HTTP layer used to make a copy of each incoming header and its
value for a request. Stop doing that and make HTTP headers zero-copy
all across the board.

This change comes with some api function changes, notably the
http_request_header() function which now takes a const char ** rather
than a char ** out pointer.

This commit also constifies several members of http_request, beware.

Additional rework how the worker processes deal with the accept lock.

Before:
	if a worker held the accept lock and it accepted a new connection
	it would release the lock for others and back off for 500ms before
	attempting to grab the lock again.

	This approach worked but under high load this starts becoming obvious.

Now:
	- workers not holding the accept lock and not having any connections
	  will wait less long before returning from kore_platform_event_wait().

	- workers not holding the accept lock will no longer blindly wait
	  an arbitrary amount in kore_platform_event_wait() but will look
	  at how long until the next lock grab is and base their timeout
	  on that.

	- if a worker its next_lock timeout is up and failed to grab the
	  lock it will try again in half the time again.

	- the worker process holding the lock will when releasing the lock
	  double check if it still has space for newer connections, if it does
	  it will keep the lock until it is full. This prevents the lock from
	  bouncing between several non busy worker processes all the time.

Additional fixes:

- Reduce the number of times we check the timeout list, only do it twice
  per second rather then every event tick.
- Fix solo worker count for TLS (we actually hold two processes, not one).
- Make sure we don't accidentally miscalculate the idle time causing new
  connections under heavy load to instantly drop.
- Swap from gettimeofday() to clock_gettime() now that MacOS caught up.
2018-02-14 13:48:49 +01:00
Joris Vink 548348f553 2018 2018-01-20 22:51:06 +01:00
rouzier f0f1296265 Add patch support (#217)
Add PATCH to supported verbs in config and what not.
2018-01-02 22:27:59 +01:00
Stanislav Yudin b73343aea4 add HTTP_METHOD_OPTIONS as another supported http method. (#186) 2017-04-04 09:37:19 +02:00
Joris Vink 59f7e85f45 Decouple pgsql from the http layer.
When the pgsql layer was introduced it was tightly coupled with the
http layer in order to make async work fluently.

The time has come to split these up and follow the same method we
used for tasks, allowing either http requests to be tied to a pgsql
data structure or a simple callback function.

This also reworks the internal queueing of pgsql requests until
connections to the db are available again.

The following API functions were changes:
	- kore_pgsql_query_init() -> kore_pgsql_setup()
		no longer takes an http_request parameter.
	- NEW kore_pgsql_init()
		must be called before operating on an kore_pgsql structure.
	- NEW kore_pgsql_bind_request()
		binds an http_request to a kore_pgsql data structure.
	- NEW kore_pgsql_bind_callback()
		binds a callback to a kore_pgsql data structure.

With all of this you can now build kore with PGSQL=1 NOHTTP=1.

The pgsql/ example has been updated to reflect these changes and
new features.
2017-03-24 12:53:07 +01:00
Joris Vink add6d724e3 expose connection address to python. 2017-03-24 12:42:40 +01:00
Joris Vink ffbabed7d6 Move websocket_send() to pyconnection.
While here convert all instances of y* for incoming binary data to y#
since we won't be changing it at all and avoids some unneeded copying.
2017-03-13 13:30:57 +01:00
Joris Vink 4a56424842 Improve python websocket handling a bit.
Convert the data parameter to a string if the op was WEBSOCKET_OP_TEXT
or convert to bytes if op was WEBSOCKET_OP_BINARY so the callee does
not have to do this anymore.
2017-03-13 12:29:42 +01:00
Joris Vink 0b84a9c1d4 expose c.disconnect() and c.fd to Python. 2017-03-13 12:22:50 +01:00
Joris Vink af899f15e0 Set error if http_body_rewind() fails. 2017-03-13 11:19:58 +01:00
Joris Vink ec901d0339 Make http_body_rewind() public.
Also let this function reset offset and lengths for http_body_read().

Make sure of this function in the python code so req.body can be called
multiple times in succession.
2017-03-13 11:17:55 +01:00
Joris Vink c136c5ffc5 python_calloc() no longer needs manual memset. 2017-02-27 21:34:26 -08:00
Joris Vink e89e644d10 call PyErr_Clear() before PyImport_ReloadModule(). 2017-02-22 20:54:57 +01:00
Joris Vink c6ca68f3f2 export http_body_path via python 2017-02-22 17:52:57 +01:00
Joris Vink e9b4f966c2 expose new cookie stuff to python.
req.populate_cookies()
value = req.cookie("name")
2017-02-07 22:54:42 +01:00
Joris Vink b8b8373702 when exiting clear any lingering python exceptions. 2017-02-06 16:54:38 +01:00