tomcat connection-timeout连接超时源码解析

 更新时间:2023年11月24日 09:38:36   作者:codecraft  
这篇文章主要为大家介绍了tomcat connection-timeout连接超时源码解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

本文主要研究一下tomcat的connection-timeout

ServerProperties.Tomcat

org/springframework/boot/autoconfigure/web/ServerProperties.java

public static class Tomcat {
        /**
         * Access log configuration.
         */
        private final Accesslog accesslog = new Accesslog();
        /**
         * Thread related configuration.
         */
        private final Threads threads = new Threads();
        /**
         * Tomcat base directory. If not specified, a temporary directory is used.
         */
        private File basedir;
        /**
         * Delay between the invocation of backgroundProcess methods. If a duration suffix
         * is not specified, seconds will be used.
         */
        @DurationUnit(ChronoUnit.SECONDS)
        private Duration backgroundProcessorDelay = Duration.ofSeconds(10);
        /**
         * Maximum size of the form content in any HTTP post request.
         */
        private DataSize maxHttpFormPostSize = DataSize.ofMegabytes(2);
        /**
         * Maximum amount of request body to swallow.
         */
        private DataSize maxSwallowSize = DataSize.ofMegabytes(2);
        /**
         * Whether requests to the context root should be redirected by appending a / to
         * the path. When using SSL terminated at a proxy, this property should be set to
         * false.
         */
        private Boolean redirectContextRoot = true;
        /**
         * Whether HTTP 1.1 and later location headers generated by a call to sendRedirect
         * will use relative or absolute redirects.
         */
        private boolean useRelativeRedirects;
        /**
         * Character encoding to use to decode the URI.
         */
        private Charset uriEncoding = StandardCharsets.UTF_8;
        /**
         * Maximum number of connections that the server accepts and processes at any
         * given time. Once the limit has been reached, the operating system may still
         * accept connections based on the "acceptCount" property.
         */
        private int maxConnections = 8192;
        /**
         * Maximum queue length for incoming connection requests when all possible request
         * processing threads are in use.
         */
        private int acceptCount = 100;
        /**
         * Maximum number of idle processors that will be retained in the cache and reused
         * with a subsequent request. When set to -1 the cache will be unlimited with a
         * theoretical maximum size equal to the maximum number of connections.
         */
        private int processorCache = 200;
        /**
         * Comma-separated list of additional patterns that match jars to ignore for TLD
         * scanning. The special '?' and '*' characters can be used in the pattern to
         * match one and only one character and zero or more characters respectively.
         */
        private List<String> additionalTldSkipPatterns = new ArrayList<>();
        /**
         * Comma-separated list of additional unencoded characters that should be allowed
         * in URI paths. Only "< > [ \ ] ^ ` { | }" are allowed.
         */
        private List<Character> relaxedPathChars = new ArrayList<>();
        /**
         * Comma-separated list of additional unencoded characters that should be allowed
         * in URI query strings. Only "< > [ \ ] ^ ` { | }" are allowed.
         */
        private List<Character> relaxedQueryChars = new ArrayList<>();
        /**
         * Amount of time the connector will wait, after accepting a connection, for the
         * request URI line to be presented.
         */
        private Duration connectionTimeout;
        /**
         * Static resource configuration.
         */
        private final Resource resource = new Resource();
        /**
         * Modeler MBean Registry configuration.
         */
        private final Mbeanregistry mbeanregistry = new Mbeanregistry();
        /**
         * Remote Ip Valve configuration.
         */
        private final Remoteip remoteip = new Remoteip();
        //......
    }
springboot的ServerProperties.Tomcat定义了connectionTimeout属性,用于指定接受连接之后等待uri的时间

customizeConnectionTimeout

org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizer.java

private void customizeConnectionTimeout(ConfigurableTomcatWebServerFactory factory, Duration connectionTimeout) {
        factory.addConnectorCustomizers((connector) -> {
            ProtocolHandler handler = connector.getProtocolHandler();
            if (handler instanceof AbstractProtocol) {
                AbstractProtocol<?> protocol = (AbstractProtocol<?>) handler;
                protocol.setConnectionTimeout((int) connectionTimeout.toMillis());
            }
        });
    }
customizeConnectionTimeout将connectionTimeout写入到AbstractProtocol

AbstractProtocol

org/apache/coyote/AbstractProtocol.java

public void setConnectionTimeout(int timeout) {
        endpoint.setConnectionTimeout(timeout);
    }
AbstractProtocol将timeout设置到endpoint

AbstractEndpoint

org/apache/tomcat/util/net/AbstractEndpoint.java

public void setConnectionTimeout(int soTimeout) { 
        socketProperties.setSoTimeout(soTimeout); 
    }
    /**
     * Keepalive timeout, if not set the soTimeout is used.
     */
    private Integer keepAliveTimeout = null;
    public int getKeepAliveTimeout() {
        if (keepAliveTimeout == null) {
            return getConnectionTimeout();
        } else {
            return keepAliveTimeout.intValue();
        }
    }
AbstractEndpoint将timeout设置到socketProperties的soTimeout,另外它的getKeepAliveTimeout方法在keepAliveTimeout为null的时候,使用的是getConnectionTimeout

