我在main
程序包中有两个策略,它们像这样调用同一实例(本地运行的OPA服务器):
package main
get[response] {
response := http.send({
"method" : "GET",
"url": "http://localhost:8181/v1/data/example"
})
}
put[response] {
response := http.send({
"method" : "PUT",
"url": "http://localhost:8181/v1/data/example",
"body": { "example": true }
})
}
我这样运行我的OPA服务器:
opa run -w -s main.rego --log-level debug --log-format text
[当我卷曲get
策略时,得到200响应:
$ curl -X POST localhost:8181/v0/data/main/get
[{"body":{},"raw_body":"{}","status":"200 OK","status_code":200}]
但是,当我卷曲放置策略时,它会在5秒后超时:
$ curl -X POST localhost:8181/v0/data/main/put
{
"code": "internal_error",
"message": "error(s) occurred while evaluating query",
"errors": [
{
"code": "eval_builtin_error",
"message": "http.send: Put http://localhost:8181/v1/data/example: net/http: request canceled (Client.Timeout exceeded while awaiting headers)",
"location": {
"file": "main.rego",
"row": 11,
"col": 14
}
}
]
}
[有人知道为什么针对OPA实例的PUT
而不是GET
请求会发生这种情况吗?
与此同时,类似的卷曲效果很好:
curl -X PUT -d "{}" http://localhost:8181/v1/data/example
[我知道这是一个奇怪/糟糕的'用例',但出于好奇,我想问一下,以便更好地了解rego http功能的状况。
OPA当前支持多个并发读取事务(例如,策略查询)和一个并发写入事务(例如,更新数据)。但是,当提交写入事务时,OPA会等到读取器完成为止。
由于您正在从策略查询内部执行写操作,因此写操作的提交将被阻止。这就是为什么您看到带有put
规则的超时的原因。您看不到get
规则超时,因为没有写事务(所以没有任何块。)
这是OPA的实现细节。理论上,OPA可以支持多个并发的读取器和写入器,在这种情况下,您可以从策略查询内部修改数据。这不是一个高优先级的功能。
也就是说,我们不建议这样做。