summaryrefslogtreecommitdiffhomepage
path: root/src/go/unit/unit.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/go/unit/unit.go')
-rw-r--r--src/go/unit/unit.go123
1 files changed, 44 insertions, 79 deletions
diff --git a/src/go/unit/unit.go b/src/go/unit/unit.go
index 77be7612..06257768 100644
--- a/src/go/unit/unit.go
+++ b/src/go/unit/unit.go
@@ -6,17 +6,13 @@
package unit
/*
-#include "nxt_go_lib.h"
+#include "nxt_cgo_lib.h"
*/
import "C"
import (
- "errors"
"fmt"
"net/http"
- "os"
- "strconv"
- "strings"
"unsafe"
)
@@ -33,104 +29,73 @@ func buf_ref(buf []byte) C.uintptr_t {
return C.uintptr_t(uintptr(unsafe.Pointer(&buf[0])))
}
-func (buf *cbuf) init(b []byte) {
- buf.b = buf_ref(b)
- buf.s = C.size_t(len(b))
+type StringHeader struct {
+ Data unsafe.Pointer
+ Len int
}
-func (buf *cbuf) GoBytes() []byte {
- if buf == nil {
- var b [0]byte
- return b[:0]
- }
+func str_ref(s string) C.uintptr_t {
+ header := (*StringHeader)(unsafe.Pointer(&s))
- return C.GoBytes(unsafe.Pointer(uintptr(buf.b)), C.int(buf.s))
+ return C.uintptr_t(uintptr(unsafe.Pointer(header.Data)))
}
-var nxt_go_quit bool = false
-
-//export nxt_go_set_quit
-func nxt_go_set_quit() {
- nxt_go_quit = true
+func (buf *cbuf) init_bytes(b []byte) {
+ buf.b = buf_ref(b)
+ buf.s = C.size_t(len(b))
}
-func nxt_go_warn(format string, args ...interface{}) {
- fmt.Fprintf(os.Stderr, "[go warn] " + format + "\n", args...)
+func (buf *cbuf) init_string(s string) {
+ buf.b = str_ref(s)
+ buf.s = C.size_t(len(s))
}
-func nxt_go_debug(format string, args ...interface{}) {
- // fmt.Fprintf(os.Stderr, "[go debug] " + format + "\n", args...)
+type SliceHeader struct {
+ Data unsafe.Pointer
+ Len int
+ Cap int
}
-func ListenAndServe(addr string, handler http.Handler) error {
- var read_port *port
-
- go_ports_env := os.Getenv("NXT_GO_PORTS")
- if go_ports_env == "" {
- return http.ListenAndServe(addr, handler)
+func (buf *cbuf) GoBytes() []byte {
+ if buf == nil {
+ var b [0]byte
+ return b[:0]
}
- nxt_go_debug("NXT_GO_PORTS=%s", go_ports_env)
-
- ports := strings.Split(go_ports_env, ";")
- pid := os.Getpid()
-
- if len(ports) != 4 {
- return errors.New("Invalid NXT_GO_PORTS format")
+ bytesHeader := &SliceHeader{
+ Data: unsafe.Pointer(uintptr(buf.b)),
+ Len: int(buf.s),
+ Cap: int(buf.s),
}
- nxt_go_debug("version=%s", ports[0])
-
- builtin_version := C.GoString(C.nxt_go_version())
-
- if ports[0] != builtin_version {
- return fmt.Errorf("Versions mismatch: Unit %s, while application is built with %s",
- ports[0], builtin_version)
- }
+ return *(*[]byte)(unsafe.Pointer(bytesHeader))
+}
- stream, stream_err := strconv.Atoi(ports[1])
- if stream_err != nil {
- return stream_err
+func GoBytes(buf unsafe.Pointer, size C.int) []byte {
+ bytesHeader := &SliceHeader{
+ Data: buf,
+ Len: int(size),
+ Cap: int(size),
}
- read_port = nil
-
- for _, port_str := range ports[2:] {
- attrs := strings.Split(port_str, ",")
-
- if len(attrs) != 5 {
- return fmt.Errorf("Invalid port format: unexpected port attributes number %d, while 5 expected",
- len(attrs))
- }
-
- var attrsN [5]int
- var err error
- for i, attr := range attrs {
- attrsN[i], err = strconv.Atoi(attr)
- if err != nil {
- return fmt.Errorf("Invalid port format: number attribute expected at %d position instead of '%s'",
- i, attr);
- }
- }
+ return *(*[]byte)(unsafe.Pointer(bytesHeader))
+}
- p := new_port(attrsN[0], attrsN[1], attrsN[2], attrsN[3], attrsN[4])
+func nxt_go_warn(format string, args ...interface{}) {
+ str := fmt.Sprintf("[go] " + format, args...)
- if attrsN[0] == pid {
- read_port = p
- }
- }
+ C.nxt_cgo_warn(str_ref(str), C.uint32_t(len(str)))
+}
- if read_port == nil {
- return errors.New("Application read port not found");
+func ListenAndServe(addr string, handler http.Handler) error {
+ if handler == nil {
+ handler = http.DefaultServeMux
}
- C.nxt_go_ready(C.uint32_t(stream))
+ rc := C.nxt_cgo_run(C.uintptr_t(uintptr(unsafe.Pointer(&handler))))
- for !nxt_go_quit {
- err := read_port.read(handler)
- if err != nil {
- return err
- }
+ if rc != 0 {
+ return http.ListenAndServe(addr, handler)
}
return nil