3

I deployed DgraphAlpha and DgraphZero in docker. I am connecting to Dgraph as described in the documentation.

func newClient() *dgo.Dgraph {
    d, err := grpc.Dial("localhost:9080", grpc.WithInsecure())
    if err != nil {
        log.Fatal(err)
    }

    return dgo.NewDgraphClient(
        api.NewDgraphClient(d),
    )
}

And the client is created successfully, but when I try to search

txn := i.dgraphClient.NewReadOnlyTxn()
defer txn.Discard(context.Background())

dgraphQuery := "search here"

response, err := txn.Query(context.Background(), dgraphQuery)
if err != nil {
    // error here
}

I get an error:

rpc error: code = Unavailable desc = connection closed before server preface received

This error does not always occur, at unexpected moments, and because of this it is difficult for me to determine its root. Has anyone encountered something similar? What could be the problem?

blackgreen
  • 34,072
  • 23
  • 111
  • 129
Devid Mercer
  • 160
  • 1
  • 2
  • 8

2 Answers2

5

Beside other transitory causes, a common cause of this error is the server running with TLS enabled and the client attempting to connect without TLS.

Make sure you correctly configured TLS options on the client:

tlsConfig := &tls.Config{
    Certificates: []tls.Certificate{myCertificate},
    RootCAs:      myCAPool,
}

tlsOpt := grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig))

conn, err := grpc.DialContext(ctx, "<connection_string>", tlsOpt)

Make also sure you are actually using client certificates on the client connection.

blackgreen
  • 34,072
  • 23
  • 111
  • 129
1

It may be some issue with timing. This may happens more often in the first requests? Do you have any log on the Dgraph side?

Consider:

  1. Use dial option WithBlock() to ensure you have a connection
  2. Use DialContext and use a context with timeout to avoid wait a lot
  3. Be aware that Insecure dial option is deprecated

Deprecated: use WithTransportCredentials and insecure.NewCredentials() instead.

About the error:

https://grpc.github.io/grpc/core/md_doc_statuscodes.html

Status code 14

This seems a transient error.

You may retry after some time. There are some retriers that can be used on this case like:

https://pkg.go.dev/github.com/eapache/go-resiliency/retrier

Tiago Peczenyj
  • 4,387
  • 2
  • 22
  • 35
  • Hi Tiago. Yes, I have a full logs from dgraph side but there are no errors or any messages that can describe the problem. Also, yes, you are right, this error occurs on the first request after connecting. I also tried to use a retrier, but it did not solve the problem. I also tested the other three solutions that you described, it also did not bring results. – Devid Mercer Mar 27 '22 at 14:51
  • 1
    Check if dgraph offer some healcheck endpoint. Something that says “I am here and I am ready to process”. In a more complex scenario, you may use a service like Consul, that will provide dns if the health check is ok. Or you can try to add a 5 seconds sleep in your application to give time to the dgraph be ready – Tiago Peczenyj Mar 27 '22 at 15:03