#9928 backport Request.beginNanoTime() from 12.0.x
Signed-off-by: Ludovic Orban <lorban@bitronix.be>
This commit is contained in:
parent
900f50f513
commit
1965a943f9
|
@ -26,6 +26,7 @@ import org.eclipse.jetty.http.HttpTokens.EndOfContent;
|
|||
import org.eclipse.jetty.util.BufferUtil;
|
||||
import org.eclipse.jetty.util.HostPort;
|
||||
import org.eclipse.jetty.util.Index;
|
||||
import org.eclipse.jetty.util.NanoTime;
|
||||
import org.eclipse.jetty.util.StringUtil;
|
||||
import org.eclipse.jetty.util.Utf8StringBuilder;
|
||||
import org.slf4j.Logger;
|
||||
|
@ -249,6 +250,7 @@ public class HttpParser
|
|||
private ByteBuffer _contentChunk;
|
||||
private int _length;
|
||||
private final StringBuilder _string = new StringBuilder();
|
||||
private long _beginNanoTime = Long.MIN_VALUE;
|
||||
|
||||
private static HttpCompliance compliance()
|
||||
{
|
||||
|
@ -300,6 +302,11 @@ public class HttpParser
|
|||
_complianceListener = (ComplianceViolation.Listener)(_handler instanceof ComplianceViolation.Listener ? _handler : null);
|
||||
}
|
||||
|
||||
public long getBeginNanoTime()
|
||||
{
|
||||
return _beginNanoTime;
|
||||
}
|
||||
|
||||
public HttpHandler getHandler()
|
||||
{
|
||||
return _handler;
|
||||
|
@ -1517,6 +1524,8 @@ public class HttpParser
|
|||
_methodString = null;
|
||||
_endOfContent = EndOfContent.UNKNOWN_CONTENT;
|
||||
_header = null;
|
||||
if (buffer.hasRemaining())
|
||||
_beginNanoTime = NanoTime.now();
|
||||
quickStart(buffer);
|
||||
}
|
||||
|
||||
|
|
|
@ -17,6 +17,8 @@ import java.util.Collections;
|
|||
import java.util.Iterator;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.eclipse.jetty.util.NanoTime;
|
||||
|
||||
public class MetaData implements Iterable<HttpField>
|
||||
{
|
||||
/**
|
||||
|
@ -115,6 +117,7 @@ public class MetaData implements Iterable<HttpField>
|
|||
{
|
||||
private final String _method;
|
||||
private final HttpURI _uri;
|
||||
private final long _beginNanoTime;
|
||||
|
||||
public Request(HttpFields fields)
|
||||
{
|
||||
|
@ -126,11 +129,19 @@ public class MetaData implements Iterable<HttpField>
|
|||
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)
|
||||
{
|
||||
super(version, fields, contentLength);
|
||||
_method = method;
|
||||
_uri = uri.asImmutable();
|
||||
this(method, uri.asImmutable(), version, fields, contentLength, null);
|
||||
}
|
||||
|
||||
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)
|
||||
|
@ -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)
|
||||
{
|
||||
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);
|
||||
_method = method;
|
||||
_uri = uri;
|
||||
_beginNanoTime = beginNanoTime;
|
||||
}
|
||||
|
||||
public long getBeginNanoTime()
|
||||
{
|
||||
return _beginNanoTime;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -36,6 +36,7 @@ import org.eclipse.jetty.http.MetaData;
|
|||
import org.eclipse.jetty.io.Connection;
|
||||
import org.eclipse.jetty.io.EndPoint;
|
||||
import org.eclipse.jetty.io.EofException;
|
||||
import org.eclipse.jetty.util.NanoTime;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
@ -340,6 +341,7 @@ public class HttpChannelOverHttp extends HttpChannel implements HttpParser.Reque
|
|||
@Override
|
||||
public boolean headerComplete()
|
||||
{
|
||||
_requestBuilder.beginNanoTime(_httpConnection.getBeginNanoTime());
|
||||
_metadata = _requestBuilder.build();
|
||||
onRequest(_metadata);
|
||||
|
||||
|
@ -705,6 +707,7 @@ public class HttpChannelOverHttp extends HttpChannel implements HttpParser.Reque
|
|||
private final HttpURI.Mutable _uriBuilder = HttpURI.build();
|
||||
private String _method;
|
||||
private HttpVersion _version;
|
||||
private long _beginNanoTime = Long.MIN_VALUE;
|
||||
|
||||
public String method()
|
||||
{
|
||||
|
@ -719,6 +722,13 @@ public class HttpChannelOverHttp extends HttpChannel implements HttpParser.Reque
|
|||
_fieldsBuilder.clear();
|
||||
}
|
||||
|
||||
public void beginNanoTime(long nanoTime)
|
||||
{
|
||||
if (nanoTime == Long.MIN_VALUE)
|
||||
nanoTime++;
|
||||
_beginNanoTime = nanoTime;
|
||||
}
|
||||
|
||||
public HttpFields.Mutable getFields()
|
||||
{
|
||||
return _fieldsBuilder;
|
||||
|
@ -726,7 +736,8 @@ public class HttpChannelOverHttp extends HttpChannel implements HttpParser.Reque
|
|||
|
||||
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()
|
||||
|
|
|
@ -107,6 +107,11 @@ public class HttpConnection extends AbstractConnection implements Runnable, Http
|
|||
LOG.debug("New HTTP Connection {}", this);
|
||||
}
|
||||
|
||||
public long getBeginNanoTime()
|
||||
{
|
||||
return _parser.getBeginNanoTime();
|
||||
}
|
||||
|
||||
public HttpConfiguration getHttpConfiguration()
|
||||
{
|
||||
return _config;
|
||||
|
|
|
@ -1709,6 +1709,16 @@ public class Request implements HttpServletRequest
|
|||
_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
|
||||
public boolean isUserInRole(String role)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue