<commit_before>// DBPortPool.java

package ed.db;

import ed.util.*;

import java.io.*;
import java.net.*;
import java.util.*;

class DBPortPool extends SimplePool<DBPort> {

    static DBPortPool get( InetSocketAddress addr ){
        
        DBPortPool p = _pools.get( addr );
        
        if (p != null) 
            return p;
        
        synchronized (_pools) {
            p = _pools.get( addr );
            if (p != null) {
                return p;
            }
            
            p = new DBPortPool( addr );
            _pools.put( addr , p);
        }

        return p;
    }

    private static final Map<InetSocketAddress,DBPortPool> _pools = Collections.synchronizedMap( new HashMap<InetSocketAddress,DBPortPool>() );

    // ----
    

    DBPortPool( InetSocketAddress addr ){
        super( addr.toString() , 10 , Bytes.CONNECTIONS_PER_HOST );
        _addr = addr;
    }

    void gotError(){
        System.out.println( "emptying DBPortPool b/c of error" );
        clear();
    }
    
    protected DBPort createNew(){
        try {
            DBPort p = new DBPort( _addr , this );
            _everWorked = true;
            return p;
        }
        catch ( IOException ioe ){
            throw new RuntimeException( "can't create port to:" + _addr , ioe );
        }
    }
    
    final InetSocketAddress _addr;
    boolean _everWorked = false;
}<commit_msg>some dbpool protection max wait time of a connection max number of threads waiting need more protection, but better than nothing<commit_after>// DBPortPool.java

package ed.db;

import ed.util.*;

import java.io.*;
import java.net.*;
import java.util.*;
import java.util.concurrent.*;

class DBPortPool extends SimplePool<DBPort> {

    public final long _maxWaitTime = 1000 * 60 * 2;

    static DBPortPool get( InetSocketAddress addr ){
        
        DBPortPool p = _pools.get( addr );
        
        if (p != null) 
            return p;
        
        synchronized (_pools) {
            p = _pools.get( addr );
            if (p != null) {
                return p;
            }
            
            p = new DBPortPool( addr );
            _pools.put( addr , p);
        }

        return p;
    }

    private static final Map<InetSocketAddress,DBPortPool> _pools = Collections.synchronizedMap( new HashMap<InetSocketAddress,DBPortPool>() );

    // ----
    
    public static class NoMoreConnection extends RuntimeException {
	NoMoreConnection(){
	    super( "No more DB Connections" );
	}
    }

    // ----

    DBPortPool( InetSocketAddress addr ){
        super( addr.toString() , 10 , Bytes.CONNECTIONS_PER_HOST );
        _addr = addr;
	_waitingSem = new Semaphore( Math.min( ed.net.httpserver.HttpServer.WORKER_THREAD_QUEUE_MAX / 2 , Bytes.CONNECTIONS_PER_HOST * 5 ) );
    }
    
    public DBPort get(){
	DBPort port = null;
	
	if ( ! _waitingSem.tryAcquire() )
	    throw new NoMoreConnection();

	try {
	    port = get( _maxWaitTime );
	}
	finally {
	    _waitingSem.release();
	}

	if ( port == null )
	    throw new NoMoreConnection();
	
	return port;
    }

    void gotError(){
        System.out.println( "emptying DBPortPool b/c of error" );
        clear();
    }
    
    protected DBPort createNew(){
        try {
            DBPort p = new DBPort( _addr , this );
            _everWorked = true;
            return p;
        }
        catch ( IOException ioe ){
            throw new RuntimeException( "can't create port to:" + _addr , ioe );
        }
    }

    final private Semaphore _waitingSem;
    final InetSocketAddress _addr;
    boolean _everWorked = false;
}
{'fraction_non_alphanumeric': 0.10287610619469026, 'fraction_numerical': 0.003595132743362832, 'mean_word_length': 1.8729150119142177, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '4529577', 'n_tokens_mistral': 1118, 'n_tokens_neox': 1097, 'n_words': 292}