Skip to content

Commit

Permalink
Go: introducing SHM_ACK observer.
Browse files Browse the repository at this point in the history
Each request processed in a separate goroutine.  In case of OOSM state,
during response write, request goroutine blocks on channel which waits
event from main thread about SHM_ACK message from router.
  • Loading branch information
mar0x committed Dec 24, 2019
1 parent 763bdff commit 26ee4cb
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 11 deletions.
20 changes: 11 additions & 9 deletions go/nxt_cgo_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ static ssize_t nxt_cgo_port_send(nxt_unit_ctx_t *, nxt_unit_port_id_t *port_id,
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_id_t *port_id,
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 @@ -34,6 +35,7 @@ nxt_cgo_run(uintptr_t handler)
init.callbacks.remove_port = nxt_cgo_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.data = (void *) handler;

Expand Down Expand Up @@ -137,6 +139,13 @@ nxt_cgo_port_recv(nxt_unit_ctx_t *ctx, nxt_unit_port_id_t *port_id,
}


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)
Expand Down Expand Up @@ -166,15 +175,8 @@ nxt_cgo_response_send(uintptr_t req)
ssize_t
nxt_cgo_response_write(uintptr_t req, uintptr_t start, uint32_t len)
{
int rc;

rc = nxt_unit_response_write((nxt_unit_request_info_t *) req,
(void *) start, len);
if (rc != NXT_UNIT_OK) {
return -1;
}

return len;
return nxt_unit_response_write_nb((nxt_unit_request_info_t *) req,
(void *) start, len, 0);
}


Expand Down
32 changes: 32 additions & 0 deletions go/observable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (C) NGINX, Inc.
*/

package unit

import (
"sync"
)

type observable struct {
sync.Mutex
observers []chan int
}

func (o *observable) attach(c chan int) {
o.Lock()
defer o.Unlock()

o.observers = append(o.observers, c)
}

func (o *observable) notify(e int) {
o.Lock()
defer o.Unlock()

for _, v := range o.observers {
v <- e
}

o.observers = nil
}
36 changes: 34 additions & 2 deletions go/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type response struct {
headerSent bool
req *http.Request
c_req C.uintptr_t
ch chan int
}

func new_response(c_req C.uintptr_t, req *http.Request) *response {
Expand All @@ -40,8 +41,26 @@ func (r *response) Write(p []byte) (n int, err error) {
r.WriteHeader(http.StatusOK)
}

res := C.nxt_cgo_response_write(r.c_req, buf_ref(p), C.uint32_t(len(p)))
return int(res), nil
l := len(p)
written := int(0)
br := buf_ref(p)

for written < l {
res := C.nxt_cgo_response_write(r.c_req, br, C.uint32_t(l - written))

written += int(res)
br += C.uintptr_t(res)

if (written < l) {
if r.ch == nil {
r.ch = make(chan int, 2)
}

wait_shm_ack(r.ch)
}
}

return written, nil
}

func (r *response) WriteHeader(code int) {
Expand Down Expand Up @@ -85,3 +104,16 @@ func (r *response) Flush() {
r.WriteHeader(http.StatusOK)
}
}

var observer_registry_ observable

func wait_shm_ack(c chan int) {
observer_registry_.attach(c)

_ = <-c
}

//export nxt_go_shm_ack_handler
func nxt_go_shm_ack_handler() {
observer_registry_.notify(1)
}

0 comments on commit 26ee4cb

Please sign in to comment.