Skip to content

Commit

Permalink
gh-30 fix the errors ?
Browse files Browse the repository at this point in the history
  • Loading branch information
Unknow0 committed Apr 15, 2024
1 parent ee42041 commit 29b5594
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 84 deletions.
6 changes: 2 additions & 4 deletions bench/result.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ parse()

while read t a n e
do
l=end[$s:$n]
e=$(($t+$a ))
[[ -z $l || $l -lt $n ]] && end[$s:$n]=$e
done < <(sort -t , -r -k 3,1n "$1")
end[$s:$n]=$t
done < <(sort -t , -r -k 3,1n "$1" | sort -t , -k 3 -u)
}

for i in $1/*.csv; do parse "$i"; done
Expand Down
173 changes: 120 additions & 53 deletions bench/test.jmx

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,11 @@ protected final void writeInto(SocketChannel channel, ByteBuffer buf) throws Int
}

channel.write(buf);
if (buf.hasRemaining())
if (buf.hasRemaining()) {
pendingWrite.prepend(buf);
break;
}
}
if (buf.hasRemaining()) // we didn't write all
pendingWrite.prepend(buf);
toggleKeyOps();
onWrite();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ else if (t.equals("usec_frac"))
});
}

private static final ThreadLocal<StringBuilder> SB = ThreadLocal.withInitial(() -> new StringBuilder());

/** format used to log */
private Part[] parts;

Expand All @@ -169,6 +171,10 @@ protected void setFormat(String format) throws ServletException {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
String f = filterConfig.getInitParameter("format");
if (f == null)
f = System.getProperty("ACCESS_LOG_FMT");
if (f == null)
f = System.getenv("ACCESS_LOG_FMT");
setFormat(f == null ? DEFAULT_FMT : f);
}

Expand All @@ -179,15 +185,17 @@ public final void doFilter(ServletRequest request, ServletResponse response, Fil
return;
}
LocalDateTime start = LocalDateTime.now();

try {
chain.doFilter(request, response);
} finally {
LocalDateTime end = LocalDateTime.now();

StringBuilder sb = new StringBuilder();
StringBuilder sb = SB.get();
for (int i = 0; i < parts.length; i++)
parts[i].append(sb, start, end, (HttpServletRequest) request, (HttpServletResponse) response);
logger.info("{}", sb);
sb.setLength(0);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public boolean closed(long now, boolean stop) {
if (p != null && !p.isClosed())
return false;

if (p == null && lastRead() < now - 1000) {
logger.warn(" request timeout");
return true;
}

if (pendingWrite.isEmpty() && keepAliveIdle > 0) {
long e = now - keepAliveIdle;
if (lastRead() <= e && lastWrite() <= e) {
Expand All @@ -74,7 +79,6 @@ protected final void onFree() {
p.close();
p = null;
}
pendingRead.clear();
}

public Future<?> submit(Runnable r) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public abstract class HttpWorker implements Runnable, HttpAdapter {

protected final HttpConnection co;
protected final EventManager events;
protected final ServletRequestImpl req;
protected final ServletResponseImpl res;
protected ServletRequestImpl req;
protected ServletResponseImpl res;

public HttpWorker(HttpConnection co) {
this.co = co;
Expand All @@ -33,18 +33,27 @@ public ServletContextImpl ctx() {
return co.getCtx();
}

protected abstract void doStart() throws IOException, InterruptedException;
protected abstract boolean doStart() throws IOException, InterruptedException;

protected abstract void doDone();

@Override
public final void run() {
public void run() {
doRun();
}

protected final void doRun() {
try {
doStart();
if (!doStart()) {
logger.warn("init req failed");
co.getOut().close();
return;
}
events.fireRequestInitialized(req);
FilterChain s = co.getCtx().getServletManager().find(req);
try {
s.doFilter(req, res);
co.pendingRead.clear();
} catch (UnavailableException e) {
// TODO add page with retry-after
res.sendError(503, e, null);
Expand All @@ -65,7 +74,6 @@ public final void run() {
} finally {
doDone();
co.flush();
co.pendingRead.clear();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jakarta.servlet.DispatcherType;
import jakarta.servlet.ServletInputStream;
import jakarta.servlet.http.Cookie;
import unknow.server.nio.NIOConnection.Out;
Expand All @@ -20,14 +21,15 @@
import unknow.server.servlet.HttpError;
import unknow.server.servlet.HttpWorker;
import unknow.server.servlet.impl.ServletRequestImpl;
import unknow.server.servlet.impl.ServletResponseImpl;
import unknow.server.servlet.impl.in.ChunckedInputStream;
import unknow.server.servlet.impl.in.EmptyInputStream;
import unknow.server.servlet.impl.in.LengthInputStream;
import unknow.server.servlet.impl.out.AbstractServletOutput;
import unknow.server.util.io.Buffers;
import unknow.server.util.io.BuffersUtils;

public class Http11Worker extends HttpWorker {
public final class Http11Worker extends HttpWorker {
private static final Logger logger = LoggerFactory.getLogger(Http11Worker.class);

private static final byte[] END = new byte[] { '\r', '\n', '\r', '\n' };
Expand Down Expand Up @@ -165,24 +167,38 @@ public void commit() throws IOException {
// out.write(ERROR_END);
// }

@SuppressWarnings("resource")
@Override
public final void doStart() throws IOException, InterruptedException {
boolean close = false;
public void run() {
doRun();
while (!co.pendingRead.isEmpty()) {
this.req = new ServletRequestImpl(this, DispatcherType.REQUEST);
this.res = new ServletResponseImpl(this);
doRun();
}
}

Out out = co.getOut();
if (!fillRequest(req))
return;
@SuppressWarnings("resource")
@Override
public final boolean doStart() throws IOException, InterruptedException {
if (!fillRequest(req)) {
logger.warn("Failed to process request");
return false;
}

if ("100-continue".equals(req.getHeader("expect"))) {
Out out = co.getOut();
out.write(HttpError.CONTINUE.encoded);
out.write('\r');
out.write('\n');
out.flush();
}

close = keepAliveIdle == 0 || !"keep-alive".equalsIgnoreCase(req.getHeader("connection"));
res.setHeader("connection", close ? "close" : "keep-alive");
if (keepAliveIdle != 0 && "keep-alive".equalsIgnoreCase(req.getHeader("connection"))) {
res.setHeader("connection", "keep-alive");
res.setHeader("keep-alive", "timeout=" + (keepAliveIdle / 1000));
} else
res.setHeader("connection", "close");
return true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ public void commit() throws IOException {
}

@Override
protected void doStart() throws IOException, InterruptedException {
protected boolean doStart() throws IOException, InterruptedException {
// TODO Auto-generated method stub

return true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public final void commit() throws IOException {
}

public void sendError(int sc, Throwable t, String msg) throws IOException {
reset();
reset(false);
ServletManager manager = co.ctx().getServletManager();
FilterChain f = manager.getError(sc, t);
if (f != null) {
Expand Down Expand Up @@ -224,12 +224,18 @@ public void resetBuffer() {

@Override
public void reset() {
reset(true);
}

public void reset(boolean clearHeader) {
resetBuffer();
status = 200;
List<String> list = headers.get("connection");
headers.clear();
if (list != null)
headers.put("connection", list);
if (clearHeader) {
List<String> list = headers.get("connection");
headers.clear();
if (list != null)
headers.put("connection", list);
}
stream = null;
writer = null;
}
Expand Down

0 comments on commit 29b5594

Please sign in to comment.