<commit_before>/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you 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.
 *
 * Contains some contributions under the Thrift Software License.
 * Please see doc/old-thrift-license.txt in the Thrift distribution for
 * details.
 */

using System;
using System.Threading;
using Thrift.Protocol;
using Thrift.Transport;

namespace Thrift.Server
{
  /// <summary>
  /// Server that uses C# built-in ThreadPool to spawn threads when handling requests
  /// </summary>
  public class TThreadPoolServer : TServer
  {
    private const int DEFAULT_MIN_THREADS = 10;
    private const int DEFAULT_MAX_THREADS = 100;
    private volatile bool stop = false;

    public TThreadPoolServer(TProcessor processor, TServerTransport serverTransport)
        : this(new TSingletonProcessorFactory(processor), serverTransport,
         new TTransportFactory(), new TTransportFactory(),
         new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(),
         DEFAULT_MIN_THREADS, DEFAULT_MAX_THREADS, DefaultLogDelegate)
    {
    }

    public TThreadPoolServer(TProcessor processor, TServerTransport serverTransport, LogDelegate logDelegate)
        : this(new TSingletonProcessorFactory(processor), serverTransport,
         new TTransportFactory(), new TTransportFactory(),
         new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(),
         DEFAULT_MIN_THREADS, DEFAULT_MAX_THREADS, logDelegate)
    {
    }

    public TThreadPoolServer(TProcessor processor,
     TServerTransport serverTransport,
     TTransportFactory transportFactory,
     TProtocolFactory protocolFactory)
        : this(new TSingletonProcessorFactory(processor), serverTransport,
           transportFactory, transportFactory,
           protocolFactory, protocolFactory,
           DEFAULT_MIN_THREADS, DEFAULT_MAX_THREADS, DefaultLogDelegate)
    {
    }

    public TThreadPoolServer(TProcessorFactory processorFactory,
                 TServerTransport serverTransport,
                 TTransportFactory transportFactory,
                 TProtocolFactory protocolFactory)
        : this(processorFactory, serverTransport,
         transportFactory, transportFactory,
         protocolFactory, protocolFactory,
         DEFAULT_MIN_THREADS, DEFAULT_MAX_THREADS, DefaultLogDelegate)
    {
    }

    public TThreadPoolServer(TProcessorFactory processorFactory,
                 TServerTransport serverTransport,
                 TTransportFactory inputTransportFactory,
                 TTransportFactory outputTransportFactory,
                 TProtocolFactory inputProtocolFactory,
                 TProtocolFactory outputProtocolFactory,
                 int minThreadPoolThreads, int maxThreadPoolThreads, LogDelegate logDel)
        : base(processorFactory, serverTransport, inputTransportFactory, outputTransportFactory,
          inputProtocolFactory, outputProtocolFactory, logDel)
    {
      lock (typeof(TThreadPoolServer))
      {
        if(maxThreadPoolThreads > 0)
        {
          if (!ThreadPool.SetMaxThreads(maxThreadPoolThreads, maxThreadPoolThreads))
          {
            throw new Exception("Error: could not SetMaxThreads in ThreadPool");
          }
        }
        if (minThreadPoolThreads > 0)
        {
          if (!ThreadPool.SetMinThreads(minThreadPoolThreads, minThreadPoolThreads))
          {
            throw new Exception("Error: could not SetMinThreads in ThreadPool");
          }
        }
      }
    }


