diff options
author | Max Romanov <max.romanov@nginx.com> | 2017-07-12 20:32:18 +0300 |
---|---|---|
committer | Max Romanov <max.romanov@nginx.com> | 2017-07-12 20:32:18 +0300 |
commit | a97a5e8a0b7a921905994bae101d4f74f5a8aaa4 (patch) | |
tree | 922a2e6fed3630439b56d99b57c7452ee259f617 /src/nginext | |
parent | 51bbdd33382eee7da34ff0ed7d0df51ab2345786 (diff) | |
download | unit-a97a5e8a0b7a921905994bae101d4f74f5a8aaa4.tar.gz unit-a97a5e8a0b7a921905994bae101d4f74f5a8aaa4.tar.bz2 |
Go: worker initialization. READY message to master.
Diffstat (limited to 'src/nginext')
-rw-r--r-- | src/nginext/nginext.go | 4 | ||||
-rw-r--r-- | src/nginext/nxt_go_lib.c | 35 | ||||
-rw-r--r-- | src/nginext/nxt_go_lib.h | 2 | ||||
-rw-r--r-- | src/nginext/nxt_go_log.h | 7 | ||||
-rw-r--r-- | src/nginext/nxt_go_run_ctx.c | 10 | ||||
-rw-r--r-- | src/nginext/nxt_go_run_ctx.h | 3 | ||||
-rw-r--r-- | src/nginext/port.go | 53 |
7 files changed, 89 insertions, 25 deletions
diff --git a/src/nginext/nginext.go b/src/nginext/nginext.go index 2bde80a6..de0218e8 100644 --- a/src/nginext/nginext.go +++ b/src/nginext/nginext.go @@ -84,7 +84,6 @@ func ListenAndServe() { var read_port *port go_ports_env := os.Getenv("NXT_GO_PORTS") - fmt.Printf("NXT_GO_PORTS = %s\n", go_ports_env) ports := strings.Split(go_ports_env, ";") pid := os.Getpid() @@ -95,7 +94,6 @@ func ListenAndServe() { } attrs := strings.Split(port_str, ",") - fmt.Printf("Port = %q\n", attrs) var attrsN [5]int var err error @@ -119,6 +117,8 @@ func ListenAndServe() { } if read_port != nil { + C.nxt_go_ready() + for !nxt_go_quit { read_port.read() } diff --git a/src/nginext/nxt_go_lib.c b/src/nginext/nxt_go_lib.c index 86d5b619..57c3da8e 100644 --- a/src/nginext/nxt_go_lib.c +++ b/src/nginext/nxt_go_lib.c @@ -42,7 +42,7 @@ nxt_go_request_done(nxt_go_request_t r) } void -nxt_go_listen_and_serve() +nxt_go_ready() { } @@ -54,16 +54,6 @@ nxt_go_process_port_msg(void *buf, size_t buf_len, void *oob, size_t oob_len) #else -#if 0 - -#include <nxt_runtime.h> -#include <nxt_master_process.h> -#include <nxt_application.h> - -#include "nxt_go_port.h" - -#endif - #include "nxt_go_run_ctx.h" #include "nxt_go_log.h" #include "nxt_go_port.h" @@ -184,6 +174,29 @@ nxt_go_request_done(nxt_go_request_t r) } +void +nxt_go_ready() +{ + char *go_stream; + nxt_port_msg_t port_msg; + + go_stream = getenv("NXT_GO_STREAM"); + + if (go_stream == NULL) { + return; + } + + port_msg.stream = atol(go_stream); + port_msg.pid = getpid(); + port_msg.reply_port = 0; + port_msg.type = NXT_PORT_MSG_READY; + port_msg.last = 0; + port_msg.mmap = 0; + + nxt_go_master_send(&port_msg, sizeof(port_msg), NULL, 0); +} + + nxt_go_request_t nxt_go_process_port_msg(void *buf, size_t buf_len, void *oob, size_t oob_len) { diff --git a/src/nginext/nxt_go_lib.h b/src/nginext/nxt_go_lib.h index 1c7f61c0..220fc9b3 100644 --- a/src/nginext/nxt_go_lib.h +++ b/src/nginext/nxt_go_lib.h @@ -31,7 +31,7 @@ int nxt_go_request_close(nxt_go_request_t r); int nxt_go_request_done(nxt_go_request_t r); -void nxt_go_listen_and_serve(); +void nxt_go_ready(); nxt_go_request_t nxt_go_process_port_msg(void *buf, size_t buf_len, void *oob, size_t oob_len); diff --git a/src/nginext/nxt_go_log.h b/src/nginext/nxt_go_log.h index da1ce6a0..d596cfb3 100644 --- a/src/nginext/nxt_go_log.h +++ b/src/nginext/nxt_go_log.h @@ -16,8 +16,7 @@ #if (NXT_DEBUG) #define nxt_go_debug(fmt, ARGS...) \ - fprintf(stdout, "go debug[%p]: " fmt "\n", \ - (void *) (intptr_t) pthread_self(), ##ARGS) + fprintf(stderr, "[go debug] " fmt "\n", ##ARGS) #else @@ -26,10 +25,10 @@ #endif #define nxt_go_warn(fmt, ARGS...) \ - fprintf(stdout, "go warn: " fmt "\n", ##ARGS) + fprintf(stderr, "[go warn] " fmt "\n", ##ARGS) #define nxt_go_error(fmt, ARGS...) \ - fprintf(stdout, "go error: " fmt "\n", ##ARGS) + fprintf(stderr, "[go error] " fmt "\n", ##ARGS) #endif /* _NXT_GO_LOG_H_INCLUDED_ */ diff --git a/src/nginext/nxt_go_run_ctx.c b/src/nginext/nxt_go_run_ctx.c index 2d284bc5..0788b0d1 100644 --- a/src/nginext/nxt_go_run_ctx.c +++ b/src/nginext/nxt_go_run_ctx.c @@ -168,6 +168,8 @@ nxt_go_ctx_init(nxt_go_run_ctx_t *ctx, nxt_port_msg_t *port_msg, ctx->wport_msg.type = NXT_PORT_MSG_DATA; ctx->wport_msg.mmap = 1; + ctx->wmmap_msg = (nxt_port_mmap_msg_t *) ( &ctx->wport_msg + 1 ); + return nxt_go_ctx_init_rbuf(ctx); } @@ -197,6 +199,7 @@ nxt_go_ctx_add_msg(nxt_go_run_ctx_t *ctx, nxt_port_msg_t *port_msg, size_t size) nxt_int_t nxt_go_ctx_flush(nxt_go_run_ctx_t *ctx, int last) { + int i; nxt_int_t rc; if (last != 0) { @@ -205,6 +208,13 @@ nxt_go_ctx_flush(nxt_go_run_ctx_t *ctx, int last) nxt_go_debug("flush buffers (%d)", last); + for (i = 0; i < ctx->nwbuf; i++) { + nxt_port_mmap_msg_t *m = ctx->wmmap_msg + i; + + nxt_go_debug(" mmap_msg[%d]={%d, %d, %d}", i, + m->mmap_id, m->chunk_id, m->size); + } + rc = nxt_go_port_send(ctx->msg.port_msg->pid, ctx->msg.port_msg->reply_port, &ctx->wport_msg, sizeof(nxt_port_msg_t) + ctx->nwbuf * sizeof(nxt_port_mmap_msg_t), NULL, 0); diff --git a/src/nginext/nxt_go_run_ctx.h b/src/nginext/nxt_go_run_ctx.h index 244bd7c3..c7c3da15 100644 --- a/src/nginext/nxt_go_run_ctx.h +++ b/src/nginext/nxt_go_run_ctx.h @@ -36,6 +36,7 @@ typedef struct { nxt_go_msg_t msg; nxt_go_process_t *process; + nxt_port_mmap_msg_t *wmmap_msg; uint32_t nrbuf; nxt_buf_t rbuf; @@ -43,7 +44,7 @@ typedef struct { uint32_t nwbuf; nxt_buf_t wbuf; nxt_port_msg_t wport_msg; - nxt_port_mmap_msg_t wmmap_msg[8]; + char wmmap_msg_buf[ sizeof(nxt_port_mmap_msg_t) * 8 ]; nxt_app_request_t r; diff --git a/src/nginext/port.go b/src/nginext/port.go index 01921b5a..639b73d5 100644 --- a/src/nginext/port.go +++ b/src/nginext/port.go @@ -25,6 +25,7 @@ type port_key struct { type port struct { key port_key + t int rcv *net.UnixConn snd *net.UnixConn } @@ -32,6 +33,7 @@ type port struct { type port_registry struct { sync.RWMutex m map[port_key]*port + t [5]*port } var port_registry_ port_registry @@ -44,6 +46,31 @@ func find_port(key port_key) *port { return res } +func remove_by_pid(pid int) { + port_registry_.Lock() + if port_registry_.m != nil { + for k, p := range port_registry_.m { + if k.pid == pid { + if port_registry_.t[p.t] == p { + port_registry_.t[p.t] = nil + } + + delete(port_registry_.m, k) + } + } + } + + port_registry_.Unlock() +} + +func master_port() *port { + port_registry_.RLock() + res := port_registry_.t[1] + port_registry_.RUnlock() + + return res +} + func add_port(p *port) { port_registry_.Lock() if port_registry_.m == nil { @@ -51,6 +78,7 @@ func add_port(p *port) { } port_registry_.m[p.key] = p + port_registry_.t[p.t] = p port_registry_.Unlock() } @@ -85,7 +113,6 @@ func getUnixConn(fd int) *net.UnixConn { return nil } - fmt.Printf("Unix-domain socket %d\n", fd) return uc } @@ -116,20 +143,36 @@ func nxt_go_port_send(pid C.int, id C.int, buf unsafe.Pointer, buf_size C.int, o return 0 } +//export nxt_go_master_send +func nxt_go_master_send(buf unsafe.Pointer, buf_size C.int, oob unsafe.Pointer, oob_size C.int) C.int { + p := master_port() + + if p != nil { + n, oobn, err := p.snd.WriteMsgUnix(C.GoBytes(buf, buf_size), C.GoBytes(oob, oob_size), nil) + + if err != nil { + fmt.Printf("write result %d (%d), %s\n", n, oobn, err) + } + + return C.int(n) + } + + return 0 +} + func new_port(pid int, id int, t int, rcv int, snd int) *port { p := &port{ key: port_key{ pid: pid, id: id, }, + t: t, rcv: getUnixConn(rcv), snd: getUnixConn(snd), } add_port(p) - fmt.Printf("new_port: %d, %d, %d, %d\n", pid, id, rcv, snd) - return p } @@ -137,9 +180,7 @@ func (p *port) read() { var buf [16384]byte var oob [1024]byte - n, oobn, _, _, err := p.rcv.ReadMsgUnix(buf[:], oob[:]) - - fmt.Printf("read result %d (%d), %s\n", n, oobn, err) + n, oobn, _, _, _ := p.rcv.ReadMsgUnix(buf[:], oob[:]) m := new_cmsg(buf[:n], oob[:oobn]) |