<commit_before>package gosock

import (
	"io"
	"net/http"
	"time"

	"github.com/gorilla/websocket"
	"github.com/rsms/gotalk"
)

type rwc struct {
	reader io.Reader
	conn   *websocket.Conn
}

func (c *rwc) Write(p []byte) (int, error) {
	err := c.conn.WriteMessage(websocket.BinaryMessage, p)
	if err != nil {
		return 0, err
	}
	return len(p), nil
}

func (c *rwc) Read(p []byte) (int, error) {
	for {
		if c.reader == nil {
			// Advance to next message.
			var err error
			_, c.reader, err = c.conn.NextReader()
			if err != nil {
				return 0, err
			}
		}
		n, err := c.reader.Read(p)
		if err == io.EOF {
			// At end of message.
			c.reader = nil
			if n > 0 {
				return n, nil
			}
			// No data read, continue to next message.
			continue
		}
		return n, err
	}
}

func (c *rwc) Close() error {
	return c.conn.Close()
}

type WebSocketServer struct {
	limits   gotalk.Limits
	handlers *gotalk.Handlers

	onAccept gotalk.SockHandler

	// Template value for accepted sockets. Defaults to 0 (no automatic heartbeats)
	heartbeatInterval time.Duration

	// Template value for accepted sockets. Defaults to nil
	onHeartbeat func(load int, t time.Time)

	upgrader websocket.Upgrader
}

type WebSocketServerOptions struct {
	Limits            gotalk.Limits
	OnAccept          gotalk.SockHandler
	HeartbeatInterval time.Duration
	OnHeartbeat       func(load int, t time.Time)
	Upgrader          websocket.Upgrader
}

func DefaultWebSocketServerOptions() WebSocketServerOptions {
	return WebSocketServerOptions{
		Limits: gotalk.NewLimits(^uint32(0), ^uint32(0)),
		Upgrader: websocket.Upgrader{
			EnableCompression: true,
			HandshakeTimeout:  8 * time.Second,
			ReadBufferSize:    1024,
			WriteBufferSize:   1024,
		},
	}
}

func NewWebSocketServer(handlers *gotalk.Handlers) *WebSocketServer {
	opts := DefaultWebSocketServerOptions()
	return NewWebSocketServerWithOptions(handlers, &opts)
}

func NewWebSocketServerWithOptions(handlers *gotalk.Handlers, opts *WebSocketServerOptions) *WebSocketServer {
	ws := &WebSocketServer{
		limits:            opts.Limits,
		handlers:          handlers,<commit_msg>add: default error logging to websocket<commit_after>package gosock

import (
	"io"
	"net/http"
	"time"

	"github.com/gorilla/websocket"
	"github.com/prasannavl/go-gluons/log"
	"github.com/rsms/gotalk"
)

type rwc struct {
	reader io.Reader
	conn   *websocket.Conn
}

func (c *rwc) Write(p []byte) (int, error) {
	err := c.conn.WriteMessage(websocket.BinaryMessage, p)
	if err != nil {
		return 0, err
	}
	return len(p), nil
}

func (c *rwc) Read(p []byte) (int, error) {
	for {
		if c.reader == nil {
			// Advance to next message.
			var err error
			_, c.reader, err = c.conn.NextReader()
			if err != nil {
				return 0, err
			}
		}
		n, err := c.reader.Read(p)
		if err == io.EOF {
			// At end of message.
			c.reader = nil
			if n > 0 {
				return n, nil
			}
			// No data read, continue to next message.
			continue
		}
		return n, err
	}
}

func (c *rwc) Close() error {
	return c.conn.Close()
}

type WebSocketServer struct {
	limits   gotalk.Limits
	handlers *gotalk.Handlers

	onAccept gotalk.SockHandler

	// Template value for accepted sockets. Defaults to 0 (no automatic heartbeats)
	heartbeatInterval time.Duration

	// Template value for accepted sockets. Defaults to nil
	onHeartbeat func(load int, t time.Time)

	upgrader websocket.Upgrader
}

type WebSocketServerOptions struct {
	Limits            gotalk.Limits
	OnAccept          gotalk.SockHandler
	HeartbeatInterval time.Duration
	OnHeartbeat       func(load int, t time.Time)
	Upgrader          websocket.Upgrader
}

func DefaultWebSocketServerOptions() WebSocketServerOptions {
	return WebSocketServerOptions{
		Limits: gotalk.NewLimits(^uint32(0), ^uint32(0)),
		Upgrader: websocket.Upgrader{
			EnableCompression: true,
			HandshakeTimeout:  8 * time.Second,
			ReadBufferSize:    256,
			WriteBufferSize:   256,
			Error: func(w http.ResponseWriter, r *http.Request, status int, reason error) {
				log.Errorf("websocket: status: %v, %v", status, reason)
				w.WriteHeader(status)
			},
		},
	}
}

func NewWebSocketServer(handlers *gotalk.Handlers) *WebSocketServer {
	opts := DefaultWebSocketServerOptions()
	return NewWebSocketServerWithOptions(handlers, &opts)
}

func NewWebSocketServerWithOptions(handlers *gotalk.Handlers, opts *WebSocketServerOptions) *WebSocketServer {
	ws := &WebSocketServer{
		limits:            opts.Limits,
		handlers:          handlers,
{'fraction_non_alphanumeric': 0.10739910313901345, 'fraction_numerical': 0.008071748878923767, 'mean_word_length': 3.3950738916256156, 'pattern_counts': {'":': 0, '<': 3, '<?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': '3915698', 'n_tokens_mistral': 1702, 'n_tokens_neox': 1540, 'n_words': 401}