Skip to content

Commit

Permalink
Go: removing C proxy functions and re-using goroutines.
Browse files Browse the repository at this point in the history
  • Loading branch information
mar0x committed Nov 18, 2020
1 parent d26afcb commit 8132e1f
Show file tree
Hide file tree
Showing 8 changed files with 225 additions and 303 deletions.
151 changes: 14 additions & 137 deletions go/nxt_cgo_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,10 @@
#include <nxt_unit_request.h>


static void nxt_cgo_request_handler(nxt_unit_request_info_t *req);
static nxt_cgo_str_t *nxt_cgo_str_init(nxt_cgo_str_t *dst,
nxt_unit_sptr_t *sptr, uint32_t length);
static int nxt_cgo_add_port(nxt_unit_ctx_t *, nxt_unit_port_t *port);
static void nxt_cgo_remove_port(nxt_unit_t *, nxt_unit_port_t *port);
static ssize_t nxt_cgo_port_send(nxt_unit_ctx_t *, nxt_unit_port_t *port,
const void *buf, size_t buf_size, const void *oob, size_t oob_size);
static ssize_t nxt_cgo_port_recv(nxt_unit_ctx_t *, nxt_unit_port_t *port,
void *buf, size_t buf_size, void *oob, size_t oob_size);
static void nxt_cgo_shm_ack_handler(nxt_unit_ctx_t *ctx);

int
nxt_cgo_run(uintptr_t handler)
Expand All @@ -30,12 +24,12 @@ nxt_cgo_run(uintptr_t handler)

memset(&init, 0, sizeof(init));

init.callbacks.request_handler = nxt_cgo_request_handler;
init.callbacks.add_port = nxt_cgo_add_port;
init.callbacks.remove_port = nxt_cgo_remove_port;
init.callbacks.request_handler = nxt_go_request_handler;
init.callbacks.add_port = nxt_go_add_port;
init.callbacks.remove_port = nxt_go_remove_port;
init.callbacks.port_send = nxt_cgo_port_send;
init.callbacks.port_recv = nxt_cgo_port_recv;
init.callbacks.shm_ack_handler = nxt_cgo_shm_ack_handler;
init.callbacks.shm_ack_handler = nxt_go_shm_ack_handler;

init.data = (void *) handler;

Expand All @@ -52,76 +46,6 @@ nxt_cgo_run(uintptr_t handler)
}


static void
nxt_cgo_request_handler(nxt_unit_request_info_t *req)
{
uint32_t i;
uintptr_t go_req;
nxt_cgo_str_t method, uri, name, value, proto, host, remote_addr;
nxt_unit_field_t *f;
nxt_unit_request_t *r;

r = req->request;

go_req = nxt_go_request_create((uintptr_t) req,
nxt_cgo_str_init(&method, &r->method, r->method_length),
nxt_cgo_str_init(&uri, &r->target, r->target_length));

nxt_go_request_set_proto(go_req,
nxt_cgo_str_init(&proto, &r->version, r->version_length), 1, 1);

for (i = 0; i < r->fields_count; i++) {
f = &r->fields[i];

nxt_go_request_add_header(go_req,
nxt_cgo_str_init(&name, &f->name, f->name_length),
nxt_cgo_str_init(&value, &f->value, f->value_length));
}

nxt_go_request_set_content_length(go_req, r->content_length);
nxt_go_request_set_host(go_req,
nxt_cgo_str_init(&host, &r->server_name, r->server_name_length));
nxt_go_request_set_remote_addr(go_req,
nxt_cgo_str_init(&remote_addr, &r->remote, r->remote_length));

if (r->tls) {
nxt_go_request_set_tls(go_req);
}

nxt_go_request_handler(go_req, (uintptr_t) req->unit->data);
}


static nxt_cgo_str_t *
nxt_cgo_str_init(nxt_cgo_str_t *dst, nxt_unit_sptr_t *sptr, uint32_t length)
{
dst->length = length;
dst->start = nxt_unit_sptr_get(sptr);

return dst;
}


