Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use kevent for macos compatibility #165

Merged
merged 3 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ on: [push, pull_request]
jobs:

test:

runs-on: ubuntu-latest
strategy:
matrix:
Expand All @@ -22,3 +21,18 @@ jobs:

#- name: Check C files are formatted
#run: nix-shell --run "net-check-format"

test-on-macos:
runs-on: macos-13

strategy:
matrix:
pg-version: ['17']

steps:
- uses: actions/checkout@v4
- uses: cachix/install-nix-action@v30
with:
nix_path: nixpkgs=channel:nixos-unstable
- name: Run tests
run: nix-shell --run "net-with-nginx net-with-pg-${{ matrix.pg-version }} python -m pytest -vv"
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PG_CFLAGS = -std=c11 -Werror -Wno-declaration-after-statement
EXTENSION = pg_net
EXTVERSION = 0.12.0
EXTVERSION = 0.13.0

DATA = $(wildcard sql/*--*.sql)

Expand All @@ -9,7 +9,7 @@ REGRESS = $(patsubst test/sql/%.sql,%,$(TESTS))
REGRESS_OPTS = --use-existing --inputdir=test

MODULE_big = $(EXTENSION)
OBJS = src/worker.o src/util.o src/core.o
OBJS = src/worker.o src/util.o src/core.o src/event.o

all: sql/$(EXTENSION)--$(EXTVERSION).sql $(EXTENSION).control

Expand Down
2 changes: 1 addition & 1 deletion nix/pg_net.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ stdenv.mkDerivation {

installPhase = ''
mkdir -p $out/bin
install -D pg_net.so -t $out/lib
install -D *.{dylib,so} -t $out/lib

install -D -t $out/share/postgresql/extension sql/*.sql
install -D -t $out/share/postgresql/extension pg_net.control
Expand Down
2 changes: 1 addition & 1 deletion nix/postgresql/generic.nix
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ let
wrapProgram $out/bin/initdb --prefix PATH ":" ${glibc.bin}/bin
'';

doCheck = !stdenv'.isDarwin;
doCheck = false;
# autodetection doesn't seem to able to find this, but it's there.
checkTarget = "check";

Expand Down
5 changes: 3 additions & 2 deletions shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ mkShell {
pythonDeps
format.do format.doCheck
nginxCustom.nginxScript
gdbScript
] ++ nixopsScripts;
] ++
nixopsScripts ++
lib.optional stdenv.isLinux [gdbScript];
shellHook = ''
export HISTFILE=.history
'';
Expand Down
1 change: 1 addition & 0 deletions sql/pg_net--0.12.0--0.13.0.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-- no SQL changes in 0.13.0
70 changes: 1 addition & 69 deletions src/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@
#include <curl/curl.h>
#include <curl/multi.h>

#include <sys/epoll.h>
#include <sys/timerfd.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <inttypes.h>

#include "util.h"
#include "core.h"
#include "event.h"

typedef struct {
int64 id;
Expand All @@ -48,73 +47,6 @@ body_cb(void *contents, size_t size, size_t nmemb, void *userp)
return realsize;
}

static int multi_timer_cb(CURLM *multi, long timeout_ms, LoopState *lstate) {
elog(DEBUG2, "multi_timer_cb: Setting timeout to %ld ms\n", timeout_ms);

itimerspec its =
timeout_ms > 0 ?
// assign the timeout normally
(itimerspec){
.it_value.tv_sec = timeout_ms / 1000,
.it_value.tv_nsec = (timeout_ms % 1000) * 1000 * 1000,
}:
timeout_ms == 0 ?
/* libcurl wants us to timeout now, however setting both fields of
* new_value.it_value to zero disarms the timer. The closest we can
* do is to schedule the timer to fire in 1 ns. */
(itimerspec){
.it_value.tv_sec = 0,
.it_value.tv_nsec = 1,
}:
// libcurl passes a -1 to indicate the timer should be deleted
(itimerspec){};

int no_flags = 0;
if (timerfd_settime(lstate->timerfd, no_flags, &its, NULL) < 0) {
ereport(ERROR, errmsg("timerfd_settime failed"));
}

return 0;
}

static int multi_socket_cb(CURL *easy, curl_socket_t sockfd, int what, LoopState *lstate, void *socketp) {
static char *whatstrs[] = { "NONE", "CURL_POLL_IN", "CURL_POLL_OUT", "CURL_POLL_INOUT", "CURL_POLL_REMOVE" };
elog(DEBUG2, "multi_socket_cb: sockfd %d received %s", sockfd, whatstrs[what]);

int epoll_op;
if(!socketp){
epoll_op = EPOLL_CTL_ADD;
bool *socket_exists = palloc(sizeof(bool));
curl_multi_assign(lstate->curl_mhandle, sockfd, socket_exists);
} else if (what == CURL_POLL_REMOVE){
epoll_op = EPOLL_CTL_DEL;
pfree(socketp);
curl_multi_assign(lstate->curl_mhandle, sockfd, NULL);
} else {
epoll_op = EPOLL_CTL_MOD;
}

epoll_event ev = {
.data.fd = sockfd,
.events =
(what & CURL_POLL_IN) ?
EPOLLIN:
(what & CURL_POLL_OUT) ?
EPOLLOUT:
0, // no event is assigned since here we get CURL_POLL_REMOVE and the sockfd will be removed
};

// epoll_ctl will copy ev, so there's no need to do palloc for the epoll_event
// https://github.com/torvalds/linux/blob/e32cde8d2bd7d251a8f9b434143977ddf13dcec6/fs/eventpoll.c#L2408-L2418
if (epoll_ctl(lstate->epfd, epoll_op, sockfd, &ev) < 0) {
int e = errno;
static char *opstrs[] = { "NONE", "EPOLL_CTL_ADD", "EPOLL_CTL_DEL", "EPOLL_CTL_MOD" };
ereport(ERROR, errmsg("epoll_ctl with %s failed when receiving %s for sockfd %d: %s", whatstrs[what], opstrs[epoll_op], sockfd, strerror(e)));
}

return 0;
}

// We need a different memory context here, as the parent function will have an SPI memory context, which has a shorter lifetime.
static void init_curl_handle(CURLM *curl_mhandle, MemoryContext curl_memctx, int64 id, Datum urlBin, NullableDatum bodyBin, NullableDatum headersBin, Datum methodBin, int32 timeout_milliseconds){
MemoryContext old_ctx = MemoryContextSwitchTo(curl_memctx);
Expand Down
4 changes: 0 additions & 4 deletions src/core.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
#ifndef CORE_H
#define CORE_H

typedef struct itimerspec itimerspec;
typedef struct epoll_event epoll_event;

typedef struct {
int epfd;
int timerfd;
CURLM *curl_mhandle;
} LoopState;

Expand Down
Loading