Skip to content

Commit

Permalink
Merge branch 'cloudwego:main' into doc_context
Browse files Browse the repository at this point in the history
  • Loading branch information
chaoranz758 authored and li-jin-gou committed Jul 18, 2023
2 parents 4f905b1 + b6f0388 commit 9b9dae1
Show file tree
Hide file tree
Showing 10 changed files with 316 additions and 83 deletions.
10 changes: 4 additions & 6 deletions content/en/docs/hertz/tutorials/basic-feature/context/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@ type HandlerFunc func(c context.Context, ctx *RequestContext)

## Context Passing and Concurrency Security

1. Metadata Storage
### Metadata Storage

Both contexts have the ability to store values, and there is a simple basis for choosing which context to use: the lifecycle of the stored value and the chosen context should match.

2. Concurrent Security

`ctx` is mainly used to store request level variables, which are reclaimed upon completion of the request. It's characteristics are high query efficiency (with the underlying `map`), unsafe coroutines, and no implementation of the `context.Context` interface.

`c` is passed as a context between middleware `/handler`. Having all the semantics of `context.Context`, coroutine security. For all places that require the `context.Context` interface as input parameters, simply pass `c` directly.
`c` is passed as a context between middleware `/handler`. it is coroutine security. For all places that require the `context.Context` interface as input parameters, simply pass `c` directly.

3. Asynchronous Delivery
### concurrent security

In addition, if there is a scenario of asynchronous transmission of `ctx`, hertz also provides the `ctx.Copy()` interface, making it easy for businesses to obtain a secure copy of the protocol.
In addition, if there are scenarios where `ctx` is passed asynchronously or used concurrently, hertz also provides the `ctx.Copy()` interface, which makes it easy for the business to get a coprocess-safe copy.
21 changes: 10 additions & 11 deletions content/zh/about/_index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,18 @@
<div class="about-project about-flex-box col-12">
<div class="about-flex-left">
<p>
CloudWeGo 的是一套由字节跳动<span class="about-b">服务框架</span>团队开源的、可快速构建<span class="about-b">企业级云原生微服务架构</span>
<span class="about-b">中间件</span>集合。
CloudWeGo 的是一套由字节跳动<span class="about-b">服务框架</span>团队开源的、可快速构建<span class="about-b">企业级云原生微服务架构</span>
<span class="about-b">中间件</span>集合。
</p>
<p>CloudWeGo 项目共同的特点是<span class="about-b">高性能、高扩展性、高可靠</span>,专注于微服务通信与治理。</p>
<p>CloudWeGo 包括 <span class="about-b">Kitex、Hertz、Volo、Netpoll</span> 等多个重点子项目,涵盖 Go 与 Rust
开发语言,上至框架下至网络库、编解码库、序列化库均是<span
class="about-b">自研,各个项目既可独立使用也可搭配使用</span>,并围绕这些项目,构建了完整的上下游生态。</p>
<p>CloudWeGo 包括 <span class="about-b">Kitex、Hertz、Volo、Netpoll</span> 等多个重点子项目,涵盖 Go 与 Rust 开发语言,
上至框架下至网络库、编解码库、序列化库均是<span class="about-b">自研,各个项目既可独立使用也可搭配使用</span>,并围绕这些项目,构建了完整的上下游生态。</p>
<p>生态项目链接:
<ul class="text-left">
<li><a href="https://github.com/kitex-contrib">Kitex-contrib</a></li>
<li><a href="https://github.com/hertz-contrib">Hertz-contrib</a></li>
<li><a href="https://github.com/volo-rs">Volo-rs</a></li>
</ul>
<ul class="text-left">
<li><a href="https://github.com/kitex-contrib">Kitex-contrib</a></li>
<li><a href="https://github.com/hertz-contrib">Hertz-contrib</a></li>
<li><a href="https://github.com/volo-rs">Volo-rs</a></li>
</ul>
</p>
<p>更多项目信息和功能介绍请查看项目<a href="/zh/docs/">文档</a>,更多技术深度解读请查看项目<a href="/zh/blog/news/">新闻</a>
</p>
Expand Down Expand Up @@ -89,7 +88,7 @@
<p>如需参与贡献,请阅读<a href="https://github.com/cloudwego/community/blob/main/CONTRIBUTING.md">贡献者指南</a><a
href="https://github.com/cloudwego/community/blob/main/COMMUNITY_MEMBERSHIP.md">贡献者体系</a>说明,建议从各项目
Good First Issue 开始贡献的第一步。Committer 及以上角色将获得社区颁发的证书和赠与的社区周边礼品。</p>
<p>更多社区相关内容,请访问https://github.com/cloudwego/community </p>
<p>更多社区相关内容,请访问 <a href="https://github.com/cloudwego/community">community</a> 仓库</p>
</div>
</div>
{{< /blocks/section>}}
10 changes: 4 additions & 6 deletions content/zh/docs/hertz/tutorials/basic-feature/context/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@ type HandlerFunc func(c context.Context, ctx *RequestContext)

