<commit_before>/*
 * Minimalist Object Storage, (C) 2015 Minio, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package minhttp

import (
	"net"
	"sync"

	"github.com/minio/minio/pkg/iodine"
)

// rateLimitedListener returns a Listener that accepts at most n simultaneous
// connections from the provided Listener.
func rateLimitedListener(l net.Listener, nconn int) net.Listener {
	return &rateLimitListener{l, make(chan struct{}, nconn)}
}

type rateLimitListener struct {
	net.Listener
	sem chan struct{}
}

func (l *rateLimitListener) accept()  { l.sem <- struct{}{} }
func (l *rateLimitListener) release() { <-l.sem }

func (l *rateLimitListener) Accept() (net.Conn, error) {
	l.accept()

	c, err := l.Listener.Accept()
	if err != nil {
		l.release()
		return nil, iodine.New(err, nil)
	}
	return &rateLimitListenerConn{Conn: c, release: l.release}, nil
}

type rateLimitListenerConn struct {
	net.Conn
	releaseOnce sync.Once
	release     func()
}

func (l *rateLimitListenerConn) Close() error {
	err := l.Conn.Close()
	l.releaseOnce.Do(l.release)
	return iodine.New(err, nil)
}<commit_msg>Add File() method to Ratelimitlistener for extracting underlying fd()<commit_after>/*
 * Minimalist Object Storage, (C) 2015 Minio, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package minhttp

import (
	"net"
	"os"
	"sync"

	"github.com/minio/minio/pkg/iodine"
)

// rateLimitedListener returns a Listener that accepts at most n simultaneous
// connections from the provided Listener.
func rateLimitedListener(l net.Listener, nconn int) net.Listener {
	return &rateLimitListener{l, make(chan struct{}, nconn)}
}

type rateLimitListener struct {
	net.Listener
	sem chan struct{}
}

func (l *rateLimitListener) accept()  { l.sem <- struct{}{} }
func (l *rateLimitListener) release() { <-l.sem }

// File - necessary to expose underlying socket fd
func (l *rateLimitListener) File() (f *os.File, err error) {
	return l.Listener.(fileListener).File()
}

// Accept - accept method for accepting new connections
func (l *rateLimitListener) Accept() (net.Conn, error) {
	l.accept()

	c, err := l.Listener.Accept()
	if err != nil {
		l.release()
		return nil, iodine.New(err, nil)
	}
	return &rateLimitListenerConn{Conn: c, release: l.release}, nil
}

type rateLimitListenerConn struct {
	net.Conn
	releaseOnce sync.Once
	release     func()
}

func (l *rateLimitListenerConn) Close() error {
	err := l.Conn.Close()
	l.releaseOnce.Do(l.release)
	return iodine.New(err, nil)
}
{'fraction_non_alphanumeric': 0.11209355390758699, 'fraction_numerical': 0.004563605248146035, 'mean_word_length': 4.104803493449782, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5690460', 'n_tokens_mistral': 1097, 'n_tokens_neox': 1060, 'n_words': 391}