Skip to content

Commit

Permalink
Merge branch 'develop' into chore/owner-channel
Browse files Browse the repository at this point in the history
  • Loading branch information
joway authored Dec 19, 2023
2 parents dde0570 + 614d0af commit 7368362
Show file tree
Hide file tree
Showing 44 changed files with 1,617 additions and 247 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.17
go-version: 1.15

- uses: actions/cache@v2
with:
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/feishu-notify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ jobs:
See: https://github.com/${{ github.repository }}/pull/${{ github.event.pull_request.number }}
run: |
sudo apt update
sudo apt install wget -y
wget -q https://github.com/xiachufang/actions-feishu/releases/download/${{ env.ACTIONS_FEISHU_TAG }}/linux-amd64-actions-feishu.tar.gz
tar zxf linux-amd64-actions-feishu.tar.gz feishu
./feishu
Expand All @@ -49,6 +51,8 @@ jobs:
See: https://github.com/${{ github.repository }}/issues/${{ github.event.issue.number }}
run: |
sudo apt update
sudo apt install wget -y
wget -q https://github.com/xiachufang/actions-feishu/releases/download/${{ env.ACTIONS_FEISHU_TAG }}/linux-amd64-actions-feishu.tar.gz
tar zxf linux-amd64-actions-feishu.tar.gz feishu
./feishu
3 changes: 2 additions & 1 deletion .github/workflows/pr-benchdiff.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ jobs:
needs: [trigger]
if: needs.trigger.outputs.triggered == 'true'
runs-on: self-hosted
timeout-minutes: 30
env:
# In markdown URL syntax
RUNS_URL: '[${{ github.workflow }} #${{ github.run_number }}](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})'
Expand Down Expand Up @@ -83,7 +84,7 @@ jobs:
- name: Setup go
uses: actions/setup-go@v2
with:
go-version: 1.17
go-version: 1.15
- name: Benchdiff
uses: WillAbides/[email protected]
id: diff
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pr-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.17
go-version: 1.15

- uses: actions/cache@v2
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/push-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.17
go-version: 1.15

- uses: actions/cache@v2
with:
Expand Down
6 changes: 5 additions & 1 deletion cloud/metainfo/README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ API 参考

1. 出于兼容性和普适性,元信息的形式为字符串的 key value 对。
2. 空串作为 key 或者 value 都是无效的。
3. 由于 context 的特性,程序对 metainfo 的增删改只会对拥有相同的 contetxt 或者其子 context 的代码可见。
3. 由于 context 的特性,程序对 metainfo 的增删改只会对拥有相同的 context 或者其子 context 的代码可见。

**常量**