Http11Processor

org/apache/coyote/http11/Http11Processor.java

public SocketState service(SocketWrapperBase<?> socketWrapper)
        throws IOException {
        RequestInfo rp = request.getRequestProcessor();
        rp.setStage(org.apache.coyote.Constants.STAGE_PARSE);
        // Setting up the I/O
        setSocketWrapper(socketWrapper);
        // Flags
        keepAlive = true;
        openSocket = false;
        readComplete = true;
        boolean keptAlive = false;
        SendfileState sendfileState = SendfileState.DONE;
        while (!getErrorState().isError() && keepAlive && !isAsync() && upgradeToken == null &&
                sendfileState == SendfileState.DONE && !protocol.isPaused()) {
            // Parsing the request header
            try {
                if (!inputBuffer.parseRequestLine(keptAlive, protocol.getConnectionTimeout(),
                        protocol.getKeepAliveTimeout())) {
                    if (inputBuffer.getParsingRequestLinePhase() == -1) {
                        return SocketState.UPGRADING;
                    } else if (handleIncompleteRequestLineRead()) {
                        break;
                    }
                }
                //......
            }
            //......
        }
        //......
    }
Http11Processor的service方法在执行inputBuffer.parseRequestLine时传入了keptAlive、protocol.getConnectionTimeout()、protocol.getKeepAliveTimeout()参数

小结

springboot提供了tomcat的connection-timeout参数配置,其配置的是socket timeout,不过springboot没有提供对keepAliveTimeout的配置,它默认是null,读取的是connection timeout的配置。

以上就是tomcat connection-timeout连接超时源码解析的详细内容,更多关于tomcat connection-timeout连接超时的资料请关注脚本之家其它相关文章!

相关文章

  • Java从List中删除元素的几种方式小结

    Java从List中删除元素的几种方式小结

    在Java中,List 接口提供了一个 remove(Object o) 方法来移除列表中与给定对象相等的第一个元素,然而,直接使用这个方法来删除列表中的元素有时并不是最优的选择,主要原因包括效率和同步性问题,本文介绍了Java从List中删除元素的几种方式,需要的朋友可以参考下
    2024-08-08
  • 解决Spring Boot 多模块注入访问不到jar包中的Bean问题

    解决Spring Boot 多模块注入访问不到jar包中的Bean问题

    这篇文章主要介绍了解决Spring Boot 多模块注入访问不到jar包中的Bean问题。具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09
  • Java使用jmeter进行压力测试

    Java使用jmeter进行压力测试

    本篇文章简单讲一下使用jmeter进行压力测试。其压测思想就是 通过创建指定数量的线程,同时请求指定接口,来模拟指定数量用户同时进行某个操作的场景,感兴趣的小伙伴们可以参考一下
    2021-07-07
  • SpringBoot项目打包部署到Tomcat的操作流程

    SpringBoot项目打包部署到Tomcat的操作流程

    在最近一个项目中,维护行里一个年代较为久远的单体项目,需要将项目打包放到的tomcat服务器下运行,所以本文就给大家介绍一下SpringBoot项目打包部署到Tomcat的流程步骤,需要的朋友可以参考下
    2023-08-08
  • JAVA实现往字符串中某位置加入一个字符串

    JAVA实现往字符串中某位置加入一个字符串

    这篇文章主要介绍了JAVA实现往字符串中某位置加入一个字符串,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-08-08
  • IDEA 显示Run Dashboard窗口的2种方式(推荐)

    IDEA 显示Run Dashboard窗口的2种方式(推荐)

    这篇文章主要介绍了IDEA 显示Run Dashboard窗口的2种方式,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-08-08
  • Java虚拟机运行时栈的栈帧

    Java虚拟机运行时栈的栈帧

    本节将会介绍一下Java虚拟机栈中的栈帧,会对栈帧的组成部分(局部变量表、操作数栈、动态链接、方法出口)分别进行介绍,最后还会通过javap命令反解析编译后的.class文件,进行分析方法执行时的局部变量表、操作数栈等
    2021-09-09
  • 23种设计模式(18)java备忘录模式

    23种设计模式(18)java备忘录模式

    这篇文章主要为大家详细介绍了23种设计模式之java备忘录模式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-01-01
  • Visual Studio Code配置Tomcat运行Java Web项目详细步骤

    Visual Studio Code配置Tomcat运行Java Web项目详细步骤

    VS Code是一款非常棒的文本编辑器,具有配置简单、功能丰富、轻量简洁的特点,并且极其适合处理中小规模的代码,这篇文章主要给大家介绍了关于Visual Studio Code配置Tomcat运行Java Web项目的详细步骤,需要的朋友可以参考下
    2023-11-11
  • SpringBoot自定义RestTemplate的拦截器链的实战指南

    SpringBoot自定义RestTemplate的拦截器链的实战指南

    在项目开发中,RestTemplate作为Spring提供的HTTP客户端工具,经常用于访问内部或三方服务,但在实际项目中,我们往往需要对请求进行统一处理,所以本文给大家介绍了SpringBoot自定义RestTemplate的拦截器链的实战指南,需要的朋友可以参考下
    2025-07-07

最新评论