static int
nxt_cgo_add_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port)
{
nxt_go_add_port((uintptr_t) ctx, port->id.pid, port->id.id,
port->in_fd, port->out_fd);

port->in_fd = -1;
port->out_fd = -1;

return NXT_UNIT_OK;
}


static void
nxt_cgo_remove_port(nxt_unit_t *unit, nxt_unit_port_t *port)
{
nxt_go_remove_port(port->id.pid, port->id.id);
}


static ssize_t
nxt_cgo_port_send(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port,
const void *buf, size_t buf_size, const void *oob, size_t oob_size)
Expand All @@ -140,78 +64,31 @@ nxt_cgo_port_recv(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port,
}


static void
nxt_cgo_shm_ack_handler(nxt_unit_ctx_t *ctx)
{
return nxt_go_shm_ack_handler();
}


int
nxt_cgo_response_create(uintptr_t req, int status, int fields,
uint32_t fields_size)
{
return nxt_unit_response_init((nxt_unit_request_info_t *) req,
status, fields, fields_size);
}


int
nxt_cgo_response_add_field(uintptr_t req, uintptr_t name, uint8_t name_len,
uintptr_t value, uint32_t value_len)
{
return nxt_unit_response_add_field((nxt_unit_request_info_t *) req,
(char *) name, name_len,
(char *) value, value_len);
}


int
nxt_cgo_response_send(uintptr_t req)
{
return nxt_unit_response_send((nxt_unit_request_info_t *) req);
}


ssize_t
nxt_cgo_response_write(uintptr_t req, uintptr_t start, uint32_t len)
nxt_cgo_response_write(nxt_unit_request_info_t *req, uintptr_t start,
uint32_t len)
{
return nxt_unit_response_write_nb((nxt_unit_request_info_t *) req,
(void *) start, len, 0);
return nxt_unit_response_write_nb(req, (void *) start, len, 0);
}


ssize_t
nxt_cgo_request_read(uintptr_t req, uintptr_t dst, uint32_t dst_len)
{
return nxt_unit_request_read((nxt_unit_request_info_t *) req,
(void *) dst, dst_len);
}


int
nxt_cgo_request_close(uintptr_t req)
nxt_cgo_request_read(nxt_unit_request_info_t *req, uintptr_t dst,
uint32_t dst_len)
{
return 0;
return nxt_unit_request_read(req, (void *) dst, dst_len);
}


void
nxt_cgo_request_done(uintptr_t req, int res)
nxt_cgo_warn(const char *msg, uint32_t msg_len)
{
nxt_unit_request_done((nxt_unit_request_info_t *) req, res);
}


void
nxt_cgo_unit_run_shared(uintptr_t ctx)
{
nxt_unit_run_shared((nxt_unit_ctx_t *) ctx);
nxt_unit_warn(NULL, "%.*s", (int) msg_len, (char *) msg);
}


void
nxt_cgo_warn(uintptr_t msg, uint32_t msg_len)
nxt_cgo_alert(const char *msg, uint32_t msg_len)
{
nxt_unit_warn(NULL, "%.*s", (int) msg_len, (char *) msg);
nxt_unit_alert(NULL, "%.*s", (int) msg_len, (char *) msg);
}
32 changes: 11 additions & 21 deletions go/nxt_cgo_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,22 @@
#include <stdint.h>
#include <stdlib.h>
#include <sys/types.h>
#include <nxt_unit.h>
#include <nxt_unit_request.h>

typedef struct {
int length;
char *start;
} nxt_cgo_str_t;
enum {
NXT_FIELDS_OFFSET = offsetof(nxt_unit_request_t, fields)
};

int nxt_cgo_run(uintptr_t handler);

int nxt_cgo_response_create(uintptr_t req, int code, int fields,
uint32_t fields_size);
ssize_t nxt_cgo_response_write(nxt_unit_request_info_t *req,
uintptr_t src, uint32_t len);

int nxt_cgo_response_add_field(uintptr_t req, uintptr_t name, uint8_t name_len,
uintptr_t value, uint32_t value_len);
ssize_t nxt_cgo_request_read(nxt_unit_request_info_t *req,
uintptr_t dst, uint32_t dst_len);

