Skip to content

Commit

Permalink
refactor(common): use ParseRequestURI instead when `NormalizeCacheV…
Browse files Browse the repository at this point in the history
…alue`

also it exports the method

Signed-off-by: Dwi Siswanto <[email protected]>
  • Loading branch information
dwisiswant0 committed Sep 13, 2024
1 parent 87e99be commit f8e9bcd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
43 changes: 27 additions & 16 deletions pkg/protocols/common/hosterrorscache/hosterrorscache.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,35 @@ func (c *Cache) Close() {
c.failedTargets.Purge()
}

func (c *Cache) normalizeCacheValue(value string) string {
finalValue := value
if strings.HasPrefix(value, "http") {
if parsed, err := url.Parse(value); err == nil {
hostname := parsed.Host
finalPort := parsed.Port()
if finalPort == "" {
if parsed.Scheme == "https" {
finalPort = "443"
} else {
finalPort = "80"
}
hostname = net.JoinHostPort(parsed.Host, finalPort)
// NormalizeCacheValue processes the input value and returns a normalized cache
// value.
func (c *Cache) NormalizeCacheValue(value string) string {
var normalizedValue string = value

u, err := url.ParseRequestURI(value)
if err != nil || u.Host == "" {
u, err2 := url.ParseRequestURI("https://" + value)
if err2 != nil {
return normalizedValue
}

normalizedValue = u.Host
err = nil
} else {
port := u.Port()
if port == "" {
switch u.Scheme {
case "https":
normalizedValue = net.JoinHostPort(u.Host, "443")
case "http":
normalizedValue = net.JoinHostPort(u.Host, "80")
}
finalValue = hostname
} else {
normalizedValue = u.Host
}
}
return finalValue

return normalizedValue
}

// ErrUnresponsiveHost is returned when a host is unresponsive
Expand Down Expand Up @@ -166,7 +177,7 @@ func (c *Cache) GetKeyFromContext(ctx *contextargs.Context, err error) string {
address = tmp.String()
}
}
finalValue := c.normalizeCacheValue(address)
finalValue := c.NormalizeCacheValue(address)
return finalValue
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/protocols/common/hosterrorscache/hosterrorscache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func TestCacheMarkFailedConcurrent(t *testing.T) {

// the cache is not atomic during items creation, so we pre-create them with counter to zero
for _, test := range tests {
normalizedValue := cache.normalizeCacheValue(test.host)
normalizedValue := cache.NormalizeCacheValue(test.host)
newItem := &cacheItem{errors: atomic.Int32{}}
newItem.errors.Store(0)
_ = cache.failedTargets.Set(normalizedValue, newItem)
Expand All @@ -131,7 +131,7 @@ func TestCacheMarkFailedConcurrent(t *testing.T) {
for _, test := range tests {
require.True(t, cache.Check(newCtxArgs(test.host)))

normalizedCacheValue := cache.normalizeCacheValue(test.host)
normalizedCacheValue := cache.NormalizeCacheValue(test.host)
failedTarget, err := cache.failedTargets.Get(normalizedCacheValue)
require.Nil(t, err)
require.NotNil(t, failedTarget)
Expand Down

0 comments on commit f8e9bcd

Please sign in to comment.