<commit_before>package cerebrum

import (
	"crypto/tls"
	"fmt"
	"net"
	"sync"
	"time"
)

// RaftLayer implements the raft.StreamLayer interface,
// so that we can use a single RPC layer for Raft and Cerebrum
type RaftLayer struct {
	// Addr is the listener address to return
	addr net.Addr

	// connCh is used to accept connections
	connCh chan net.Conn

	// config for TLS connections
	config *tls.Config

	// pool is a connection pool
	// pool *ConnPool
	dialer Dialer

	// Tracks if we are closed
	closed    bool
	closeCh   chan struct{}
	closeLock sync.Mutex
}

// NewRaftLayer is used to initialize a new RaftLayer which can
// be used as a StreamLayer for Raft. If a tlsConfig is provided,
// then the connection will use TLS.
func NewRaftLayer(dialer Dialer, addr net.Addr, config *tls.Config) *RaftLayer {
	layer := &RaftLayer{
		dialer:  dialer,
		addr:    addr,
		connCh:  make(chan net.Conn),
		closeCh: make(chan struct{}),
	}
	return layer
}

// Handoff is used to hand off a connection to the
// RaftLayer. This allows it to be Accept()'ed
func (l *RaftLayer) Handoff(c net.Conn) error {
	select {
	case l.connCh <- c:
		return nil
	case <-l.closeCh:
		return fmt.Errorf("Raft RPC layer closed")
	}
}

// Accept is used to return connection which are<commit_msg>Update RaftLayer to implement yamuxer.Handler<commit_after>package cerebrum

import (
	"crypto/tls"
	"fmt"
	"net"
	"sync"
	"time"

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

// RaftLayer implements the raft.StreamLayer interface,
// so that we can use a single RPC layer for Raft and Cerebrum
type RaftLayer struct {
	// Addr is the listener address to return
	addr net.Addr

	// connCh is used to accept connections
	connCh chan net.Conn

	// config for TLS connections
	config *tls.Config

	// pool is a connection pool
	// pool *ConnPool
	dialer Dialer

	// Tracks if we are closed
	closed    bool
	closeCh   chan struct{}
	closeLock sync.Mutex
}

// NewRaftLayer is used to initialize a new RaftLayer which can
// be used as a StreamLayer for Raft. If a tlsConfig is provided,
// then the connection will use TLS.
func NewRaftLayer(dialer Dialer, addr net.Addr, config *tls.Config) *RaftLayer {
	layer := &RaftLayer{
		dialer:  dialer,
		addr:    addr,
		connCh:  make(chan net.Conn),
		closeCh: make(chan struct{}),
	}
	return layer
}

// Handoff is used to hand off a connection to the
// RaftLayer. This allows it to be Accept()'ed
func (l *RaftLayer) Handoff(c net.Conn) error {
	select {
	case l.connCh <- c:
		return nil
	case <-l.closeCh:
		return fmt.Errorf("Raft RPC layer closed")
	}
}

func (l *RaftLayer) Handle(c context.Context, conn net.Conn) {
	select {
	case <-c.Done():
		conn.Close()
		return
	case <-l.closeCh:
		conn.Close()
		return
	case l.connCh <- conn:
		return
	}
}

// Accept is used to return connection which are
{'fraction_non_alphanumeric': 0.09644128113879004, 'fraction_numerical': 0.0, 'mean_word_length': 3.285060975609756, 'pattern_counts': {'":': 0, '<': 10, '<?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': '5474785', 'n_tokens_mistral': 1010, 'n_tokens_neox': 960, 'n_words': 360}