int nxt_cgo_response_send(uintptr_t req);

ssize_t nxt_cgo_response_write(uintptr_t req, uintptr_t src, uint32_t len);

ssize_t nxt_cgo_request_read(uintptr_t req, uintptr_t dst, uint32_t dst_len);

int nxt_cgo_request_close(uintptr_t req);

void nxt_cgo_request_done(uintptr_t req, int res);

void nxt_cgo_unit_run_shared(uintptr_t ctx);

void nxt_cgo_warn(uintptr_t msg, uint32_t msg_len);
void nxt_cgo_warn(const char *msg, uint32_t msg_len);
void nxt_cgo_alert(const char *msg, uint32_t msg_len);

#endif /* _NXT_CGO_LIB_H_INCLUDED_ */
48 changes: 31 additions & 17 deletions go/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package unit
import "C"

import (
"io"
"net"
"os"
"sync"
Expand Down Expand Up @@ -79,44 +80,51 @@ func getUnixConn(fd int) *net.UnixConn {

c, err := net.FileConn(f)
if err != nil {
nxt_go_warn("FileConn error %s", err)
nxt_go_alert("FileConn error %s", err)
return nil
}

uc, ok := c.(*net.UnixConn)
if !ok {
nxt_go_warn("Not a Unix-domain socket %d", fd)
nxt_go_alert("Not a Unix-domain socket %d", fd)
return nil
}

return uc
}

//export nxt_go_add_port
func nxt_go_add_port(ctx C.uintptr_t, pid C.int, id C.int, rcv C.int, snd C.int) {
p := &port{
func nxt_go_add_port(ctx *C.nxt_unit_ctx_t, p *C.nxt_unit_port_t) C.int {

new_port := &port{
key: port_key{
pid: int(pid),
id: int(id),
pid: int(p.id.pid),
id: int(p.id.id),
},
rcv: getUnixConn(int(rcv)),
snd: getUnixConn(int(snd)),
rcv: getUnixConn(int(p.in_fd)),
snd: getUnixConn(int(p.out_fd)),
}

add_port(p)
add_port(new_port)

p.in_fd = -1
p.out_fd = -1

if id == 65535 {
go func(ctx C.uintptr_t) {
C.nxt_cgo_unit_run_shared(ctx);
if new_port.key.id == 65535 {
go func(ctx *C.nxt_unit_ctx_t) {
C.nxt_unit_run_shared(ctx);
}(ctx)
}

return C.NXT_UNIT_OK
}

//export nxt_go_remove_port
func nxt_go_remove_port(pid C.int, id C.int) {
func nxt_go_remove_port(unit *C.nxt_unit_t, p *C.nxt_unit_port_t) {

key := port_key{
pid: int(pid),
id: int(id),
pid: int(p.id.pid),
id: int(p.id.id),
}

port_registry_.Lock()
Expand All @@ -139,7 +147,7 @@ func nxt_go_port_send(pid C.int, id C.int, buf unsafe.Pointer, buf_size C.int,
p := find_port(key)

if p == nil {
nxt_go_warn("port %d:%d not found", pid, id)
nxt_go_alert("port %d:%d not found", pid, id)
return 0
}

Expand Down Expand Up @@ -167,14 +175,20 @@ func nxt_go_port_recv(pid C.int, id C.int, buf unsafe.Pointer, buf_size C.int,
p := find_port(key)

if p == nil {
nxt_go_warn("port %d:%d not found", pid, id)
nxt_go_alert("port %d:%d not found", pid, id)
return 0
}

n, oobn, _, _, err := p.rcv.ReadMsgUnix(GoBytes(buf, buf_size),
GoBytes(oob, oob_size))

if err != nil {
if nerr, ok := err.(*net.OpError); ok {
if nerr.Err == io.EOF {
return 0
}
}

nxt_go_warn("read result %d (%d), %s", n, oobn, err)

n = -1
Expand Down
Loading

0 comments on commit 8132e1f

Please sign in to comment.