我正在寻找一种map或filter来自Python编程语言的C ++类似物。它们中的第一个对每个iterable都应用某个函数,并返回结果列表,第二个函数从这些iterable的元素构造一个列表,对于这些元素,函数返回true。
我想在C ++中使用类似的功能:
在C ++中,Python的映射和过滤器有没有很好的实现?
在这短短的例子中,我试图去解决它使用这样的工具boost::bind和std::for_each我面临的一个困难。本std::vector<std::string> result应包含所有的字符串std::vector<std::string> raw是lexicographicaly比从标准输入最后一个字符串更高。但是实际上,result容器在返回点仍然是空的。
#include <iostream>
#include <vector>
#include <algorithm>
#include <boost/bind.hpp>
void filter_strings(std::string& current, std::string& last, std::vector<std::string>& results)
{
if (current > last)
{
results.push_back(current);
std::cout << "Matched: " << current << std::endl;
}
}
int main()
{
std::vector<std::string> raw, result;
std::string input, last;
//Populate first container with a data
while(std::getline(std::cin, input)) …Run Code Online (Sandbox Code Playgroud) 是否有可能Gitlab通过命令行而不是通过Web界面获取API的个人访问令牌?我正在进行一些集成测试,并且将其Gitlab部署到干净的环境中是测试会话设置的一部分。部署测试后,用户正在使用GitlabAPI 做一些工作。为了访问API,测试用户需要提供个人访问令牌。
我设法转储流量,并且看到在呈现的HTML模板中提供了令牌以响应POST请求:
00:06:40.616996 IP6 localhost.amanda > localhost.53808: Flags [P.], seq 1:580, ack 1054, win 497, options [nop,nop,TS val 3133641719 ecr 3133641673], length 579
`..U.c.@................................'`.0...y.eIz.....k.....
........HTTP/1.1 302 Found
Server: nginx
Date: Tue, 21 Nov 2017 21:06:40 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 119
Connection: keep-alive
Cache-Control: no-cache
Location: http://localhost:10080/profile/personal_access_tokens
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-Request-Id: 88178813-95ad-419a-b56b-5a5ddb183885
X-Runtime: 0.044209
X-Ua-Compatible: IE=edge
X-Xss-Protection: 1; mode=block
X-Accel-Buffering: no
Run Code Online (Sandbox Code Playgroud)
内心深处的回应:
<input type="text" name="created-personal-access-token" id="created-personal-access-token" value="j1WZujuaKVVEkh8h8Fej" readonly="readonly" class="form-control js-select-on-focus" …Run Code Online (Sandbox Code Playgroud) 我设置了一系列 gRPC 请求和响应,它们都可以正常工作,但是当我尝试获取客户端 IP 地址和调用我的 gRPC API 的用户代理时,我卡住了。
我阅读了 Go gRPC 文档和其他来源,但没有找到太多有价值的信息。他们中很少有人谈论 Golang 中的 gRPC。
在设置 gRPC API 时,我应该设置一个键值来在上下文中存储 IP 地址吗?
我们将valgrind其用作 CI 流程的一部分。如果存在一些内存问题,valgrind必须返回非零代码,并且报告此事件。这就是我们运行它的方式:
valgrind --error-exitcode=1 --tool=memcheck --leak-check=full \
--errors-for-leak-kinds=definite --show-leak-kinds=definite \
--track-origins=yes ./some_cgo_application
(...)
==25182== HEAP SUMMARY:
==25182== in use at exit: 2,416,970 bytes in 34,296 blocks
==25182== total heap usage: 83,979 allocs, 49,684 frees, 5,168,335 bytes allocated
==25182==
==25182== LEAK SUMMARY:
==25182== definitely lost: 0 bytes in 0 blocks
==25182== indirectly lost: 0 bytes in 0 blocks
==25182== possibly lost: 3,024 bytes in 7 blocks
==25182== still reachable: 2,413,946 bytes in 34,289 blocks
==25182== of …Run Code Online (Sandbox Code Playgroud) 我正在使用 gRPC 构建 API,并且在服务器端,我想在客户端断开连接时收到通知,识别它并基于此执行一些任务。
到目前为止,我能够使用grpc.StatsHandlermethod检测客户端断开连接HandleConn。我尝试使用上下文传递值,但无法从服务器端访问它们。
客户端:
conn, err := grpc.DialContext(
context.WithValue(context.Background(), "user_id", 1234),
address,
grpc.WithInsecure(),
)
Run Code Online (Sandbox Code Playgroud)
服务器端:
// Build stats handler
type serverStats struct {}
func (h *serverStats) TagRPC(ctx context.Context, info *stats.RPCTagInfo) context.Context {
return ctx
}
func (h *serverStats) HandleRPC(ctx context.Context, s stats.RPCStats) {}
func (h *serverStats) TagConn(ctx context.Context, info *stats.ConnTagInfo) context.Context {
return context.TODO()
}
func (h *serverStats) HandleConn(ctx context.Context, s stats.ConnStats) {
fmt.Println(ctx.Value("user_id")) // Returns nil, can't access the value
switch s.(type) { …Run Code Online (Sandbox Code Playgroud) 在下面的gRPC-client代码中,第二个是if必要的吗?
status, err := cli.GetStatus(ctx, &empty.Empty{})
if err != nil {
return err
}
if status == nil {
// this should NEVER happen - right?
return fmt.Errorf("nil Status result returned")
}
Run Code Online (Sandbox Code Playgroud)
go 直觉上,为了以防万一,应该始终检查 nil 。但是,有一个运行时检查来捕获任何客户端到服务器的nil使用情况,例如
status, err := cli.GetStatus(ctx, nil) // <- runtime error
if err != nil {
// "rpc error: code = Internal desc = grpc: error while marshaling: proto: Marshal called with nil"
return err
}
Run Code Online (Sandbox Code Playgroud)
那么是否存在类似的服务器到客户端运行时保证,从而消除检查的需要status == …
我正在设计Content-addressable storage,所以我正在寻找一个哈希函数来生成对象标识符。每个对象都应该以这种方式根据其内容获得短 ID:object_id = hash(object_content).
先决条件:
32字节,以便256^32最大程度地寻址对象(但可以放宽此要求)。考虑到这些要求,我选择了SHA256哈希,但不幸的是,它对于我的目的来说还不够快。最快的实现SHA256,我能到基准是openssl和boringssl:我的桌面上,Intel Core I5 6400它大约给了420 MB/s每个核心。其他实现(如crypto/rsaGo)甚至更慢。我想SHA256用其他散列函数替换,它提供与 相同的碰撞保证SHA256,但提供更好的吞吐量(至少600 MB/s每个内核)。
请分享您对解决此问题的可能选项的看法。
另外我想指出的是,硬件更新(例如购买带有AVX512指令集的现代 CPU )是不可能的。重点是找到可以在商品硬件上提供更好性能的哈希函数。
我正在使用微服务架构创建系统。有两个微服务A和B,每个都位于自己的存储库中。
有一个user.proto包含protobuf定义和gRPC方法签名的文件。用作服务器的A用途user.pb.go。B使用user.pb.go作为(一个客户A)。
一种构造方法是在中出现原型定义A,并B具有对以下内容的代码依赖性A:
A
??? pb
? ??? user.proto
? ??? user.pb.go
??? service.go
B
??? service.go
B-->A
Run Code Online (Sandbox Code Playgroud)
另一种方法是使用另一个P包含原始定义的回购,A并B取决于新的回购:
A
??? service.go
B
??? service.go
P
??? user.proto
??? user.pb.go
A-->P
B-->P
Run Code Online (Sandbox Code Playgroud)
或者,新的仓库可能只包含原始文件,并且在A和B中都生成了代码:
A
??? service.go
??? pb
??? user.pb.go
B
??? service.go
??? pb
??? user.pb.go
P
??? user.proto
Run Code Online (Sandbox Code Playgroud)
这里有什么更好的方法?
在 Datastax 的文档中,他们说 Paxos 协议有四个阶段(意思是在轻量级事务中):
- 准备/承诺
- 读取/结果
- 提议/接受
- 提交/确认
左边是提议者的阶段,右边是接受者的阶段。
然后他们尝试解释这个过程:
提案者通过向法定数量的接受者发送一条包含提案编号的消息来进行准备。如果提案编号是他们收到的最高提案,则每个接受者都承诺接受该提案。一旦提案者收到承诺的接受者的法定数量,就会从每个接受者读取提案的值并将其发送回提案者。提议者找出要使用的值,并将该值连同提议编号一起提交给法定人数的接受者。当且仅当接受者尚未承诺接受具有高编号的提案时,每个接受者都会接受具有一定编号的提案。如果满足所有条件,则该值将作为 Cassandra 写入操作提交并确认。
我无法理解这个解释。有人可以用更清楚的方式解释一下吗?
我的原型文件是:
syntax = "proto3";
import "google/protobuf/timestamp.proto";
service Foo {
rpc now(NowRequest) returns (NowResponse) {}
}
message NowRequest {}
message NowResponse {
google.protobuf.Timestamp now = 1;
}
Run Code Online (Sandbox Code Playgroud)
我生成代码的命令和由此产生的错误是:
protoc foo.proto --go_out=plugins=grpc,import_path=proto:internal/proto
foo.proto:3:1: Import "google/protobuf/timestamp.proto" was not found or had errors.
foo.proto:12:3: "google.protobuf.Timestamp" is not defined.
Run Code Online (Sandbox Code Playgroud)
我的协议版本是:
protoc --version
libprotoc 3.11.3
Run Code Online (Sandbox Code Playgroud)
我已遵循本指南并查看了此问题。如何导入众所周知的类型?我需要下载其他东西吗?我如何知道我当前安装的确切已知类型是什么?谢谢你。