leafo / pgmoon

A pure Lua Postgres driver for use in OpenResty & more
MIT License
390 stars 93 forks source link

Add locking to query functions #132

Closed leafo closed 1 year ago

leafo commented 1 year ago

Since it's common to yield on network actions, in order to prevent multiple coroutines issuing queries at the same time a locking mechanism should be implemented over the duration of the query function.

Few notes:

leafo commented 1 year ago

Upon further testing, queries appear to be processed and returned in sequential order. I may do more stress test in the future to try to expose any issues.

If you send in 20 queries at once, they are returned in the order they were sent, which means that the correct response will be returned to the correct query. The throughput will just be limited to one query at a time. The query methods use an atomic send operation: they send the entire message as a single string on the socket. If we ever have a query operation that uses multiple socket write then we have to be careful that they might yield and another thread could try to send data in-between corrupt the request. Reads in pgmoon are not atomic but it doesn't matter since a single postgres connection will never returned an interleaved response as it would be impossible to parse.

Because of this, I don't believe it's necessary to add locking, it works as expected. A pgmoon connection is safe for concurrent access.

See https://github.com/leafo/lapis-concurrency-test for progress on implement concurrent connection pooling for Lapis & cqueues