<commit_before>package main

import (
	"net"
	"net/http"
	"strconv"
	"sync"

	"golang.org/x/net/websocket"
)

func websocketHTTPHandler() {
	http.Handle("/", websocket.Handler(websocketHandler))
}

func websocketHandler(ws *websocket.Conn) {
	client := NewClient()

	remoteAddr, remotePort, _ := net.SplitHostPort(ws.Request().RemoteAddr)
	client.remoteAddr = remoteAddr
	client.remotePort, _ = strconv.Atoi(remotePort)

	clientHostnames, err := net.LookupAddr(client.remoteAddr)
	if err != nil {
		client.remoteHostname = client.remoteAddr
	} else {
		client.remoteHostname = clientHostnames[0]
	}

	client.Log(2, "New client from %s %s", client.remoteAddr, client.remoteHostname)

	// We wait until the client send queue has been drained
	var sendDrained sync.WaitGroup
	sendDrained.Add(1)

	// Read from websocket
	go func() {
		for {
			r := make([]byte, 1024)
			len, err := ws.Read(r)
			if err == nil && len > 0 {
				client.Log(1, "client->: %s", string(r))
				select {
				case client.Recv <- string(r):
				default:
					client.Log(3, "Recv queue full. Dropping data")
					// TODO: Should this really just drop the data or close the connection?
				}

			} else if err != nil {
				client.Log(1, "Websocket read error: %s", err.Error())
				break

			} else if len == 0 {
				client.Log(1, "Got 0 bytes from websocket")
			}
		}

		close(client.Recv)
		client.signalClose <- "client_closed"
	}()

	// Write to websocket
	go func() {
		for {
			line, ok := <-client.Send
			if !ok {
				sendDrained.Done()
				break
			}
<commit_msg>Reading correct data from the websocket<commit_after>package main

import (
	"net"
	"net/http"
	"strconv"
	"sync"

	"golang.org/x/net/websocket"
)

func websocketHTTPHandler() {
	http.Handle("/", websocket.Handler(websocketHandler))
}

func websocketHandler(ws *websocket.Conn) {
	client := NewClient()

	remoteAddr, remotePort, _ := net.SplitHostPort(ws.Request().RemoteAddr)
	client.remoteAddr = remoteAddr
	client.remotePort, _ = strconv.Atoi(remotePort)

	clientHostnames, err := net.LookupAddr(client.remoteAddr)
	if err != nil {
		client.remoteHostname = client.remoteAddr
	} else {
		client.remoteHostname = clientHostnames[0]
	}

	client.Log(2, "New client from %s %s", client.remoteAddr, client.remoteHostname)

	// We wait until the client send queue has been drained
	var sendDrained sync.WaitGroup
	sendDrained.Add(1)

	// Read from websocket
	go func() {
		for {
			r := make([]byte, 1024)
			len, err := ws.Read(r)
			if err == nil && len > 0 {
				message := string(r[:len])
				client.Log(1, "client->: %s", message)
				select {
				case client.Recv <- message:
				default:
					client.Log(3, "Recv queue full. Dropping data")
					// TODO: Should this really just drop the data or close the connection?
				}

			} else if err != nil {
				client.Log(1, "Websocket read error: %s", err.Error())
				break

			} else if len == 0 {
				client.Log(1, "Got 0 bytes from websocket")
			}
		}

		close(client.Recv)
		client.signalClose <- "client_closed"
	}()

	// Write to websocket
	go func() {
		for {
			line, ok := <-client.Send
			if !ok {
				sendDrained.Done()
				break
			}

{'fraction_non_alphanumeric': 0.1378541865647883, 'fraction_numerical': 0.008914358484559057, 'mean_word_length': 3.2516914749661705, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, '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': '4702331', 'n_tokens_mistral': 1275, 'n_tokens_neox': 1088, 'n_words': 283}