Expand All @@ -48,6 +48,8 @@ metainfo 包提供了几个常量字符串前缀,用于无 context(例如网
- 从 context 里获取指定 key 的 transient 数据(包括 transient-upstream 数据)。
- `GetAllValues(ctx context.Context) map[string]string`
- 从 context 里获取所有 transient 数据(包括 transient-upstream 数据)。
- `RangeValues(ctx context.Context, f func(k, v string) bool)`
- 从 context 里基于 f 过滤获取 transient 数据(包括 transient-upstream 数据)。
- `WithValue(ctx context.Context, k string, v string) context.Context`
- 向 context 里添加一个 transient 数据。
- `DelValue(ctx context.Context, k string) context.Context`
Expand All @@ -56,6 +58,8 @@ metainfo 包提供了几个常量字符串前缀,用于无 context(例如网
- 从 context 里获取指定 key 的 persistent 数据。
- `GetAllPersistentValues(ctx context.Context) map[string]string`
- 从 context 里获取所有 persistent 数据。
- `RangePersistentValues(ctx context.Context, f func(k, v string) bool)`
- 从 context 里基于 f 过滤获取 persistent 数据。
- `WithPersistentValue(ctx context.Context, k string, v string) context.Context`
- 向 context 里添加一个 persistent 数据。
- `DelPersistentValue(ctx context.Context, k string) context.Context`
Expand Down
2 changes: 1 addition & 1 deletion cloud/metainfo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ API Reference

Package metainfo provides serveral string prefixes to denote the kinds of meta information where context is not available (such as when transmiting data through network).

Typical codes for bussiness logic should never use prefixes. And frameworks that support metainfo may choose other approaches to achieve the same goal.
Typical codes for business logic should never use prefixes. And frameworks that support metainfo may choose other approaches to achieve the same goal.

- `PrefixPersistent`
- `PrefixTransient`
Expand Down
16 changes: 12 additions & 4 deletions cloud/metainfo/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package metainfo
import (
"context"
"strings"

"golang.org/x/net/http/httpguts"
)

// HTTP header prefixes.
Expand Down Expand Up @@ -107,18 +109,24 @@ func FromHTTPHeader(ctx context.Context, h HTTPHeaderCarrier) context.Context {

// ToHTTPHeader writes all metainfo into the given HTTP header.
// Note that this function does not call TransferForward inside.
// Any key or value that does not follow the HTTP specification
// will be discarded.
func ToHTTPHeader(ctx context.Context, h HTTPHeaderSetter) {
if ctx == nil || h == nil {
return
}

for k, v := range GetAllValues(ctx) {
k := HTTPPrefixTransient + CGIVariableToHTTPHeader(k)
h.Set(k, v)
if httpguts.ValidHeaderFieldName(k) && httpguts.ValidHeaderFieldValue(v) {
k := HTTPPrefixTransient + CGIVariableToHTTPHeader(k)
h.Set(k, v)
}
}

for k, v := range GetAllPersistentValues(ctx) {
k := HTTPPrefixPersistent + CGIVariableToHTTPHeader(k)
h.Set(k, v)
if httpguts.ValidHeaderFieldName(k) && httpguts.ValidHeaderFieldValue(v) {
k := HTTPPrefixPersistent + CGIVariableToHTTPHeader(k)
h.Set(k, v)
}
}
}
167 changes: 166 additions & 1 deletion cloud/metainfo/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,31 @@ func GetAllValues(ctx context.Context) (m map[string]string) {
return
}

// RangeValues calls f sequentially for each transient kv.
// If f returns false, range stops the iteration.
func RangeValues(ctx context.Context, f func(k, v string) bool) {
n := getNode(ctx)
if n == nil {
return
}

if cnt := len(n.stale) + len(n.transient); cnt == 0 {
return
}

for _, kv := range n.stale {
if !f(kv.key, kv.val) {
return
}
}

for _, kv := range n.transient {
if !f(kv.key, kv.val) {
return
}
}
}

// WithValue sets the value into the context by the given key.
// This value will be propagated to the next service/endpoint through an RPC call.
//
Expand Down Expand Up @@ -131,7 +156,22 @@ func GetAllPersistentValues(ctx context.Context) (m map[string]string) {
return
}

// WithPersistentValue sets the value info the context by the given key.
// RangePersistentValues calls f sequentially for each persistent kv.
// If f returns false, range stops the iteration.
func RangePersistentValues(ctx context.Context, f func(k, v string) bool) {
n := getNode(ctx)
if n == nil {
return
}

for _, kv := range n.persistent {
if !f(kv.key, kv.val) {
break
}
}
}

// WithPersistentValue sets the value into the context by the given key.
// This value will be propagated to the services along the RPC call chain.
func WithPersistentValue(ctx context.Context, k, v string) context.Context {
if len(k) == 0 || len(v) == 0 {
Expand Down Expand Up @@ -162,3 +202,128 @@ func DelPersistentValue(ctx context.Context, k string) context.Context {
}
return ctx
}

func getKey(kvs []string, i int) string {
return kvs[i*2]
}

func getValue(kvs []string, i int) string {
return kvs[i*2+1]
}

// CountPersistentValues counts the length of persisten KV pairs
func CountPersistentValues(ctx context.Context) int {
if n := getNode(ctx); n == nil {
return 0
} else {
return len(n.persistent)
}
}

// CountValues counts the length of transient KV pairs
func CountValues(ctx context.Context) int {
if n := getNode(ctx); n == nil {
return 0
} else {
return len(n.stale) + len(n.transient)
}
}

// WithPersistentValues sets the values into the context by the given keys.
// This value will be propagated to the services along the RPC call chain.
func WithPersistentValues(ctx context.Context, kvs ...string) context.Context {
if len(kvs)%2 != 0 {
panic("len(kvs) must be even")
}

kvLen := len(kvs) / 2

if ctx == nil || len(kvs) == 0 {
return ctx
}

var n *node
if m := getNode(ctx); m != nil {
nn := *m
n = &nn
n.persistent = make([]kv, len(m.persistent), len(m.persistent)+kvLen)
copy(n.persistent, m.persistent)
} else {
n = &node{
persistent: make([]kv, 0, kvLen),
}
}

for i := 0; i < kvLen; i++ {
key := getKey(kvs, i)
val := getValue(kvs, i)

if len(key) == 0 || len(val) == 0 {
continue
}

if idx, ok := search(n.persistent, key); ok {
if n.persistent[idx].val != val {
n.persistent[idx].val = val
}
} else {
n.persistent = append(n.persistent, kv{key: key, val: val})
}
}

return withNode(ctx, n)
}

// WithValue sets the values into the context by the given keys.
// This value will be propagated to the next service/endpoint through an RPC call.
//
// Notice that it will not propagate any further beyond the next service/endpoint,
// Use WithPersistentValues if you want to pass key/value pairs all the way.
func WithValues(ctx context.Context, kvs ...string) context.Context {
if len(kvs)%2 != 0 {
panic("len(kvs) must be even")
}

kvLen := len(kvs) / 2

if ctx == nil || len(kvs) == 0 {
return ctx
}

var n *node
if m := getNode(ctx); m != nil {
nn := *m
n = &nn
n.transient = make([]kv, len(m.transient), len(m.transient)+kvLen)
copy(n.transient, m.transient)
} else {
n = &node{
transient: make([]kv, 0, kvLen),
}
}

for i := 0; i < kvLen; i++ {
key := getKey(kvs, i)
val := getValue(kvs, i)

if len(key) == 0 || len(val) == 0 {
continue
}

if res, ok := remove(n.stale, key); ok {
n.stale = res
n.transient = append(n.transient, kv{key: key, val: val})
continue
}

if idx, ok := search(n.transient, key); ok {
if n.transient[idx].val != val {
n.transient[idx].val = val
}
} else {
n.transient = append(n.transient, kv{key: key, val: val})
}
}

return withNode(ctx, n)
}
Loading

0 comments on commit 7368362

Please sign in to comment.