<commit_before>package handler

import (
	"fmt"
	"github.com/Comcast/webpa-common/context"
	"github.com/Comcast/webpa-common/hash"
	"net/http"
)

// HashRedirector provides a ContextHandler that redirects requests based on a ServiceHash.
func HashRedirector(serviceHash hash.ServiceHash) ContextHandler {
	return ContextHandlerFunc(func(requestContext context.Context, response http.ResponseWriter, request *http.Request) {
		address, err := serviceHash.Get(requestContext.DeviceId().Bytes())
		if err != nil {
			// service hash errors should be http.StatusServiceUnavailable, since
			// they almost always indicate that no nodes are in the hash due to no
			// available service nodes in the remote system (e.g. zookeeper)
			context.WriteJsonError(
				response,
				http.StatusServiceUnavailable,
				fmt.Sprintf("No nodes avaiable: %s", err.Error()),
			)

			return
		}

		target := address + request.URL.Path
		http.Redirect(response, request, target, http.StatusTemporaryRedirect)
	})
}<commit_msg>Log warnings for no nodes found<commit_after>package handler

import (
	"fmt"
	"github.com/Comcast/webpa-common/context"
	"github.com/Comcast/webpa-common/hash"
	"net/http"
)

// HashRedirector provides a ContextHandler that redirects requests based on a ServiceHash.
func HashRedirector(serviceHash hash.ServiceHash) ContextHandler {
	return ContextHandlerFunc(func(requestContext context.Context, response http.ResponseWriter, request *http.Request) {
		address, err := serviceHash.Get(requestContext.DeviceId().Bytes())
		if err != nil {
			message := fmt.Sprintf("No nodes avaiable: %s", err.Error())
			requestContext.Logger().Warn(message)

			// service hash errors should be http.StatusServiceUnavailable, since
			// they almost always indicate that no nodes are in the hash due to no
			// available service nodes in the remote system (e.g. zookeeper)
			context.WriteJsonError(
				response,
				http.StatusServiceUnavailable,
				message,
			)

			return
		}

		target := address + request.URL.Path
		http.Redirect(response, request, target, http.StatusTemporaryRedirect)
	})
}
{'fraction_non_alphanumeric': 0.10114503816793893, 'fraction_numerical': 0.0, 'mean_word_length': 4.390745501285347, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7020851', 'n_tokens_mistral': 660, 'n_tokens_neox': 598, 'n_words': 170}