Skip to content

Commit

Permalink
feat: add resolving host (#51)
Browse files Browse the repository at this point in the history
* feat: add resolving host

* feat: add dcpconnect method resolvable http
  • Loading branch information
Abdulsametileri authored Jul 7, 2023
1 parent aa3658d commit 91d15fa
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions couchbase/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"os"
"time"

"github.com/couchbase/gocbcore/v10/connstr"

"github.com/Trendyol/go-dcp-client/config"

"github.com/google/uuid"
Expand Down Expand Up @@ -142,7 +144,7 @@ func (s *client) connect(bucketName string, connectionBufferSize uint, connectio
&gocbcore.AgentConfig{
BucketName: bucketName,
SeedConfig: gocbcore.SeedConfig{
HTTPAddrs: s.config.Hosts,
HTTPAddrs: resolveHostsAsHTTP(s.config.Hosts),
},
SecurityConfig: s.getSecurityConfig(),
CompressionConfig: gocbcore.CompressionConfig{
Expand Down Expand Up @@ -183,6 +185,29 @@ func (s *client) connect(bucketName string, connectionBufferSize uint, connectio
return client, nil
}

func resolveHostsAsHTTP(hosts []string) []string {
if len(hosts) == 1 {
parsedConnStr, err := connstr.Parse(hosts[0])
if err != nil {
panic("error parsing connection string " + hosts[0] + " " + err.Error())
}

out, err := connstr.Resolve(parsedConnStr)
if err != nil {
panic("error resolving connection string " + parsedConnStr.String() + " " + err.Error())
}

var httpHosts []string
for _, specHost := range out.HttpHosts {
httpHosts = append(httpHosts, fmt.Sprintf("%s:%d", specHost.Host, specHost.Port))
}

return httpHosts
}

return hosts
}

func (s *client) Connect() error {
connectionBufferSize := s.config.ConnectionBufferSize
connectionTimeout := s.config.ConnectionTimeout
Expand Down Expand Up @@ -245,7 +270,7 @@ func (s *client) DcpConnect() error {
agentConfig := &gocbcore.DCPAgentConfig{
BucketName: s.config.BucketName,
SeedConfig: gocbcore.SeedConfig{
HTTPAddrs: s.config.Hosts,
HTTPAddrs: resolveHostsAsHTTP(s.config.Hosts),
},
SecurityConfig: s.getSecurityConfig(),
CompressionConfig: gocbcore.CompressionConfig{
Expand Down

0 comments on commit 91d15fa

Please sign in to comment.