Google Cloud Run for TCPListener clients?

Viewed 794

I currently have a C# application running as a windows service. The app starts up a TCPListener, then in a while loop grabs the clients and immediately throws them to a ThreadPool.UnsafeQueueUserWorkItem to do all the actual work. The client is closed at the end of the UnsageQueueUserWorkItem call. The basic code is below:

var server = new TcpListener(ip, port);
server.Start();

while (true)
{
   try
   {
      TcpClient client = await server.AcceptTcpClientAsync();
      var cw = new TcpClientService(logger, client, parser, dataRepo, propertyRecordDefinitions, 
             vimAlertsSent, reservations, emailClient);

      ThreadPool.UnsafeQueueUserWorkItem(x => ((TcpClientService)x).Run(), cw);
    }
    catch(Exception iex)
    {     
       //DO SOME LOGGING    
    }
    finally
    {               
    }
}

This all runs as a windows service on a VM in AWS. Im wondering if this is a good candidate for Google Cloud Run (or any other serverless/stateless function). I am getting hundreds of requests (clients) a minute and am hoping to scale to thousands. As I understand it, Cloud Run could potentially be triggered by the incoming request, then I could just run my TcpClientService code that is currently being called by the Threadpool.UnsafeQueueUserWorkItem. Is this a good implementation? Is this what Google Cloud Run is optimized for? I am wondering if I will see some degradation in that DB connections may not be pooled, and some other structures I share across the threads (all my inputs to the TcpClientService call) would have to be newed up each function call. Thoughts?

1 Answers

Cloud Run only supports HTTP/1 or HTTP/2 (includes gRPC) over TLS (though your application doesn't need to worry about terminating TLS). Therefore, arbitrary TCP protocols that aren't one of these are not supported. See: Which protocols can my application serve traffic on?

Cloud Run container instances don't receive individual TCP connections from clients. It rather receives plain unencrypted "HTTP requests" from its load balancer, which terminates HTTPS requests. So the Cloud Run container instances do not get a direct TCP connection from the client.

Other than that, the use you described below is definitely what Cloud Run is intended for:

I am getting hundreds of requests (clients) a minute and am hoping to scale to thousands.

Related