## 上下文传递与并发安全

1. 元数据存储
### 元数据存储

`context.Context``RequestContext` 都有存储值的能力,具体选择使用哪一个上下文有个简单依据:所储存值的生命周期和所选择的上下文要匹配。

2. 并发安全

`ctx` 主要用来存储请求级别的变量,请求结束就回收了,特点是查询效率高(底层是 `map`),协程不安全,且未实现 `context.Context` 接口。

`c` 作为上下文在中间件 `/handler` 之间传递,拥有 `context.Context` 的所有语义,协程安全。所有需要 `context.Context` 接口作为入参的地方,直接传递 `c` 即可。
`c` 作为上下文在中间件 `/handler` 之间传递,协程安全。所有需要 `context.Context` 接口作为入参的地方,直接传递 `c` 即可。

3. 异步传递
### 协程安全

除此之外,如果存在异步传递 `ctx` 的场景,hertz 也提供了 `ctx.Copy()` 接口,方便业务能够获取到一个协程安全的副本。
除此之外,如果存在异步传递 `ctx` 或并发使用 `ctx` 的场景,hertz 也提供了 `ctx.Copy()` 接口,方便业务能够获取到一个协程安全的副本。
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: "连接类型"
date: 2021-09-28
weight: 5
keywords: ["Kitex", "短连接", "长链接", "链接多路复用"]
keywords: ["Kitex", "短连接", "长连接", "连接多路复用"]
description: "Kitex 支持短连接、长连接池、连接多路复用以及连接池状态监控。"
---

Expand Down
1 change: 1 addition & 0 deletions layouts/partials/scripts.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@
<!-- scripts for prism -->
<script src='{{ "/js/prism.js" | relURL }}'></script>
{{ end }}
<script src='{{ "/js/tabpane-persist.js" | relURL }}'></script>
{{ partial "hooks/body-end.html" . }}
20 changes: 20 additions & 0 deletions layouts/shortcodes/codetab.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{{- $index := .Ordinal -}}
<!-- Make sure that we are enclosed within a tabs shortcode block -->
{{- if ne .Parent.Name "tabs" -}}
{{- errorf "codetab must be used within a tabs block" -}}
{{- end -}}

<!-- Generate the unique ID based on the enclosing tabs .Ordinal -->
{{- $guid := printf "tabs-%d" .Parent.Ordinal -}}
<!-- Trim any leading and trailing newlines from .Inner, this avoids
spurious lines during syntax highlighting -->

{{- $entry := .Parent.Get $index -}}
{{- $entry := lower $entry -}}

{{- $tabid := printf "%s-%s-tab" $guid $entry | anchorize -}}
{{- $entryid := printf "%s-%s" $guid $entry | anchorize -}}

<div class="tab-pane fade{{ if eq $index 0 }} show active{{ end }}" id="{{ $entryid }}" role="tabpanel" aria-labelled-by="{{ $tabid }}">
{{- .Inner -}}
</div>
77 changes: 52 additions & 25 deletions layouts/shortcodes/tab.html
Original file line number Diff line number Diff line change
@@ -1,28 +1,55 @@
<!-- Make sure that we are enclosed within a tabpane shortcode block -->
{{ with $.Parent }}
{{- if ne $.Parent.Name "tabpane" -}}
{{- errorf "tab must be used within a tabpane block" -}}
{{- end -}}
{{- end -}}
{{- /* Make sure that we are enclosed within a tabpane shortcode block */ -}}

