DGraph Go client throwing error “all SubConns are in TransientFailure, latest connection error: connection closed”

I am trying to connect DGraph using Go client on a Windows machine.

I have started by DGraph Client by using the following docker command.

docker run --rm -it -p 8080:8080 -p 9080:9080 -p 8000:8000 -v ~/dgraph:/dgraph dgraph/standalone:v20.03.0

I can able to access dgraph UI at HTTP://localhost:8000. I was able to Alter Schema, Set data, and Query it back using dgraph UI.

I have the following implementation in Go to connect DGraph.

import (
    "context"
    "fmt"
	"log"

    "github.com/dgraph-io/dgo/v2"
	"github.com/dgraph-io/dgo/v2/protos/api"
    "google.golang.org/grpc"
)

// UserGroup type
type UserGroup struct {
    UID         string `json:"uid,omitempty"`
    Name        string `json:"name,omitempty"`
    Description string `json:"description,omitempty"`
    Level       int    `json:"level,omitempty"`
    Enabled     bool   `json:"enabled,omitempty"`
}

// User type
type User struct {
    UID       string      `json:"uid,omitempty"`
	UserID    string      `json:"userId,omitempty"`
    Email     string      `json:"email,omitempty"`
	Enabled   bool        `json:"married,omitempty"`
	UserRoles []UserGroup `json:"user.userGroups,omitempty"`
    DType     []string    `json:"dgraph.type,omitempty"`
}

// CancelFunc type
type CancelFunc func()

// Client of dGraph
func Client() (*dgo.Dgraph, CancelFunc) {
    conn, err := grpc.Dial("localhost:9080", grpc.WithInsecure())
	if err != nil {
     	log.Fatal("While trying to dial gRPC")
    }

    dc := api.NewDgraphClient(conn)
	dg := dgo.NewDgraphClient(dc)

    return dg, func() {
	    if err := conn.Close(); err != nil {
		    log.Printf("Error while closing connection:%v", err)
	    }
    }
}

// GetData func
func GetData() {
    dg, cancel := Client()
	defer cancel()

    ctx := context.Background()
	q := `{
    	q(func: eq(dgraph.type, "User")) {
	      dgraph.type
	      uid
	      userId
		  email
    	}
      }`

	resp, err := dg.NewTxn().Query(ctx, q)
    if err != nil {
	    fmt.Println("Query error")
		log.Fatal(err)
    }

    fmt.Println(resp)
}

But I am getting the following error.

error: code = Unavailable desc = all SubConns are in TransientFailure, latest connection error: connection closed

Not sure what exactly I am missing here. Any help will be appreciated.

Hi @sandeepkangude

Welcome to the coolest graph database community on the planet. That makes you one of the cool cats :).

The error you posted is a generic gRPC error. To help me figure out what’s wrong, can you tell me which line of your code caused the error? A stack trace would be fantastic.

Hello @chewxy,

I am getting this error in the following line.

 resp, err := dg.NewTxn().Query(ctx, q)

I am getting an error whenever I try any operation (Login/Alter/Set/Query)

I’m trying to reproduce your error, but can’t. I get this instead:

$ go run *.go
json:"{\"q\":[]}" txn:<start_ts:180002 > latency:<parsing_ns:110480 processing_ns:8996345 encoding_ns:17805 assign_timestamp_ns:698104 total_ns:9925184 > metrics:<num_uids:<key:"_total" value:0 > num_uids:<key:"dgraph.type" value:0 > num_uids:<key:"email" value:0 > num_uids:<key:"uid" value:0 > num_uids:<key:"userId" value:0 > > 

Can you do me a favour and run the following command in Powershell?

Get-Process -Id (Get-NetTCPConnection -LocalPort 9080).OwningProcess

This will tell you what application is listening on port 9080

This is the result of Get-Process command

PS C:\Users\sk> Get-Process -Id (Get-NetTCPConnection -LocalPort 9080).OwningProcess
 
Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
 -------  ------    -----      -----     ------     --  -- -----------
     754      42    49488      26864      54.34  16064   1 lghub_agent

I am getting this error only when I connect to my localhost dgraph instance. If I connect to any other instance it is working fine. Anything else I can do to make it work with my localhost setup?

Well that is not very informative, other than you’re using a logitech mouse/keyboard/headphones/webcam.

So, if the logitech hub is using port 9080… can you try port forwarding your docker to another port, then connect to that port?

I am so sorry, I forgot run my docker. Here is the new output

PS C:\Users\sk> Get-Process -Id (Get-NetTCPConnection -LocalPort 9080).OwningProcess

Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName


280      22    22748      19480       1.30  17252   1 com.docker.backend
769      42    49488      26896      57.09  16064   1 lghub_agent

I tried with different port 9081, but still the same issue.

Ok give me a few hours. I am setting up a Windows system to replicate the problem

Thanks @chewxy
Take ur time.

I am using Windows 10 Enterprise OS and go version go1.15.3 windows/amd64

@chewxy

Somehow I am able to solve this problem.

I just uninstall lghub_agent process running on port 9080.

But I am still confused that, why gRPC server did not respond when I change the port to 9081.

Add -o=1 to your start command

This error in general it means that Dgo (or any client) wasn’t able to connect to an instance. If you see this error on the Dgraph instance, that is related to internal things. But isn’t the case here as you are using a standalone image.

In addition, you should create your own standalone image. As this image has fixed settings.

That’s odd, when there is a conflict of ports, usually, a conflict error isn’t thrown? At least in Darwin and Linux, it is like that.

As mentioned jokk33 above, you have to use the offset flag or try to “mirror” the port via docker run.
e.g:

docker run --rm -it -p 8081:8080 -p 9081:9080 -p 8000:8000 \
-v ~/dgraph:/dgraph dgraph/standalone:v20.03.0

If that doesn’t work, you should use the offset flag. But this is a standalone image, in that case you have to create your own standalone image.

Windows allows processes to steal sockets (SO_REUSEADDR) when they get priority.

1 Like

This works for me. Thank u