ericp
ericp

Reputation: 611

Limit netty4 tcp endpoint to a single instance

I have a mock endpoint at 8001 that will echo anything provided to it. I have an http endpoint that will submit the end of the URL to the mock endpoint, and provide a response from the endpoint's response. That works fine.

The challenge is, I want the http route to use only 1 tcp connection to the 8001 endpoint.

I created a worker group as explained elsewhere, and set the worker count to 1. Looking through the source code, I'm thinking this approach should work.

However, when I do this bash command:

for a in {1..5}; do curl "http://localhost:8080/upstream/REQUESTNUM$a"  > $a.txt &  done;

I see multiple connections to 8001. I would have expected the http endpoint requests would have to share a single pool worker, but that doesn't seem to be the case.

Perhaps I am missing something, or perhaps there is another way to accomplish my goal of using only 1 back-end tcp connection for all the http requests.

How do I accomplish it?

<?xml version="1.0" encoding="UTF-8"?>                                                                                                                                                                             
<beans xmlns="http://www.springframework.org/schema/beans"                                                                                                                                                         
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"                                                                                                                                                          
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd                                                                                   
    http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">                                                                                                                 
    <camelContext                                                                                                                                                                                                  
        xmlns="http://camel.apache.org/schema/spring">                                                                                                                                                             

        <route id="mockUpstream">                                                                                                                                                                                  
            <from                                                                                                                                                                                                  
                uri="netty4:tcp://localhost:8001?sync=true&amp;textline=true&amp;keepAlive=true&amp;disconnect=false&amp;reuseChannel=true" />                                                                     
            <log message="Incoming to upstream: ${body}" />                                                                                                                                                        
            <transform>                                                                                                                                                                                            
                <simple>${body}</simple>                                                                                                                                                                           
            </transform>                                                                                                                                                                                           
        </route>                                                                                                                                                                                                   

        <route id="httpServer">                                                                                                                                                                                    
            <from                                                                                                                                                                                                  
                uri="netty4-http:http://0.0.0.0:8080/upstream?matchOnUriPrefix=true" />                                                                                                                            
            <!-- optional just use CamelHttpQuery from header, for full query -->                                                                                                                                  
            <log                                                                                                                                                                                                   
                message="Incoming http command: ${in.headers[CamelHttpPath]}" />                                                                                                                                   
            <transform>                                                                                                                                                                                            
                <simple>${in.headers[CamelHttpPath]}</simple>                                                                                                                                                      
            </transform>                                                                                                                                                                                           
            <to                                                                                                                                                                                                    
                uri="netty4:tcp://localhost:8001?workerGroup=#sharedPool&amp;sync=true&amp;textline=true&amp;keepAlive=true&amp;disconnect=false&amp;reuseChannel=true" />                                         
            <transform>                                                                                                                                                                                            
                <simple>${body}</simple>                                                                                                                                                                           
            </transform>                                                                                                                                                                                           
        </route>                                                                                                                                                                                                   

    </camelContext>                                                                                                                                                                                                

    <bean id="poolBuilder"                                                                                                                                                                                         
        class="org.apache.camel.component.netty4.NettyWorkerPoolBuilder">                                                                                                                                          
        <property name="workerCount" value="1" />                                                                                                                                                                  
    </bean>                                                                                                                                                                                                        

    <bean id="sharedPool" class="io.netty.channel.EventLoopGroup"                                                                                                                                                  
        factory-bean="poolBuilder" factory-method="build"                                                                                                                                                          
        destroy-method="shutdown">                                                                                                                                                                                 
    </bean>                                                                                                                                                                                                        
</beans>  

Upvotes: 0

Views: 413

Answers (1)

ericp
ericp

Reputation: 611

Looking at the logs, with TRACE level logging, I saw the NettyProducer's pool was indeed set to use 1 max active connection, but the NettyProducer was allowed 100 idle connections. I changed the following line and it is now behaving as expected:

   <to                                                                 
        uri="netty4:tcp://localhost:8001?workerGroup=#sharedPool&amp;sync=true&amp;textline=true&amp;keepAlive=true&amp;disconnect=false&amp;reuseChannel=true&amp;producerPoolMaxActive=1&amp;producerPoolMaxIdle=1" />

I assumed the "producer" settings were only good for the producer side (netty in mock host route) of the connection, but it looks like they can be used by the consumer end (netty in http route), too.

edit: I confused the terms producer and consumer and got that backwards above. Seeing that the "to" element is producing a request for something to consume, the producer* parameters make sense for (netty in http route). The (netty in mock host route) is the consumer of requests.

Upvotes: 1

Related Questions