#9928 backport Request.beginNanoTime() from 12.0.x

Signed-off-by: Ludovic Orban <lorban@bitronix.be>
This commit is contained in:
Ludovic Orban 2023-08-30 14:02:02 +02:00
parent 900f50f513
commit 1965a943f9
5 changed files with 61 additions and 4 deletions

View File

@ -26,6 +26,7 @@ import org.eclipse.jetty.http.HttpTokens.EndOfContent;
import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.HostPort; import org.eclipse.jetty.util.HostPort;
import org.eclipse.jetty.util.Index; import org.eclipse.jetty.util.Index;
import org.eclipse.jetty.util.NanoTime;
import org.eclipse.jetty.util.StringUtil; import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.Utf8StringBuilder; import org.eclipse.jetty.util.Utf8StringBuilder;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -249,6 +250,7 @@ public class HttpParser
private ByteBuffer _contentChunk; private ByteBuffer _contentChunk;
private int _length; private int _length;
private final StringBuilder _string = new StringBuilder(); private final StringBuilder _string = new StringBuilder();
private long _beginNanoTime = Long.MIN_VALUE;
private static HttpCompliance compliance() private static HttpCompliance compliance()
{ {
@ -300,6 +302,11 @@ public class HttpParser
_complianceListener = (ComplianceViolation.Listener)(_handler instanceof ComplianceViolation.Listener ? _handler : null); _complianceListener = (ComplianceViolation.Listener)(_handler instanceof ComplianceViolation.Listener ? _handler : null);
} }
public long getBeginNanoTime()
{
return _beginNanoTime;
}
public HttpHandler getHandler() public HttpHandler getHandler()
{ {
return _handler; return _handler;
@ -1517,6 +1524,8 @@ public class HttpParser
_methodString = null; _methodString = null;
_endOfContent = EndOfContent.UNKNOWN_CONTENT; _endOfContent = EndOfContent.UNKNOWN_CONTENT;
_header = null; _header = null;
if (buffer.hasRemaining())
_beginNanoTime = NanoTime.now();
quickStart(buffer); quickStart(buffer);
} }

View File

