Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/cn/server.md
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,18 @@ server端会自动尝试其支持的协议,无需用户指定。`cntl->protoco

如果你有更多的协议需求,可以联系我们。

`ServerOptions.enabled_protocols`用于限制server提供的协议。它是一个以空格分隔的协议名列表,非空时server只提供列表中的协议,列表中出现未知的协议名会导致`Server::Start()`失败。`rdma_handshake`总是会被提供,因为它是传输层握手且不派发任何请求,将它写入列表是合法的,但不会有任何影响。内置服务、restful服务和`ServerOptions.http_master_service`都通过http/h2提供,因此当它们被启用时(`ServerOptions.has_builtin_services`默认为true),列表中必须同时包含`http`或`h2`,否则`Server::Start()`会失败并给出如何修正配置的提示。`ServerOptions.ssl_options.alpns`中的每个协议也必须被提供,并且该列表在`Server`第一次`Start()`时生效:用不同的列表重启同一个`Server`会失败,需要改用新的`Server`。例如:

```c++
ServerOptions options;
// 提供baidu_std协议和http协议,后者保证内置服务可访问。
options.enabled_protocols = "baidu_std http";
...
// 关闭内置服务后,可以不提供http。
options.has_builtin_services = false;
options.enabled_protocols = "baidu_std";
```

# fork without exec
一般来说,[fork](https://linux.die.net/man/3/fork)出的子进程应尽快调用[exec](https://linux.die.net/man/3/exec)以重置所有状态,中间只应调用满足async-signal-safe的函数。这么使用fork的brpc程序在之前的版本也不会有问题。

Expand Down
12 changes: 12 additions & 0 deletions docs/en/server.md
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,18 @@ Server detects supported protocols automatically, without assignment from users.

If you need more protocols, contact us.

`ServerOptions.enabled_protocols` restricts the protocols to serve. It is a space-separated list of protocol names, and when it is non-empty only the listed protocols are served; an unknown name makes `Server::Start()` fail. `rdma_handshake` is always served as it is a transport level handshake dispatching no request, naming it in the list is allowed and changes nothing. Builtin services, restful services and `ServerOptions.http_master_service` are served through http/h2, so when they are enabled (`ServerOptions.has_builtin_services` is true by default), `http` or `h2` must be listed as well, otherwise `Server::Start()` fails with an error explaining how to fix the configuration. Every name in `ServerOptions.ssl_options.alpns` must be served as well, and the list is applied by the first `Start()` of a `Server`: restarting that `Server` with a different list fails, use a new `Server` instead. For example:

```c++
ServerOptions options;
// Serve baidu_std RPC and http, the latter keeps builtin services reachable.
options.enabled_protocols = "baidu_std http";
...
// Builtin services are disabled, so http is not needed.
options.has_builtin_services = false;
options.enabled_protocols = "baidu_std";
```

# fork without exec
In general, [forked](https://linux.die.net/man/3/fork) subprocess should call [exec](https://linux.die.net/man/3/exec) ASAP, before which only async-signal-safe functions should be called. brpc programs using fork like this should work correctly even in previous versions.

Expand Down
4 changes: 3 additions & 1 deletion src/brpc/acceptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,9 @@ void Acceptor::Join() {
return;
}
// `_listened_fd' will be set to -1 once it has been recycled
while (_listened_fd > 0 || !_socket_map.empty()) {
// `_listened_fd' is set to -1 once it has been recycled. fd 0 is a valid
// listened fd, so compare against the sentinel instead of 0.
while (_listened_fd >= 0 || !_socket_map.empty()) {
_empty_cond.Wait();
}
const int saved_idle_timeout_sec = _idle_timeout_sec;
Expand Down
23 changes: 15 additions & 8 deletions src/brpc/details/ssl_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -999,19 +999,26 @@ void Print(std::ostream& os, X509* cert, const char* sep) {

std::string ALPNProtocolToString(const AdaptiveProtocolType& protocol) {
butil::StringPiece name = protocol.name();
// Default use http 1.1 version
if (name.starts_with("http")) {
// http/1.1 is the ALPN name of the http protocol. Other protocols whose
// names merely start with "http" must keep their own name.
if (static_cast<ProtocolType>(protocol) == PROTOCOL_HTTP) {
name.set("http/1.1");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After preserving the original custom protocol name, a 128-byte http… name is encoded with a length of 127 on platforms where char is signed, while 128 bytes are appended as the body. As a result, the h2 that follows it cannot be negotiated correctly either. Please encode the actual unsigned byte length, append using append(data, size), and reject names longer than 255 bytes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[replied by brpc-oncall robot] Fixed: the length is now encoded as an unsigned char byte and the whole name is appended with append(data, size); names longer than 255 bytes are rejected (and InitALPNOptions() fails the start) instead of being truncated. ALPNProtocolToStringTest.LengthIsEncodedAsUnsignedByte covers a 128-byte name and a 256-byte one.

}

// ALPN extension uses 1 byte to record the protocol length
// and it's maximum length is 255.
if (name.size() > CHAR_MAX) {
name = name.substr(0, CHAR_MAX);
// ALPN extension uses 1 byte to record the protocol length, which is at
// most 255 bytes. Truncating the name silently would produce a wrong ALPN
// entry, so reject it instead.
if (name.size() > 255) {
LOG(ERROR) << "Invalid alpn name=`" << name
<< "', it's longer than 255 bytes";
return std::string();
}

char length = static_cast<char>(name.size());
return std::string(&length, 1) + name.data();
const unsigned char length = static_cast<unsigned char>(name.size());
std::string raw_alpn;
raw_alpn.append(reinterpret_cast<const char*>(&length), 1);
raw_alpn.append(name.data(), name.size());
return raw_alpn;
}

bool BuildALPNProtocolList(
Expand Down
Loading
Loading