“HTTP/2”与“Vertx: Future异步操作设计”:页面之间的差异

来自WHY42
(页面间差异)
无编辑摘要
 
 
第1行: 第1行:
=Future相关操作的设计=
== AsyncResult ==
AsyncResult表示一个异步操作的结果,这个结果要么是成功的,要么是失败的。
<syntaxhighlight lang="java">
interface AsyncResult<T> {
    /* 获取异步结果,或者失败原因。倘若失败,那么结果为null */
    T getResult();
    Throwable cause();
    boolean succeeded();
    boolean failed();


== HTTP2 vs HTTP1 ==
    /* 对结果进行转换。如果成功,那么可以将成功的结果转换为另一种形式;否则可以得到一个替代的结果。*/
Multiplexing: HTTP/1.1 loads resources one after the other, so if one resource cannot be loaded, it blocks all the other resources behind it. In contrast, HTTP/2 is able to use a single TCP connection to send multiple streams of data at once so that no one resource blocks any other resource. HTTP/2 does this by splitting data into binary-code messages and numbering these messages so that the client knows which stream each binary message belongs to.
    <V> AsyncResult<V> map(Function<T, V> mapper);
 
    <V> AsyncResult<V> map(V value);
Server push: Typically, a server only serves content to a client device if the client asks for it. However, this approach is not always practical for modern webpages, which often involve several dozen separate resources that the client must request. HTTP/2 solves this problem by allowing a server to "push" content to a client before the client asks for it. The server also sends a message letting the client know what pushed content to expect – like if Bob had sent Alice a Table of Contents of his novel before sending the whole thing.
    <V> AsyncResult<V> mapEmpty();
 
    AsyncResult<T> otherwise(T value);
Header compression: Small files load more quickly than large ones. To speed up web performance, both HTTP/1.1 and HTTP/2 compress HTTP messages to make them smaller. However, HTTP/2 uses a more advanced compression method called HPACK that eliminates redundant information in HTTP header packets. This eliminates a few bytes from every HTTP packet. Given the volume of HTTP packets involved in loading even a single webpage, those bytes add up quickly, resulting in faster loading.
    AsyncResult<T> otherwise(Function<Throwable, T> mapper);
== HTTP/2 Discovery ==
    AsyncResult<T> otherwiseEmpty();
;h2: HTTP/2 over TLS
}
;h2c: HTTP/2 is run over cleartext TCP
 
== Starting HTTP/2 for "http" URIs==
如果客户端无法事先知道服务端是否支持http2,可以通过HTTP Upgrade mechanism(定义在HTTP/1.1)中来实现,这样在之后的请求中可以采取HTTP/2。
 
<syntaxhighlight lang="lisp">
GET / HTTP/1.1
Host: server.example.com
Connection: Upgrade, HTTP2-Settings
Upgrade: h2c
HTTP2-Settings: <base64url encoding of HTTP/2 SETTINGS payload>
</syntaxhighlight>
</syntaxhighlight>


这样的话,如果请求中由payload必须一次性发完。然后才能升级到HTTP/2。另一种办法是通过<span class="article-label">OPTIONS</span>请求
==Future==
来判断,这样做会多一次请求。
Future表示一个尚未完成的异步操作,并继承自AsyncResult
<syntaxhighlight lang="java">
interface Future<T>
    extends AsyncResult<T> {
    boolean isComplete();
    Future<T> onComplete(Handler<AsyncResult<T>> handler);
    Future<T> onSuccess(Handler<T> handler);
    Future<T> onFailure(Handler<Throwable> handler);
    <V> Future<V> compose(Function<T, Future<V>> mapper);
    <V> Future<V> compose(Function<T, Future<V>> successMapper,
                      Function<Throwable, Future<V>> failureMapper);
    Future<T> recover(Function<Throwable, Future<T>> mapper);
    <V> Future<V> transform(Function<AsyncResult<T>, Future<V>> mapper);
    <V> Future<T> eventually(Function<Void, Future<V>> mapper);
    CompletionStage<T> toCompletionState();
}


如果服务端不支持HTTP/2,返回中则不包含Upgrade header:
<syntaxhighlight lang="lisp">
HTTP/1.1 200 OK
Content-Length: 243
Content-Type: text/html
</syntaxhighlight>
</syntaxhighlight>


否则,返回一个101 (Switching Protocols)响应:
[[Category:Vert.x]]
 
<syntaxhighlight lang="lisp">
HTTP/1.1 101 Switching Protocols
Connection: Upgrade
Upgrade: h2c
 
[ HTTP/2 connection ...
</syntaxhighlight>
 
在h2c后面的空行之后,就可以开始发送HTTP/2的frame了。
 
=== HTTP2-Settings ===
 
[[Category:Network]]

2021年7月25日 (日) 08:21的版本

Future相关操作的设计

AsyncResult

AsyncResult表示一个异步操作的结果,这个结果要么是成功的,要么是失败的。

interface AsyncResult<T> {
    /* 获取异步结果,或者失败原因。倘若失败,那么结果为null */
    T getResult();
    Throwable cause();
    boolean succeeded();
    boolean failed();

    /* 对结果进行转换。如果成功,那么可以将成功的结果转换为另一种形式;否则可以得到一个替代的结果。*/
    <V> AsyncResult<V> map(Function<T, V> mapper);
    <V> AsyncResult<V> map(V value);
    <V> AsyncResult<V> mapEmpty();
    AsyncResult<T> otherwise(T value);
    AsyncResult<T> otherwise(Function<Throwable, T> mapper);
    AsyncResult<T> otherwiseEmpty();
}

Future

Future表示一个尚未完成的异步操作,并继承自AsyncResult

interface Future<T>
    extends AsyncResult<T> {
    boolean isComplete();
    Future<T> onComplete(Handler<AsyncResult<T>> handler);
    Future<T> onSuccess(Handler<T> handler);
    Future<T> onFailure(Handler<Throwable> handler);
    <V> Future<V> compose(Function<T, Future<V>> mapper);
    <V> Future<V> compose(Function<T, Future<V>> successMapper, 
                      Function<Throwable, Future<V>> failureMapper);
    Future<T> recover(Function<Throwable, Future<T>> mapper);
    <V> Future<V> transform(Function<AsyncResult<T>, Future<V>> mapper);
    <V> Future<T> eventually(Function<Void, Future<V>> mapper);
    CompletionStage<T> toCompletionState();
}