annotate src/org/eclipse/jetty/server/nio/SelectChannelConnector.java @ 949:e9088af3787f

remove SelectSet._changes
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 11 Oct 2016 23:18:13 -0600
parents f5aefdc4a81a
children a778413aefc0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 //
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 // ========================================================================
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 // ------------------------------------------------------------------------
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 // All rights reserved. This program and the accompanying materials
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 // are made available under the terms of the Eclipse Public License v1.0
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 // and Apache License v2.0 which accompanies this distribution.
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 //
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 // The Eclipse Public License is available at
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 // http://www.eclipse.org/legal/epl-v10.html
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 //
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 // The Apache License v2.0 is available at
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 // http://www.opensource.org/licenses/apache2.0.php
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 //
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 // You may elect to redistribute this code under either of these licenses.
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 // ========================================================================
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 //
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 package org.eclipse.jetty.server.nio;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 import java.io.IOException;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 import java.net.InetSocketAddress;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 import java.net.Socket;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 import java.nio.channels.SelectionKey;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 import java.nio.channels.ServerSocketChannel;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 import java.nio.channels.SocketChannel;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 import org.eclipse.jetty.io.AsyncEndPoint;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 import org.eclipse.jetty.io.ConnectedEndPoint;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 import org.eclipse.jetty.io.Connection;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 import org.eclipse.jetty.io.EndPoint;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 import org.eclipse.jetty.io.nio.AsyncConnection;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 import org.eclipse.jetty.io.nio.SelectChannelEndPoint;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 import org.eclipse.jetty.io.nio.SelectorManager;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 import org.eclipse.jetty.io.nio.SelectorManager.SelectSet;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 import org.eclipse.jetty.server.AsyncHttpConnection;
907
bc4e299de953 remove AbstractNIOConnector
Franklin Schmidt <fschmidt@gmail.com>
parents: 906
diff changeset
37 import org.eclipse.jetty.server.Connector;
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 import org.eclipse.jetty.server.Request;
887
df84a1741687 make Connector reference to server explicit
Franklin Schmidt <fschmidt@gmail.com>
parents: 865
diff changeset
39 import org.eclipse.jetty.server.Server;
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 /* ------------------------------------------------------------------------------- */
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 /**
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 * Selecting NIO connector.
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 * <p>
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 * This connector uses efficient NIO buffers with a non blocking threading model. Direct NIO buffers
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 * are used and threads are only allocated to connections with requests. Synchronization is used to
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 * simulate blocking for the servlet API, and any unflushed content at the end of request handling
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 * is written asynchronously.
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 * </p>
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 * <p>
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 * This connector is best used when there are a many connections that have idle periods.
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 * </p>
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 *
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 * @org.apache.xbean.XBean element="nioConnector" description="Creates an NIO based socket connector"
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 */
907
bc4e299de953 remove AbstractNIOConnector
Franklin Schmidt <fschmidt@gmail.com>
parents: 906
diff changeset
56 public class SelectChannelConnector extends Connector
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 {
864
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
58 private final SelectorManager _manager = new ConnectorSelectorManager();
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59
887
df84a1741687 make Connector reference to server explicit
Franklin Schmidt <fschmidt@gmail.com>
parents: 865
diff changeset
60 public SelectChannelConnector(Server server,int port)
864
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
61 {
887
df84a1741687 make Connector reference to server explicit
Franklin Schmidt <fschmidt@gmail.com>
parents: 865
diff changeset
62 super(server,port);
864
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
63 _manager.setMaxIdleTime(getMaxIdleTime());
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
64 addBean(_manager,true);
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
65 setAcceptors(Math.max(1,(Runtime.getRuntime().availableProcessors()+3)/4));
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
66 }
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
67
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
68 @Override
906
Franklin Schmidt <fschmidt@gmail.com>
parents: 903
diff changeset
69 public final void accept() throws IOException
864
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
70 {
906
Franklin Schmidt <fschmidt@gmail.com>
parents: 903
diff changeset
71 ServerSocketChannel server = _acceptChannel;
864
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
72
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
73 if (server!=null && server.isOpen() && _manager.isStarted())
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
74 {
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
75 SocketChannel channel = server.accept();
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
76 channel.configureBlocking(false);
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
77 Socket socket = channel.socket();
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
78 configure(socket);
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
79 _manager.register(channel);
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
80 }
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
81 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82
864
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
83 @Override
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
84 public void customize(EndPoint endpoint, Request request) throws IOException
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
85 {
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
86 request.setTimeStamp(System.currentTimeMillis());
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
87 endpoint.setMaxIdleTime(_maxIdleTime);
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
88 super.customize(endpoint, request);
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
89 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
90
864
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
91 @Override
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
92 public void persist(EndPoint endpoint) throws IOException
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
93 {
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
94 AsyncEndPoint aEndp = ((AsyncEndPoint)endpoint);
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
95 aEndp.setCheckForIdle(true);
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
96 super.persist(endpoint);
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
97 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
98
864
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
99 @Override
909
c60c1adfac3e simplify connectors
Franklin Schmidt <fschmidt@gmail.com>
parents: 907
diff changeset
100 protected synchronized void doStart() throws Exception
864
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
101 {
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
102 _manager.setSelectSets(getAcceptors());
948
f5aefdc4a81a simplify SelectChannelConnector
Franklin Schmidt <fschmidt@gmail.com>
parents: 937
diff changeset
103 // _manager.setMaxIdleTime(getMaxIdleTime());
913
17f4fe8271de simplify connectors
Franklin Schmidt <fschmidt@gmail.com>
parents: 912
diff changeset
104 _manager.setLowResourcesConnections(0);
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
105
909
c60c1adfac3e simplify connectors
Franklin Schmidt <fschmidt@gmail.com>
parents: 907
diff changeset
106 if (_acceptChannel == null)
c60c1adfac3e simplify connectors
Franklin Schmidt <fschmidt@gmail.com>
parents: 907
diff changeset
107 {
c60c1adfac3e simplify connectors
Franklin Schmidt <fschmidt@gmail.com>
parents: 907
diff changeset
108 // Create a new server socket
c60c1adfac3e simplify connectors
Franklin Schmidt <fschmidt@gmail.com>
parents: 907
diff changeset
109 _acceptChannel = ServerSocketChannel.open();
c60c1adfac3e simplify connectors
Franklin Schmidt <fschmidt@gmail.com>
parents: 907
diff changeset
110 // Set to blocking mode
c60c1adfac3e simplify connectors
Franklin Schmidt <fschmidt@gmail.com>
parents: 907
diff changeset
111 _acceptChannel.configureBlocking(true);
c60c1adfac3e simplify connectors
Franklin Schmidt <fschmidt@gmail.com>
parents: 907
diff changeset
112
c60c1adfac3e simplify connectors
Franklin Schmidt <fschmidt@gmail.com>
parents: 907
diff changeset
113 // Bind the server socket to the local host and port
c60c1adfac3e simplify connectors
Franklin Schmidt <fschmidt@gmail.com>
parents: 907
diff changeset
114 _acceptChannel.socket().setReuseAddress(true);
c60c1adfac3e simplify connectors
Franklin Schmidt <fschmidt@gmail.com>
parents: 907
diff changeset
115 InetSocketAddress addr = getHost()==null?new InetSocketAddress(port):new InetSocketAddress(getHost(),port);
c60c1adfac3e simplify connectors
Franklin Schmidt <fschmidt@gmail.com>
parents: 907
diff changeset
116 _acceptChannel.bind(addr);
c60c1adfac3e simplify connectors
Franklin Schmidt <fschmidt@gmail.com>
parents: 907
diff changeset
117 if( _acceptChannel.socket().getLocalPort() != port)
c60c1adfac3e simplify connectors
Franklin Schmidt <fschmidt@gmail.com>
parents: 907
diff changeset
118 throw new IOException("Server channel not bound");
c60c1adfac3e simplify connectors
Franklin Schmidt <fschmidt@gmail.com>
parents: 907
diff changeset
119 }
c60c1adfac3e simplify connectors
Franklin Schmidt <fschmidt@gmail.com>
parents: 907
diff changeset
120
864
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
121 super.doStart();
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
122 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
123
864
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
124 protected AsyncConnection newConnection(SocketChannel channel,final AsyncEndPoint endpoint)
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
125 {
887
df84a1741687 make Connector reference to server explicit
Franklin Schmidt <fschmidt@gmail.com>
parents: 865
diff changeset
126 return new AsyncHttpConnection(SelectChannelConnector.this,endpoint,server);
864
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
127 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
128
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
129
864
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
130 private final class ConnectorSelectorManager extends SelectorManager
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
131 {
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
132 @Override
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 864
diff changeset
133 public void execute(Runnable task)
864
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
134 {
887
df84a1741687 make Connector reference to server explicit
Franklin Schmidt <fschmidt@gmail.com>
parents: 865
diff changeset
135 server.threadPool.execute(task);
864
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
136 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
137
864
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
138 @Override
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
139 public AsyncConnection newConnection(SocketChannel channel,AsyncEndPoint endpoint, Object attachment)
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
140 {
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
141 return SelectChannelConnector.this.newConnection(channel,endpoint);
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
142 }
e21ca9878a10 simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
143 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
144 }