Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot connect kdb+ through unix domain socket #23

Open
even-wei opened this issue Apr 23, 2019 · 1 comment
Open

Cannot connect kdb+ through unix domain socket #23

even-wei opened this issue Apr 23, 2019 · 1 comment

Comments

@even-wei
Copy link

Here is my code, please check it out.

package main

import (
	"bytes"
	"fmt"
	"log"
	"os"
	"os/exec"
	"strconv"

	kdb "github.com/sv/kdbgo"
)

var testPort = 0

func StartQ(ready chan struct{}) {
	fmt.Println("Starting q process on random port")
	_, err := exec.LookPath("q")
	if err != nil {
		log.Fatal("installing q is in your future")
	}
	cmd := exec.Command("q", "-p", "0W", "-q")
	stdin, err := cmd.StdinPipe()
	if err != nil {
		log.Fatal("Failed to connect stdin", err)
	}
	stdin.Write([]byte(".z.pi:.z.pg:.z.ps:{value 0N!x};system\"p\"\n"))
	stdout, err := cmd.StdoutPipe()
	if err != nil {
		log.Fatal("Failed to connect stdout", err)
	}
	stderr, err := cmd.StderrPipe()
	if err != nil {
		log.Fatal("Failed to connect stderr", err)
	}
	err = cmd.Start()
	if err != nil {
		log.Fatal("Failed to start q", err)
	}
	buf := make([]byte, 16)
	n, err := stdout.Read(buf)
	if err != nil {
		log.Fatal("Failed to fill port number for child process: ", err)
	}
	testPort, err = strconv.Atoi(string(buf[:bytes.IndexByte(buf, 'i')]))
	if err != nil {
		fmt.Println("Failed to setup listening port", string(buf[:n]), err)
		cmd.Process.Kill()
		os.Exit(1)
	}
	fmt.Println("Listening port is ", testPort)
	go func() {
		for {
			buf := make([]byte, 256)
			n, err := stderr.Read(buf)
			fmt.Println("Q stderr output:", string(buf[:n]))
			if err != nil {
				fmt.Println("Q stderr error:", err)
				return
			}
		}
	}()
	go func() {
		for {
			buf := make([]byte, 256)
			_, err := stdout.Read(buf)
			//fmt.Println("Q stdout output:", string(buf[:n]))
			if err != nil {
				fmt.Println("Q stdout error:", err)
				return
			}
		}
	}()
	ready <- struct{}{}
	cmd.Wait()
	//stdin.Write([]byte("show `exiting_process;\nexit 0\n"))
}

func main() {
	ready := make(chan struct{})
	go StartQ(ready)

	<-ready
	log.Println("Connect to kdb+ through unix socket.")
	con, err := kdb.DialUnix("localhost", testPort, "")
	if err != nil {
		panic(err)
	}

	log.Println("Close the connection")
	err = con.Close()
	if err != nil {
		panic(err)
	}
}
@sv
Copy link
Owner

sv commented Jun 5, 2019

What is the error you get from DialUnix?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants