From 91d15fa6cd625fc993e62979aa2b3fd7936f4990 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?A=2ESamet=20=C4=B0leri?= Date: Fri, 7 Jul 2023 15:40:49 +0300 Subject: [PATCH] feat: add resolving host (#51) * feat: add resolving host * feat: add dcpconnect method resolvable http --- couchbase/client.go | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/couchbase/client.go b/couchbase/client.go index c755706..cb2b427 100644 --- a/couchbase/client.go +++ b/couchbase/client.go @@ -8,6 +8,8 @@ import ( "os" "time" + "github.com/couchbase/gocbcore/v10/connstr" + "github.com/Trendyol/go-dcp-client/config" "github.com/google/uuid" @@ -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{ @@ -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 @@ -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{