<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          技術(shù)閱讀周刊第十四期:常用的 Git 配置

          共 7608字,需瀏覽 16分鐘

           ·

          2024-04-11 05:19

          技術(shù)閱讀周刊,每周更新。

          歷史更新

          How I write HTTP services in Go after 13 years d9c40a0e7e5df14e33879c9d9b3ce564.webp
          1. 使用NewServer函數(shù)構(gòu)建服務(wù)實例,利用依賴注入方式將所有的依賴參數(shù)包含進(jìn)來。
                
                func NewServer(
           logger *Logger
           config *Config
           commentStore *commentStore
           anotherStore *anotherStore
          )
           http.Handler
           {
           mux := http.NewServeMux()
           addRoutes(
            mux,
            Logger,
            Config,
            commentStore,
            anotherStore,
           )
           var handler http.Handler = mux
           handler = someMiddleware(handler)
           handler = someMiddleware2(handler)
           handler = someMiddleware3(handler)
           return handler
          }
          1. 在routes.go文件中統(tǒng)一定義所有路由函數(shù)。
                
                func addRoutes(
           mux                 *http.ServeMux,
           logger              *logging.Logger,
           config              Config,
           tenantsStore        *TenantsStore,
           commentsStore       *CommentsStore,
           conversationService *ConversationService,
           chatGPTService      *ChatGPTService,
           authProxy           *authProxy
          )
           {
           mux.Handle("/api/v1/", handleTenantsGet(logger, tenantsStore))
           mux.Handle("/oauth2/", handleOAuth2Proxy(logger, authProxy))
           mux.HandleFunc("/healthz", handleHealthzPlease(logger))
           mux.Handle("/", http.NotFoundHandler())
          }
          1. 主函數(shù)只調(diào)用run函數(shù)來運(yùn)行服務(wù)
                
                func run(ctx context.Context, w io.Writer, args []string) error {
           ctx, cancel := signal.NotifyContext(ctx, os.Interrupt)
           defer cancel()

           // ...
          }

          func main() {
           ctx := context.Background()
           if err := run(ctx, os.Stdout, os.Args); err != nil {
            fmt.Fprintf(os.Stderr, "%s\n", err)
            os.Exit(1)
           }
          }
          1. 返回閉包 handle
                
                // handleSomething handles one of those web requests
          // that you hear so much about.
          func handleSomething(logger *Logger) http.Handler {
           thing := prepareThing()
           return http.HandlerFunc(
            func(w http.ResponseWriter, r *http.Request) {
             // use thing to handle request
             logger.Info(r.Context(), "msg""handleSomething")
            }
           )
          }
          1. 定義通用的encode和decode函數(shù)
                
                func encode[T any](w http.ResponseWriter, r *http.Request, status int, v T) error {
           w.Header().Set("Content-Type""application/json")
           w.WriteHeader(status)
           if err := json.NewEncoder(w).Encode(v); err != nil {
            return fmt.Errorf("encode json: %w", err)
           }
           return nil
          }

          func decode[T any](r *http.Request) (T, error) {
           var v T
           if err := json.NewDecoder(r.Body).Decode(&v); err != nil {
            return v, fmt.Errorf("decode json: %w", err)
           }
           return v, nil
          }
          1. 提供一個抽象的 Validator 接口用于驗證
                
                
          // Validator is an object that can be validated.
          type Validator interface {
           // Valid checks the object and returns any
           // problems. If len(problems) == 0 then
           // the object is valid.
           Valid(ctx context.Context) (problems map[string]string)
          }

          func decodeValid[T Validator](r *http.Request) (T, map[string]string, error) {
           var v T
           if err := json.NewDecoder(r.Body).Decode(&v); err != nil {
            return v, nil, fmt.Errorf("decode json: %w", err)
           }
           if problems := v.Valid(r.Context()); len(problems) > 0 {
            return v, problems, fmt.Errorf("invalid %T: %d problems", v, len(problems))
           }
           return v, nilnil
          }

          自定義校驗需要實現(xiàn) Validator 接口。

          1. 使用 Once 延遲調(diào)用來提高啟動性能。
                
                func handleTemplate(files string...) http.HandlerFunc {
           var (
            init    sync.Once
            tpl     *template.Template
            tplerr  error
           )
           return func(w http.ResponseWriter, r *http.Request) {
            init.Do(func(){
             tpl, tplerr = template.ParseFiles(files...)
            })
            if tplerr != nil {
             http.Error(w, tplerr.Error(), http.StatusInternalServerError)
             return
            }
            // use tpl
           }
          }
          What is OpenTelemetry? ceee720f737066f3a21c4e5dd2a6ba56.webp

          這是一篇 OTel 的科普文章

          OpenTelemetry 提供一個統(tǒng)一、可擴(kuò)展的框架,用于收集、分析和觀察分布式系統(tǒng)的性能數(shù)據(jù)。它包括一組API、庫、代理和收集器,這些組件可以跨多種編程語言和平臺實現(xiàn)對應(yīng)用程序的監(jiān)控。

          OpenTelemetry 整合 OpenTracing 和 OpenCensus。

          ebf03b7f9d8e524f4eed8e3dab583ae4.webp

          2019年,兩個社區(qū)進(jìn)行了合并。

          同時 OTel 具備以下特征:

          1. 統(tǒng)一性:OpenTelemetry 提供了一個統(tǒng)一的API,使得開發(fā)者可以在不同的編程語言和框架中以一致的方式實現(xiàn)監(jiān)控。

          2. 可擴(kuò)展性:可以編寫自己的擴(kuò)展來滿足個性化需要

          3. 跨平臺:OpenTelemetry 支持多種編程語言,如 Java、Python、Go、.NET 等,以及多種云服務(wù)和容器平臺。

          4. 社區(qū)驅(qū)動:作為一個開源項目,OpenTelemetry 由一個活躍的社區(qū)支持,社區(qū)成員貢獻(xiàn)代碼、文檔和最佳實踐。

          5. 與現(xiàn)有工具的兼容性:OpenTelemetry 設(shè)計時考慮了與現(xiàn)有監(jiān)控工具的兼容性,如 Prometheus、Jaeger、Zipkin 等,這使得它可以輕松地集成到現(xiàn)有的監(jiān)控基礎(chǔ)設(shè)施中。

          提供了一種名為:OTLP(OpenTelemetry Protocol)的通訊協(xié)議,基于 gRPC。

          使用該協(xié)議用于客戶端與 Collector 采集器進(jìn)行交互。

          Collector 是 OpenTelemetry 架構(gòu)中的一個關(guān)鍵組件,它負(fù)責(zé)接收、處理和導(dǎo)出數(shù)據(jù)(Trace/log/metrics)。

          073b0ada92f1acabecc6def999370aaa.webp

          它可以接受從客戶端發(fā)出的數(shù)據(jù)進(jìn)行處理,同時可以導(dǎo)出為不同格式的數(shù)據(jù)。

          總的來說 OTel 是可觀測系統(tǒng)的新標(biāo)準(zhǔn),基于它可以兼容以前使用的 Prometheus、 victoriametrics、skywalking 等系統(tǒng),同時還可以靈活擴(kuò)展,不用與任何但一生態(tài)或技術(shù)棧進(jìn)行綁定。

          Popular git config options a5cd93811ace1c141886fafbe47d5ecf.webp

          本文總結(jié)了一些常用的 git 配置

          1. pull.ff onlypull.rebase true:這兩個選項都可以避免在執(zhí)行git pull時意外創(chuàng)建合并提交,特別是當(dāng)上游分支已經(jīng)發(fā)生了變化的時候。

          2. merge.conflictstyle diff3:這個選項使得合并沖突更易于閱讀,通過在沖突中顯示原始代碼版本,幫助用戶更好地解決沖突。

          3. rebase.autosquash truerebase.autostash true:這些選項使得修改舊提交變得更容易,并且自動處理stash。

          4. push.default simplepush.default current:這些選項告訴git push自動推送當(dāng)前分支到同名的遠(yuǎn)程分支。

          5. init.defaultBranch main:創(chuàng)建新倉庫時,默認(rèn)創(chuàng)建main分支而不是master分支。

          6. commit.verbose true:在提交時顯示整個提交差異。

          7. rerere.enabled true:啟用rerere功能,自動解決沖突

          8. help.autocorrect:設(shè)置自動矯正的級別,以自動運(yùn)行建議的命令。

          9. core.pager delta:設(shè)置Git使用的分頁器,例如使用delta來查看帶有語法高亮的diff。

          10. diff.algorithm histogram:設(shè)置Git的diff算法,以改善函數(shù)重排時的diff顯示。

          文章鏈接:

          • https://grafana.com/blog/2024/02/09/how-i-write-http-services-in-go-after-13-years/
          • https://codeboten.medium.com/what-is-opentelemetry-6a7e5c6901c5
          • https://jvns.ca/blog/2024/02/16/popular-git-config-options/

          往期推薦

          【譯】Apache Pulsar 3.2.0 發(fā)布

          我的 2023

          手把手教你為開源項目貢獻(xiàn)代碼

          得物云原生全鏈路追蹤Trace2.0架構(gòu)實踐

           

          4dc95fa25e0d6497bf9229740bc56323.webp

          點(diǎn)分享

          621f76fcf6a50573fbca79d98e86414a.webp

          點(diǎn)收藏

          1e85fb313d9753077cb2c470ca65d678.webp

          點(diǎn)點(diǎn)贊

          d251a6b70dc38a2bd46717f8b6c8cf6a.webp

          點(diǎn)在看

           

          瀏覽 39
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产精品美女久久久久AV夜色 | 成人免费一级毛片在线播放视频 | 青娱乐97 | 骚色综合| 国产乱伦AV无码 |