<commit_before>// Copyright © 2015 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.

package http

import (
	"bytes"
	"encoding/base64"
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"sync"

	"github.com/TheThingsNetwork/ttn/core"
	"github.com/TheThingsNetwork/ttn/core/adapters/http/parser"
	. "github.com/TheThingsNetwork/ttn/core/errors"
	"github.com/TheThingsNetwork/ttn/utils/errors"
	"github.com/TheThingsNetwork/ttn/utils/stats"
	"github.com/apex/log"
)

// Adapter type materializes an http adapter which implements the basic http protocol
type Adapter struct {
	parser.PacketParser                // The adapter's parser contract
	http.Client                        // Adapter is also an http client
	serveMux            *http.ServeMux // Holds a references to the adapter servemux in order to dynamically define endpoints
	packets             chan pktReq    // Channel used to "transforms" incoming request to something we can handle concurrently
	ctx                 log.Interface  // Just a logger, no one really cares about him.
}

// Message sent through the packets channel when an incoming request arrives
type pktReq struct {
	core.Packet             // The actual packet that has been parsed
	response    chan pktRes // A response channel waiting for an success or reject confirmation
}

// Message sent through the response channel of a pktReq
type pktRes struct {
	statusCode int    // The http status code to set as an answer
	content    []byte // The response content.
}

// NewAdapter constructs and allocates a new http adapter
func NewAdapter(port uint, parser parser.PacketParser, ctx log.Interface) (*Adapter, error) {
	a := Adapter{
		PacketParser: parser,
		serveMux:     http.NewServeMux(),
		packets:      make(chan pktReq),
		ctx:          ctx,
		Client:       http.Client{},
	}

	a.RegisterEndpoint("/healthz", func(w http.ResponseWriter, req *http.Request) {<commit_msg>[refactor] Set timeout of HTTP adapter to 6 seconds<commit_after>// Copyright © 2015 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.

package http

import (
	"bytes"
	"encoding/base64"
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"sync"
	"time"

	"github.com/TheThingsNetwork/ttn/core"
	"github.com/TheThingsNetwork/ttn/core/adapters/http/parser"
	. "github.com/TheThingsNetwork/ttn/core/errors"
	"github.com/TheThingsNetwork/ttn/utils/errors"
	"github.com/TheThingsNetwork/ttn/utils/stats"
	"github.com/apex/log"
)

// Adapter type materializes an http adapter which implements the basic http protocol
type Adapter struct {
	parser.PacketParser                // The adapter's parser contract
	http.Client                        // Adapter is also an http client
	serveMux            *http.ServeMux // Holds a references to the adapter servemux in order to dynamically define endpoints
	packets             chan pktReq    // Channel used to "transforms" incoming request to something we can handle concurrently
	ctx                 log.Interface  // Just a logger, no one really cares about him.
}

// Message sent through the packets channel when an incoming request arrives
type pktReq struct {
	core.Packet             // The actual packet that has been parsed
	response    chan pktRes // A response channel waiting for an success or reject confirmation
}

// Message sent through the response channel of a pktReq
type pktRes struct {
	statusCode int    // The http status code to set as an answer
	content    []byte // The response content.
}

// NewAdapter constructs and allocates a new http adapter
func NewAdapter(port uint, parser parser.PacketParser, ctx log.Interface) (*Adapter, error) {
	a := Adapter{
		PacketParser: parser,
		serveMux:     http.NewServeMux(),
		packets:      make(chan pktReq),
		ctx:          ctx,
		Client: http.Client{
			Timeout: 6 * time.Second,
		},
	}

	a.RegisterEndpoint("/healthz", func(w http.ResponseWriter, req *http.Request) {
{'fraction_non_alphanumeric': 0.08382936507936507, 'fraction_numerical': 0.003472222222222222, 'mean_word_length': 3.09026369168357, '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': '3686267', 'n_tokens_mistral': 1200, 'n_tokens_neox': 1149, 'n_words': 430}