-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wk/taco 131 proxy handle h2h1 streamid (#211)
* TACO-131 h2 message session * TACO-131 updating http tests for http message session * TACO-131 http sessions use the same meta object * TACO-131 h2 interleaved request test
- Loading branch information
Showing
17 changed files
with
406 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
xio-core/src/main/java/com/xjeffrose/xio/http/Http2MessageSession.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package com.xjeffrose.xio.http; | ||
|
||
import com.google.common.collect.Maps; | ||
import com.xjeffrose.xio.http.internal.MessageMetaState; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.util.AttributeKey; | ||
import java.util.Map; | ||
import javax.annotation.Nullable; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
/** | ||
* A finite state machine to track the current HTTP/2 message session. This class exists to store | ||
* the connection specific state of the message session | ||
*/ | ||
@Slf4j | ||
public class Http2MessageSession { | ||
|
||
private static final AttributeKey<Http2MessageSession> CHANNEL_MESSAGE_SESSION_KEY = | ||
AttributeKey.newInstance("xio_channel_h2_message_session"); | ||
|
||
static Http2MessageSession contextMessageSession(ChannelHandlerContext ctx) { | ||
Http2MessageSession session = ctx.channel().attr(CHANNEL_MESSAGE_SESSION_KEY).get(); | ||
if (session == null) { | ||
session = new Http2MessageSession(); | ||
ctx.channel().attr(CHANNEL_MESSAGE_SESSION_KEY).set(session); | ||
} | ||
return session; | ||
} | ||
|
||
private Map<Integer, MessageMetaState> streamIdRequests = Maps.newHashMap(); | ||
|
||
private Http2MessageSession() {} | ||
|
||
public void onRequest(Request request) { | ||
MessageMetaState initialRequest = streamIdRequests.get(request.streamId()); | ||
if (initialRequest == null) { | ||
if (request.startOfMessage()) { | ||
streamIdRequests.put( | ||
request.streamId(), new MessageMetaState(request, request.endOfMessage())); | ||
} else { | ||
log.error("Received an h2 message segment without a startOfMessage - request: {}", request); | ||
} | ||
} else { | ||
initialRequest.requestFinished = request.endOfMessage(); | ||
} | ||
} | ||
|
||
public void onRequestData(SegmentedData data) { | ||
MessageMetaState initialRequest = streamIdRequests.get(data.streamId()); | ||
|
||
if (initialRequest == null) { | ||
log.error( | ||
"Received an h2 message SegmentedData without a current Request, dropping data: {}", | ||
data); | ||
return; | ||
} | ||
|
||
if (data.endOfMessage()) { | ||
initialRequest.requestFinished = true; | ||
} | ||
} | ||
|
||
public void onResponse(Response response) { | ||
MessageMetaState initialRequest = streamIdRequests.get(response.streamId()); | ||
if (initialRequest != null && response.endOfMessage()) { | ||
initialRequest.responseFinished = true; | ||
} | ||
} | ||
|
||
/** | ||
* Called before a SegmentedData object is sent to the client as part of a Response. | ||
* | ||
* @param data The SegmentedData object the server is about to send | ||
*/ | ||
public void onResponseData(SegmentedData data) { | ||
MessageMetaState initialRequest = streamIdRequests.get(data.streamId()); | ||
if (initialRequest != null) { | ||
if (data.endOfMessage()) { | ||
initialRequest.responseFinished = true; | ||
} | ||
} else { | ||
log.error( | ||
"Attempted to write SegmentedData without a current Request, dropping data: {}", data); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the Request object for the current session (if any). | ||
* | ||
* @return the current Request or null | ||
*/ | ||
@Nullable | ||
public Request currentRequest(int streamId) { | ||
MessageMetaState initialRequest = streamIdRequests.get(streamId); | ||
if (initialRequest != null) { | ||
return initialRequest.request; | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Check if the message session has completed, if so remove state and prepare for the next | ||
* session. | ||
*/ | ||
public void flush(int streamId) { | ||
MessageMetaState initialRequest = streamIdRequests.get(streamId); | ||
if (initialRequest != null | ||
&& initialRequest.requestFinished | ||
&& initialRequest.responseFinished) { | ||
streamIdRequests.remove(streamId); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.