# Best practices for single connection, multiple client, and auto reconnection?

**URL:** <https://discuss.dgraph.io/t/best-practices-for-single-connection-multiple-client-and-auto-reconnection/3416>\
**Category:** Users\
**Created:** [October 16, 2018, 9:19am UTC](https://discuss.dgraph.io/t/best-practices-for-single-connection-multiple-client-and-auto-reconnection/3416 "2018-10-16T09:19:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![u007](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/u007/32/1220_2.png) [@u007](https://discuss.dgraph.io/u/u007)\
**Post date:** [October 16, 2018, 9:19am UTC](https://discuss.dgraph.io/t/best-practices-for-single-connection-multiple-client-and-auto-reconnection/3416/1 "2018-10-16T09:19:58Z")

</div>

hi,

i would like to followup on my issue here:

> [@Multiple connections and connection pooling in golang](http://discuss.hypermode.com/t/multiple-connections-and-connection-pooling-in-golang/3093):
>
> what is the best practice for dealing with handler in golang via dgo? do i have to initiate respective grpc to server on every handler on my webserver? or do i initiate a grpc connection to server, and reuse the same grpc for multiple handler via dgo client? sample code:

so i can use only 1 single connection and create client from the connection for each http handler right?  
and what if dgraph was restarted? does it auto reconnect? or i have to also restart my web instance if i connect only when i start the server?

---

<div class="post-metadata">

**Author:** ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)\
**Post date:** [October 16, 2018, 5:46pm UTC](https://discuss.dgraph.io/t/best-practices-for-single-connection-multiple-client-and-auto-reconnection/3416/2 "2018-10-16T17:46:53Z")

</div>

> [@u007](#):
>
> so i can use only 1 single connection and create client from the connection for each http handler right?

Do you mean every http endpoint created by different Dgraph Servers? by offset. Or separate each operation (mutation, query, drop\_all, commit, alter and so on) per client? I think the answer is yes to all.

> [@u007](#):
>
> what if dgraph was restarted?

The client will certainly connect when the instance is ready.

> [@u007](#):
>
> does it auto reconnect? or i have to also restart my web instance if i connect only when i start the server?

I do not think so, but it will subtly connect when there is an DB instance ready. For each request the Client will return an error. If you make a new request, it will make a new connection as soon as it is available.

---

<div class="post-metadata">

**Author:** ![u007](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/u007/32/1220_2.png) [@u007](https://discuss.dgraph.io/u/u007)\
**Post date:** [October 17, 2018, 2:49am UTC](https://discuss.dgraph.io/t/best-practices-for-single-connection-multiple-client-and-auto-reconnection/3416/3 "2018-10-17T02:49:49Z")

</div>

> [@MichelDiz](#):
>
> Do you mean every http endpoint created by different Dgraph Servers? by offset. Or separate each operation (mutation, query, drop\_all, commit, alter and so on) per client? I think the answer is yes to all.

yes, i mean every handler call, means every request.  
should i do grpc connect for every instant of request?  
because my concern is, if dont grpc connect, if dgraph is down or restarted, will it reconnect

---

<div class="post-metadata">

**Author:** ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)\
**Post date:** [October 17, 2018, 3:15am UTC](https://discuss.dgraph.io/t/best-practices-for-single-connection-multiple-client-and-auto-reconnection/3416/4 "2018-10-17T03:15:25Z")

</div>

Not sure if I get the whole concept of your question, but are you having any connection problems?

---

<div class="post-metadata">

**Author:** ![u007](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/u007/32/1220_2.png) [@u007](https://discuss.dgraph.io/u/u007)\
**Post date:** [October 17, 2018, 10:13am UTC](https://discuss.dgraph.io/t/best-practices-for-single-connection-multiple-client-and-auto-reconnection/3416/5 "2018-10-17T10:13:04Z")

</div>

im not having any right now.  
okay in my go code,

i initiate a grpc connection to dgraph,

then in each handler, example:

```auto
http.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		//initiate client with grpc connection from main
	}))

```

my question is, since my grpc is initiated outside of handler, if i restart dgraph, will it havae connection issue in my web server? that i need to also restart my web server?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex007/uploads/dgraph/original/2X/2/2b38fdece2abe5814f2e51bee69d1e67fc304b0a.png) [@system](https://discuss.dgraph.io/u/system)\
**Post date:** [November 16, 2018, 10:13am UTC](https://discuss.dgraph.io/t/best-practices-for-single-connection-multiple-client-and-auto-reconnection/3416/6 "2018-11-16T10:13:08Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