    /// <summary>
    /// Use new ThreadPool thread for each new client connection
    /// </summary>
    public override void Serve()
    {
      try
      {
        serverTransport.Listen();
      }
      catch (TTransportException ttx)
      {
        logDelegate("Error, could not listen on ServerTransport: " + ttx);
        return;<commit_msg>THRIFT-4190 Improve C# TThreadPoolServer defaults (part 2 of 2) Client: C# Patch: <NAME><commit_after>/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you 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.
 *
 * Contains some contributions under the Thrift Software License.
 * Please see doc/old-thrift-license.txt in the Thrift distribution for
 * details.
 */

using System;
using System.Threading;
using Thrift.Protocol;
using Thrift.Transport;

namespace Thrift.Server
{
  /// <summary>
  /// Server that uses C# built-in ThreadPool to spawn threads when handling requests
  /// </summary>
  public class TThreadPoolServer : TServer
  {
    private const int DEFAULT_MIN_THREADS = -1;  // use .NET ThreadPool defaults
    private const int DEFAULT_MAX_THREADS = -1;  // use .NET ThreadPool defaults
    private volatile bool stop = false;

    public struct Configuration
    {
      public int MinWorkerThreads;
      public int MaxWorkerThreads;
      public int MinIOThreads;
      public int MaxIOThreads;

      public Configuration(int min = DEFAULT_MIN_THREADS, int max = DEFAULT_MAX_THREADS)
      {
        MinWorkerThreads = min;
        MaxWorkerThreads = max;
        MinIOThreads = min;
        MaxIOThreads = max;
      }

      public Configuration(int minWork, int maxWork, int minIO, int maxIO)
      {
        MinWorkerThreads = minWork;
        MaxWorkerThreads = maxWork;
        MinIOThreads = minIO;
        MaxIOThreads = maxIO;
      }
    }

    public TThreadPoolServer(TProcessor processor, TServerTransport serverTransport)
        : this(new TSingletonProcessorFactory(processor), serverTransport,
         new TTransportFactory(), new TTransportFactory(),
         new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(),
         new Configuration(), DefaultLogDelegate)
    {
    }

    public TThreadPoolServer(TProcessor processor, TServerTransport serverTransport, LogDelegate logDelegate)
        : this(new TSingletonProcessorFactory(processor), serverTransport,
         new TTransportFactory(), new TTransportFactory(),
         new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(),
         new Configuration(), logDelegate)
    {
    }

    public TThreadPoolServer(TProcessor processor,
     TServerTransport serverTransport,
     TTransportFactory transportFactory,
     TProtocolFactory protocolFactory)
        : this(new TSingletonProcessorFactory(processor), serverTransport,
           transportFactory, transportFactory,
           protocolFactory, protocolFactory,
           new Configuration(), DefaultLogDelegate)
    {
    }

    public TThreadPoolServer(TProcessorFactory processorFactory,
                 TServerTransport serverTransport,
                 TTransportFactory transportFactory,
                 TProtocolFactory protocolFactory)
        : this(processorFactory, serverTransport,
         transportFactory, transportFactory,
         protocolFactory, protocolFactory,
         new Configuration(), DefaultLogDelegate)
    {
    }

    public TThreadPoolServer(TProcessorFactory processorFactory,
                 TServerTransport serverTransport,
                 TTransportFactory inputTransportFactory,
                 TTransportFactory outputTransportFactory,
                 TProtocolFactory inputProtocolFactory,
                 TProtocolFactory outputProtocolFactory,
                 int minThreadPoolThreads, int maxThreadPoolThreads, LogDelegate logDel)
        : this(processorFactory, serverTransport, inputTransportFactory, outputTransportFactory,
         inputProtocolFactory, outputProtocolFactory,
         new Configuration(minThreadPoolThreads, maxThreadPoolThreads),
         logDel)
    {
    }

    public TThreadPoolServer(TProcessorFactory processorFactory,
                 TServerTransport serverTransport,
                 TTransportFactory inputTransportFactory,
                 TTransportFactory outputTransportFactory,
                 TProtocolFactory inputProtocolFactory,
                 TProtocolFactory outputProtocolFactory,
                 Configuration threadConfig,
                 LogDelegate logDel)
        : base(processorFactory, serverTransport, inputTransportFactory, outputTransportFactory,
        inputProtocolFactory, outputProtocolFactory, logDel)
    {
      lock (typeof(TThreadPoolServer))
      {
        if ((threadConfig.MaxWorkerThreads > 0) || (threadConfig.MaxIOThreads > 0))
        {
          int work, comm;
          ThreadPool.GetMaxThreads(out work, out comm);
          if (threadConfig.MaxWorkerThreads > 0)
            work = threadConfig.MaxWorkerThreads;
          if (threadConfig.MaxIOThreads > 0)
            comm = threadConfig.MaxIOThreads;
          if (!ThreadPool.SetMaxThreads(work, comm))
            throw new Exception("Error: could not SetMaxThreads in ThreadPool");
        }

        if ((threadConfig.MinWorkerThreads > 0) || (threadConfig.MinIOThreads > 0))
        {
          int work, comm;
          ThreadPool.GetMinThreads(out work, out comm);
          if (threadConfig.MinWorkerThreads > 0)
            work = threadConfig.MinWorkerThreads;
          if (threadConfig.MinIOThreads > 0)
            comm = threadConfig.MinIOThreads;
          if (!ThreadPool.SetMinThreads(work, comm))
            throw new Exception("Error: could not SetMinThreads in ThreadPool");
        }
      }
    }


    /// <summary>
    /// Use new ThreadPool thread for each new client connection
    /// </summary>
    public override void Serve()
    {
      try
      {
        serverTransport.Listen();
      }
      catch (TTransportException ttx)
      {
        logDelegate("Error, could not listen on ServerTransport: " + ttx);
        return;
{'fraction_non_alphanumeric': 0.06586881052439247, 'fraction_numerical': 0.002832084779828248, 'mean_word_length': 2.875044247787611, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 22, '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': '6259891', 'n_tokens_mistral': 2635, 'n_tokens_neox': 2593, 'n_words': 798}