@ -17,6 +17,8 @@ import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
import java.util.function.Supplier; import java.util.function.Supplier;
import org.eclipse.jetty.util.NanoTime;
public class MetaData implements Iterable<HttpField> public class MetaData implements Iterable<HttpField>
{ {
/** /**
@ -115,6 +117,7 @@ public class MetaData implements Iterable<HttpField>
{ {
private final String _method; private final String _method;
private final HttpURI _uri; private final HttpURI _uri;
private final long _beginNanoTime;
public Request(HttpFields fields) public Request(HttpFields fields)
{ {
@ -126,11 +129,19 @@ public class MetaData implements Iterable<HttpField>
this(method, uri, version, fields, Long.MIN_VALUE); this(method, uri, version, fields, Long.MIN_VALUE);
} }
public Request(long beginNanoTime, String method, HttpURI uri, HttpVersion version, HttpFields fields)
{
this(beginNanoTime, method, uri, version, fields, Long.MIN_VALUE);
}
public Request(String method, HttpURI uri, HttpVersion version, HttpFields fields, long contentLength) public Request(String method, HttpURI uri, HttpVersion version, HttpFields fields, long contentLength)
{ {
super(version, fields, contentLength); this(method, uri.asImmutable(), version, fields, contentLength, null);
_method = method; }
_uri = uri.asImmutable();
public Request(long beginNanoTime, String method, HttpURI uri, HttpVersion version, HttpFields fields, long contentLength)
{
this(beginNanoTime, method, uri.asImmutable(), version, fields, contentLength, null);
} }
public Request(String method, String scheme, HostPortHttpField authority, String uri, HttpVersion version, HttpFields fields, long contentLength) public Request(String method, String scheme, HostPortHttpField authority, String uri, HttpVersion version, HttpFields fields, long contentLength)
@ -141,10 +152,21 @@ public class MetaData implements Iterable<HttpField>
} }
public Request(String method, HttpURI uri, HttpVersion version, HttpFields fields, long contentLength, Supplier<HttpFields> trailers) public Request(String method, HttpURI uri, HttpVersion version, HttpFields fields, long contentLength, Supplier<HttpFields> trailers)
{
this(NanoTime.now(), method, uri, version, fields, contentLength, trailers);
}
public Request(long beginNanoTime, String method, HttpURI uri, HttpVersion version, HttpFields fields, long contentLength, Supplier<HttpFields> trailers)
{ {
super(version, fields, contentLength, trailers); super(version, fields, contentLength, trailers);
_method = method; _method = method;
_uri = uri; _uri = uri;
_beginNanoTime = beginNanoTime;
}
public long getBeginNanoTime()
{
return _beginNanoTime;
} }
@Override @Override

View File

@ -36,6 +36,7 @@ import org.eclipse.jetty.http.MetaData;
import org.eclipse.jetty.io.Connection; import org.eclipse.jetty.io.Connection;
import org.eclipse.jetty.io.EndPoint; import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.io.EofException; import org.eclipse.jetty.io.EofException;
import org.eclipse.jetty.util.NanoTime;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -340,6 +341,7 @@ public class HttpChannelOverHttp extends HttpChannel implements HttpParser.Reque
@Override @Override
public boolean headerComplete() public boolean headerComplete()
{ {
_requestBuilder.beginNanoTime(_httpConnection.getBeginNanoTime());
_metadata = _requestBuilder.build(); _metadata = _requestBuilder.build();
onRequest(_metadata); onRequest(_metadata);
@ -705,6 +707,7 @@ public class HttpChannelOverHttp extends HttpChannel implements HttpParser.Reque
private final HttpURI.Mutable _uriBuilder = HttpURI.build(); private final HttpURI.Mutable _uriBuilder = HttpURI.build();
private String _method; private String _method;
private HttpVersion _version; private HttpVersion _version;
private long _beginNanoTime = Long.MIN_VALUE;
public String method() public String method()
{ {
@ -719,6 +722,13 @@ public class HttpChannelOverHttp extends HttpChannel implements HttpParser.Reque
_fieldsBuilder.clear(); _fieldsBuilder.clear();
} }
public void beginNanoTime(long nanoTime)
{
if (nanoTime == Long.MIN_VALUE)
nanoTime++;
_beginNanoTime = nanoTime;
}
public HttpFields.Mutable getFields() public HttpFields.Mutable getFields()
{ {
return _fieldsBuilder; return _fieldsBuilder;
@ -726,7 +736,8 @@ public class HttpChannelOverHttp extends HttpChannel implements HttpParser.Reque
public MetaData.Request build() public MetaData.Request build()
{ {
return new MetaData.Request(_method, _uriBuilder, _version, _fieldsBuilder); long nanoTime = _beginNanoTime == Long.MIN_VALUE ? NanoTime.now() : _beginNanoTime;
return new MetaData.Request(nanoTime, _method, _uriBuilder, _version, _fieldsBuilder);
} }
public HttpVersion version() public HttpVersion version()

View File

@ -107,6 +107,11 @@ public class HttpConnection extends AbstractConnection implements Runnable, Http
LOG.debug("New HTTP Connection {}", this); LOG.debug("New HTTP Connection {}", this);
} }
public long getBeginNanoTime()
{
return _parser.getBeginNanoTime();
}
public HttpConfiguration getHttpConfiguration() public HttpConfiguration getHttpConfiguration()
{ {
return _config; return _config;

View File

@ -1709,6 +1709,16 @@ public class Request implements HttpServletRequest
_secure = secure; _secure = secure;
} }
/**
* <p>Get the nanoTime at which the request arrived to a connector, obtained via {@link System#nanoTime()}.
* This method can be used when measuring latencies.</p>
* @return The nanoTime at which the request was received/created in nanoseconds
*/
public long getBeginNanoTime()
{
return _metaData.getBeginNanoTime();
}
@Override @Override
public boolean isUserInRole(String role) public boolean isUserInRole(String role)
{ {