<!-- Prefill header if not given as parameter -->
{{ $header := default (printf "Tab %v" ( add $.Ordinal 1)) (.Get "header") }}
{{ with $.Parent -}}
{{ if ne $.Parent.Name "tabpane" -}}
{{ errorf "Found shortcode %q enclosed inside a %q block, must be enclosed inside a %q block. Error position: %s" $.Name $.Parent.Name "tabpane" $.Position -}}
{{ end -}}
{{ else -}}
{{ errorf "shortcode %q must be enclosed inside a %q block, but no parent block was found. Error position: %s" $.Name "tabpane" $.Position -}}
{{ end -}}

<!-- store all tab info in dict tab -->
{{ $tab := dict "header" $header }}
{{ with $.Get "lang" }}
{{ $tab = merge $tab (dict "language" ($.Get "lang")) }}
{{ end }}
{{ with $.Get "highlight" }}
{{ $tab = merge $tab (dict "highlight" ($.Get "highlight")) }}
{{ end }}
{{ with $.Inner }}
<!-- Trim any leading and trailing newlines from .Inner, this avoids
spurious lines during syntax highlighting -->
{{ $tab = merge $tab (dict "content" (trim $.Inner "\n")) }}
{{ end }}
{{ $header := "Tab" -}}
{{ if and (not .IsNamedParams) (.Get 0) -}}
{{ $header = (.Get 0) -}}
{{ else -}}
{{/* Prefill header if not given as named or unnamed parameter */ -}}
{{ $header = .Get "header" | default (printf "Tab %v" .Ordinal) -}}
{{ end -}}

<!-- add dict tab to parent's scratchpad -->
{{ with .Parent }}
{{- $.Parent.Scratch.SetInMap "tabs" (printf "%v" $.Ordinal) $tab -}}
{{ end }}
{{/* store all tab info in dict tab */ -}}
{{ $tab := dict "header" $header -}}
{{ with $.Get "lang" -}}
{{ $tab = merge $tab (dict "language" ($.Get "lang")) -}}
{{ end -}}
{{ with $.Get "highlight" -}}
{{ $tab = merge $tab (dict "highlight" ($.Get "highlight")) -}}
{{ end -}}
{{ with $.Get "text" -}}
{{ if ne ( printf "%T" . ) "bool" -}}
{{ errorf "Shortcode %q: boolean value expected for parameter %q, but got %s. Error position: %s" $.Name "text" (printf "%T" .) $.Position -}}
{{ end -}}
{{ $tab = merge $tab (dict "text" ($.Get "text")) -}}
{{ end -}}
{{ with $.Get "right" -}}
{{ if ne ( printf "%T" . ) "bool" -}}
{{ errorf "Shortcode %q: boolean value expected for parameter %q, but got %s. Error position: %s" $.Name "right" (printf "%T" .) $.Position -}}
{{ end -}}
{{ $tab = merge $tab (dict "rightpush" ($.Get "right")) -}}
{{ end -}}
{{ with $.Get "disabled" -}}
{{ if ne ( printf "%T" . ) "bool" -}}
{{ errorf "Shortcode %q: boolean value expected for parameter %q, but got %s. Error position: %s" $.Name "disabled" (printf "%T" .) $.Position -}}
{{ end -}}
{{ $tab = merge $tab (dict "disabled" ($.Get "disabled")) -}}
{{ end -}}

{{ with $.Inner -}}
{{/* Trim any leading and trailing newlines from .Inner, this avoids
spurious lines during syntax highlighting */ -}}
{{ $tab = merge $tab (dict "content" $.Inner) -}}
{{ end -}}

{{/* add dict tab to parent's scratchpad */ -}}
{{ with .Parent -}}
{{ $.Parent.Scratch.SetInMap "tabs" (printf "%02v" $.Ordinal) $tab -}}
{{ end -}}
Loading

0 comments on commit 9b9dae1

Please sign in to comment.