<commit_before>package typhon

import (
	"fmt"
	"io"
	"net"
	"net/http"
	"os"
	"strconv"
	"time"

	"github.com/facebookgo/httpdown"
	log "github.com/mondough/slog"
	"golang.org/x/net/context"
)

type Listener interface {
	httpdown.Server
	Addr() net.Addr
	WaitC() <-chan struct{}
}

type listener struct {
	httpdown.Server
	addr net.Addr
}

func (l listener) Addr() net.Addr {
	return l.addr
}

func (l listener) WaitC() <-chan struct{} {
	c := make(chan struct{}, 0)
	go func() {
		l.Wait()
		close(c)
	}()
	return c
}

func Listen(svc Service) Listener {
	// Determine on which address to listen, choosing in order one of:
	// 1. LISTEN_ADDR environment variable
	// 2. PORT variable (listening on all interfaces)
	// 3. Random, available port
	addr := ":0"
	if addr_ := os.Getenv("LISTEN_ADDR"); addr_ != "" {
		addr = addr_
	} else if port, err := strconv.Atoi(os.Getenv("PORT")); err == nil && port >= 0 {
		addr = fmt.Sprintf(":%d", port)
	}
	tcpAddr, err := net.ResolveTCPAddr("tcp", addr)
	if err != nil {
		panic(err)
	}

	l, err := net.ListenTCP("tcp", tcpAddr)
	if err != nil {
		panic(err)
	}

	svc = svc.Filter(networkFilter)
	downer := &httpdown.HTTP{
		StopTimeout: 20 * time.Second,
		KillTimeout: 25 * time.Second}
	server := downer.Serve(&http.Server{
		Handler:        httpHandler(svc),
		MaxHeaderBytes: http.DefaultMaxHeaderBytes}, l)
	log.Info(nil, "Listening on %v", l.Addr())
	return listener{
		Server: server,
		addr:   l.Addr()}
}<commit_msg>Allow overriding default listener address<commit_after>package typhon

import (
	"fmt"
	"io"
	"net"
	"net/http"
	"os"
	"strconv"
	"time"

	"github.com/facebookgo/httpdown"
	log "github.com/mondough/slog"
	"golang.org/x/net/context"
)

var DefaultListenAddr = ":0"

type Listener interface {
	httpdown.Server
	Addr() net.Addr
	WaitC() <-chan struct{}
}

type listener struct {
	httpdown.Server
	addr net.Addr
}

func (l listener) Addr() net.Addr {
	return l.addr
}

func (l listener) WaitC() <-chan struct{} {
	c := make(chan struct{}, 0)
	go func() {
		l.Wait()
		close(c)
	}()
	return c
}

func Listen(svc Service) Listener {
	// Determine on which address to listen, choosing in order one of:
	// 1. LISTEN_ADDR environment variable
	// 2. PORT variable (listening on all interfaces)
	// 3. Random, available port
	addr := DefaultListenAddr
	if addr_ := os.Getenv("LISTEN_ADDR"); addr_ != "" {
		addr = addr_
	} else if port, err := strconv.Atoi(os.Getenv("PORT")); err == nil && port >= 0 {
		addr = fmt.Sprintf(":%d", port)
	}
	tcpAddr, err := net.ResolveTCPAddr("tcp", addr)
	if err != nil {
		panic(err)
	}

	l, err := net.ListenTCP("tcp", tcpAddr)
	if err != nil {
		panic(err)
	}

	svc = svc.Filter(networkFilter)
	downer := &httpdown.HTTP{
		StopTimeout: 20 * time.Second,
		KillTimeout: 25 * time.Second}
	server := downer.Serve(&http.Server{
		Handler:        httpHandler(svc),
		MaxHeaderBytes: http.DefaultMaxHeaderBytes}, l)
	log.Info(nil, "Listening on %v", l.Addr())
	return listener{
		Server: server,
		addr:   l.Addr()}
}
{'fraction_non_alphanumeric': 0.159189099368561, 'fraction_numerical': 0.006646726487205052, 'mean_word_length': 3.6450617283950617, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 5, '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': '7002666', 'n_tokens_mistral': 1197, 'n_tokens_neox': 1135, 'n_words': 299}