Go: worker initialization. READY message to master.

This commit is contained in:
Max Romanov
2017-07-12 20:32:18 +03:00
parent 51bbdd3338
commit a97a5e8a0b
8 changed files with 138 additions and 95 deletions

View File

@@ -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()
}

View File

@@ -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)
{

View File

@@ -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);

View File

@@ -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_ */

View File

@@ -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);

View File

@@ -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;

View File

@@ -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])