Giter Site home page Giter Site logo

mycontext's Introduction

自己实现一个context包

###博客文章链接 知乎 csdn

2019.11.22 23:16
已经基本实现,但是目前存在几个问题

  1. 个人感觉还依然存在一些bug,不合理的地方
  2. 核心的问题就是并发情况下的处理,互斥锁的是否加对

感悟

想要写一份考虑周全,优化好,健壮的代码并不容易,有经验则更容易 没经验估计得试错才发现一些隐藏的bug,需要全局的考虑才能知道哪些 地方可能容易出错,所以明天准备整理下代码,修正一些不合理的地方 我觉得这也是个人需要去提升一个方向,如何做到代码的健壮性,性能好 而不是一味只追求实现就行。

找到的bug集合(个人实现过程)

  1. 由于sync.Mutex导致的死锁问题,cancel中详细解释了原因

一些发现

  1. 使用了大量的懒加载的技术,即不先初始化变量,用到的时候才初始化
    应该处于资源性能的考虑吧

附:官方使用context建议

  1. Do not store Contexts inside a struct type; instead, pass a Context explicitly to each function that needs it. The Context should be the first parameter, typically named ctx.
  2. Do not pass a nil Context, even if a function permits it. Pass context.TODO if you are unsure about which Context to use.
  3. Use context Values only for request-scoped data that transits processes and APIs, not for passing optional parameters to functions.
  4. The same Context may be passed to functions running in different goroutines; Contexts are safe for simultaneous use by multiple goroutines.

个人使用context建议

  1. 不要嵌套过多的context,构建非常复杂的context树,这样你可能会疑惑到底该调用哪个
    cancel,除非你真的非常理解context的内部机制

mycontext's People

Contributors

colin0000007 avatar

Watchers

 avatar

Forkers

linshanyu

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.