<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by It is Zion on Medium]]></title>
        <description><![CDATA[Stories by It is Zion on Medium]]></description>
        <link>https://medium.com/@lebum?source=rss-fba40115516a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*PGcA06sOPrYbj4Q9PiIxGQ.jpeg</url>
            <title>Stories by It is Zion on Medium</title>
            <link>https://medium.com/@lebum?source=rss-fba40115516a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 10:46:27 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@lebum/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Product Managers 1 — End-to-End System]]></title>
            <link>https://lebum.medium.com/product-managers-1-end-to-end-system-2d0b74202969?source=rss-fba40115516a------2</link>
            <guid isPermaLink="false">https://medium.com/p/2d0b74202969</guid>
            <category><![CDATA[agile]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[product-manager]]></category>
            <category><![CDATA[waterfall]]></category>
            <category><![CDATA[lean]]></category>
            <dc:creator><![CDATA[It is Zion]]></dc:creator>
            <pubDate>Wed, 30 Nov 2022 11:52:30 GMT</pubDate>
            <atom:updated>2022-11-30T11:58:40.002Z</atom:updated>
            <content:encoded><![CDATA[<h3>Product Managers 1 — End-to-End System</h3><p>I’m going to write a product manager blog series, I hope it will be useful for people who want to learn<br>This product manager series will first talk about the product development system, and then explain how to do each step in this system separately</p><p><strong>What is the goal of product development?</strong><br>To deliver solutions that solve user or business problems and achieve business goals through technical implementation.</p><p><strong>What is the purpose of product development?</strong><br>The purpose of product development is not to develop a product, but to be able to solve the needs of users.</p><p><strong>What is a requirement?</strong><br>The user’s current problem.</p><p><strong>Problem Domain and Implementation Domain</strong><br>Business goals, users or business problems, solutions belong to the problem domain.<br>Technical implementation belongs to implementation domain<br>The process of product development is the transformation from the problem domain to the implementation domain through organized activities.<br>The first step is to decompose the requirements in the problem domain, so that the decomposed requirements can achieve separate flow and continuous delivery, and further decomposition in the implementation phase is normal.</p><p><strong>Waterfall</strong><br>The core idea of waterfall is to build the product first, and then the customer will come.<br>Waterfall moves the product forward in an orderly fashion, delivering value at the end, i.e.:<br>Requirements -&gt; Design -&gt; Development -&gt; Testing -&gt; Value delivered to the user<br>But due to Moore’s law, the earlier the product is delivered, the higher the value of the product.<br>The time when the team has accumulated the most knowledge about the project is at the late stage of the product, and the time when the project starts is the time when the knowledge is least sufficient, which is the paradox between knowledge accumulation and decision time under waterall.</p><p><strong>Agile</strong><br>The point of agile is to make up for the shortcomings of waterfall with iterative development:<br>Deliver value faster: Faster does not mean faster in absolute terms, but earlier relative to waterfall, delivering a portion of the value in each iteration, which is more delivery than last, according to the inverse Moore’s Law.<br>Flexibility to respond to change: start with some initial solutions, then update the strategy each iteration to build new knowledge.<br>Delivering value faster and being flexible to change is the goal of agile.</p><p><strong>Difference between agile and waterfall</strong><br>The difference between agile and waterfall is like drawing a character, waterfall is to start from the head, and then draw the rest of the body after the head is drawn well enough, while agile is to draw the whole outline of the body as a skeleton first(what is commonly known as MVP), and then embellish the body, with additions and deletions in the process.<br>Anyone who knows about sketching knows that drawing like waterfall is not a good drawing.</p><p><strong>Lean</strong><br>Iteration in the development phase, the process is from requirements analysis to test acceptance, but in reality there is still business planning and product definition before requirements analysis, and after test acceptance there is still solution implementation and validation, so only the development phase iteration process from a macro point of view is still waterfall, subdivided into water-scrum-fall, the delivery of value is still very late to complete.<br>So it is not enough to achieve only the iteration of the development model, you need to achieve end-to-end delivery of value, end-to-end refers to the user’s problem to the delivery of user solutions.<br>To visualize the whole process from end to end, a kanban board is a good choice.</p><p><strong>The lean system</strong><br>The Lean System has three levels:<br>Objectives, Principles, and Practices (divided into management practices and engineering practices)<br>Objective: To deliver useful value smoothly and with high quality.<br>Principles: Explore and discover useful value, focus and improve the efficiency of value flow.<br>Management practices: Innovation practices (business model design, validation step planning, lean product design, qualitative validation, lean data analysis, impact maps), lean requirements analysis and management (scenario analysis, use case analysis, domain modeling, requirements maps, release planning, instantiated requirements), lean Kanban methods (visualize value flow, show process planning, control WIP, manage work flow, Creating feedback and continuous improvement).<br>Engineering practices: continuous software delivery, sustainable and efficient development, rapid business development (continuous integration, continuous refactoring, etc.).<br>Smoothness of purpose means that the value delivery process should be smooth, delivering user value in the shortest possible time, not intermittently and problematically; high quality means meeting requirements and avoiding unnecessary errors, which does not conflict with the necessary trial and error for exploration; useful value means that the delivered value should meet market and user requirements and have business impact, contributing to organizational performance.</p><p><strong>The “Think — Make — Check” cycle</strong><br>The original process of lean is “build — check — learn” cycle, but directly from the build cost a lot, so changed to “think — make — check” cycle, “build — check — learn” cycle that the customer development process omitted, suitable for a large company with a lot of users and the ability to quickly develop the product.</p><p><strong>The end-to-end process of lean</strong><br>1. Identify the user problem from the target (Select a driver that you want to improve, set a completion standard for the driver based on the business model, and first quantitatively identify the user’s problem from the target through data analysis (if there is no data, then you can analyze the competitor’s data)) (problem domain).<br>2. from user problem to solution (user story map, UX, functional structure map) (problem domain).<br>3. development and testing (MVP, after each iteration before developing a feature, if the cost of implementing the full version is small, then you can implement the beta version first, if the cost is large, implement the demo version first) (implementation domain).<br>4. deploy (deploy not immediately release) (implementation domain).<br>5. release (ABn test) (see if the solution meets the driver metrics and guardrail metrics, if it does, go back to step 3 and implement the full version, if it doesn’t, do customer development and then go back to step 2 and try again or just give up) (implementation domain).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2d0b74202969" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding and usage of context in Golang]]></title>
            <link>https://lebum.medium.com/understanding-and-usage-of-context-in-golang-6a460f9e8d2f?source=rss-fba40115516a------2</link>
            <guid isPermaLink="false">https://medium.com/p/6a460f9e8d2f</guid>
            <category><![CDATA[golang-tutorial]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[programming-languages]]></category>
            <dc:creator><![CDATA[It is Zion]]></dc:creator>
            <pubDate>Wed, 30 Dec 2020 16:49:43 GMT</pubDate>
            <atom:updated>2021-03-29T09:25:20.042Z</atom:updated>
            <content:encoded><![CDATA[<p>When a goroutine can start other goroutines, and those goroutines start other goroutines, and so on, then the first goroutine should be able to send cancellation signals to all embedded goroutines.</p><p>The sole purpose of the context package is to perform cancellation signals between goroutines, regardless of how they were generated.</p><p>The interface of the Context is defined as:</p><pre>type Context interface {<br>    Deadline() (deadline time.Time, ok bool)<br>    Done() &lt;- chan struct{}<br>    Err() error<br>    Value(key interface{}) interface{}<br>}</pre><ul><li>Deadline: The first value is the deadline, at which point the Context will automatically trigger the Cancel action. The second value is a boolean value, true means the deadline is set, false means the deadline is not set. If the deadline is not set, you have to call the cancel function manually to cancel the Context.</li><li>Done: return a read-only channel (only after being canceled), type struct{}, when this channel is readable, it means the parent context has initiated the cancel request, according to this signal, the developer can do some cleanup actions, exit the goroutine</li><li>Err: returns the reason why the context was cancelled</li><li>Value: returns the value bound to the Context, it is a key-value pair, so you need to pass a Key to get the corresponding value, this value is thread-safe</li></ul><p>To create a Context, you must specify a parent Context.<br>Two built-in contexts, background and todo, serve as the top-level parent context:</p><pre>var (<br>    background = new(emptyCtx)<br>    todo = new(emptyCtx)<br>)</pre><pre>func Background() Context {<br>    return background<br>}</pre><pre>func TODO() Context {<br>    return todo<br>}</pre><p>Background, mainly used in the main function, initialization and test code, is the top-level Context of the tree structure, the root Context, which cannot be cancelled.<br>TODO, when you don’t know what Context to use, you can use this.</p><p>They are both essentially of type emptyCtx, both are non-cancelable, neither has a set deadline, and neither carries any value for the Context:</p><pre>type emptyCtx int</pre><pre>func (_ *emptyCtx) Deadline() (deadline time.Time, ok bool) {<br>    return<br>}</pre><pre>func (_ *emptyCtx) Done() &lt;- chan struct{} {<br>    return nil<br>}</pre><pre>func (_ *emptyCtx) Err() error {<br>    return nil<br>}</pre><pre>func (*emptyCtx) Value(key interface{}) interface{} {<br>    return nil<br>}</pre><p>The context package also has several common functions:</p><ul><li><strong>func WithCancel(parent Context) (ctx Context, cancel CancelFunc)</strong></li><li><strong>func WithDeadline(parent Context, deadline time.Time) (Context, CancelFunc)</strong></li><li><strong>func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc)</strong></li><li><strong>func WithValue(parent Context, key, val interface{}) Context</strong></li></ul><p>Note that these methods mean that the context can be inherited once to achieve one more function, for example, using the WithCancel function to pass in the root context, it creates a child context, which has an additional function of cancel context, then use this context(context01) as the parent context, and pass it as the first parameter to the WithDeadline function, the child context(context02) is obtained, compared to the child context(context01), It has an additional function to cancel the context automatically after the deadline.</p><h3><strong>WithCancel</strong></h3><p>For channel, although channel can also notify many nested goroutines to exit, channel is not thread-safe, while context is thread-safe.</p><p>For example:</p><pre>package main</pre><pre>import (<br>    &quot;runtime&quot;<br>    &quot;fmt&quot;<br>    &quot;time&quot;<br>    &quot;context&quot;<br>)</pre><pre>func monitor2(ch chan bool, index int) {<br>    for {<br>        select {<br>        case v := &lt;- ch:<br>            fmt.Printf(&quot;monitor2: %v, the received channel value is: %v, ending\n&quot;, index, v)<br>            return<br>        default:<br>            fmt.Printf(&quot;monitor2: %v in progress...\n&quot;, index)<br>            time.Sleep(2 * time.Second)<br>        }<br>    }<br>}</pre><pre>func monitor1(ch chan bool, index int) {<br>    for {<br>        go monitor2(ch, index)<br>        select {<br>        case v := &lt;- ch:<br>            // this branch is only reached when the ch channel is closed, or when data is sent(either true or false)<br>            fmt.Printf(&quot;monitor1: %v, the received channel value is: %v, ending\n&quot;, index, v)<br>            return<br>        default:<br>            fmt.Printf(&quot;monitor1: %v in progress...\n&quot;, index)<br>            time.Sleep(2 * time.Second)<br>        }<br>    }<br>}</pre><pre>func main() {<br>    var stopSingal chan bool = make(chan bool, 0)</pre><pre>    for i := 1; i &lt;= 5; i = i + 1 {<br>        go monitor1(stopSingal, i)<br>    }</pre><pre>    time.Sleep(1 * time.Second)</pre><pre>    // close all gourtines<br>    close(stopSingal)</pre><pre>    // waiting 10 seconds, if the screen does not display &lt;monitorX: xxxx in progress...&gt;, all goroutines have been shut down<br>    time.Sleep(10 * time.Second)</pre><pre>    println(runtime.NumGoroutine())<br>    println(&quot;main program exit!!!!&quot;)<br>}</pre><p>The result of the execution is :</p><pre>monitor1: 5 in progress...<br>monitor2: 5 in progress...<br>monitor1: 2 in progress...<br>monitor2: 2 in progress...<br>monitor2: 1 in progress...<br>monitor1: 1 in progress...<br>monitor1: 4 in progress...<br>monitor1: 3 in progress...<br>monitor2: 4 in progress...<br>monitor2: 3 in progress...<br>monitor1: 4, the received channel value is: false, ending<br>monitor1: 3, the received channel value is: false, ending<br>monitor2: 2, the received channel value is: false, ending<br>monitor2: 1, the received channel value is: false, ending<br>monitor1: 1, the received channel value is: false, ending<br>monitor2: 5, the received channel value is: false, ending<br>monitor2: 3, the received channel value is: false, ending<br>monitor2: 3, the received channel value is: false, ending<br>monitor2: 4, the received channel value is: false, ending<br>monitor2: 5, the received channel value is: false, ending<br>monitor2: 1, the received channel value is: false, ending<br>monitor1: 5, the received channel value is: false, ending<br>monitor1: 2, the received channel value is: false, ending<br>monitor2: 2, the received channel value is: false, ending<br>monitor2: 4, the received channel value is: false, ending<br>1<br>main program exit!!!!</pre><p>Here a channel is used to send end notifications to all goroutines, but the situation here is relatively simple, if in a complex project, suppose multiple goroutines have some kind of error and execute repeatedly, then it is possible to repeatedly close the channel or close the channel and then write values to it, thus triggering a runtime panic. This is why we use context to avoid these problems, using <strong>WithCancel</strong> as an example:</p><pre>package main</pre><pre>import (<br>    &quot;runtime&quot;<br>    &quot;fmt&quot;<br>    &quot;time&quot;<br>    &quot;context&quot;<br>)</pre><pre>func monitor2(ctx context.Context, number int) {<br>    for {<br>        select {<br>        case v := &lt;- ctx.Done():<br>            fmt.Printf(&quot;monitor: %v, the received channel value is: %v, ending\n&quot;, number,v)<br>            return<br>        default:<br>            fmt.Printf(&quot;monitor: %v in progress...\n&quot;, number)<br>            time.Sleep(2 * time.Second)<br>        }<br>    }<br>}</pre><pre>func monitor1(ctx context.Context, number int) {<br>    for {<br>        go monitor2(ctx, number)<br>        select {<br>        case v := &lt;- ctx.Done():<br>            // this branch is only reached when the ch channel is closed, or when data is sent(either true or false)<br>            fmt.Printf(&quot;monitor: %v, the received channel value is: %v, ending\n&quot;, number, v)<br>            return<br>        default:<br>            fmt.Printf(&quot;monitor: %v in progress...\n&quot;, number)<br>            time.Sleep(2 * time.Second)<br>        }<br>    }<br>}</pre><pre>func main() {<br>    var ctx context.Context = nil<br>    var cancel context.CancelFunc = nil<br>    ctx, cancel = context.WithCancel(context.Background())</pre><pre>    for i := 1; i &lt;= 5; i = i + 1 {<br>        go monitor1(ctx, i)<br>    }</pre><pre>    time.Sleep(1 * time.Second)</pre><pre>    // close all gourtines<br>    cancel()</pre><pre>    // waiting 10 seconds, if the screen does not display &lt;monitor: xxxx in progress&gt;, all goroutines have been shut down<br>    time.Sleep(10 * time.Second)</pre><pre>    println(runtime.NumGoroutine())<br>    println(&quot;main program exit!!!!&quot;)<br>}</pre><h3>WithTimeout and WithDeadline</h3><p><strong>WithTimeout</strong> and <strong>WithDeadline</strong> are basically the same in terms of usage and function, they both indicate that the context will be automatically canceled after a certain time, the only difference can be seen from the definition of the function, the second parameter passed to <strong>WithDeadline</strong> is of type <strong>time.Duration</strong> type, which is a relative time, meaning how long after the timeout is cancelled.</p><p>Example:</p><pre>package main</pre><pre>import (<br>    &quot;runtime&quot;<br>    &quot;fmt&quot;<br>    &quot;time&quot;<br>    &quot;context&quot;<br>)</pre><pre>func monitor2(ctx context.Context, index int) {<br>    for {<br>        select {<br>        case v := &lt;- ctx.Done():<br>            fmt.Printf(&quot;monitor2: %v, the received channel value is: %v, ending\n&quot;, index, v)<br>            return<br>        default:<br>            fmt.Printf(&quot;monitor2: %v in progress...\n&quot;, index)<br>            time.Sleep(2 * time.Second)<br>        }<br>    }<br>}</pre><pre>func monitor1(ctx context.Context, index int) {<br>    for {<br>        go monitor2(ctx, index)<br>        select {<br>        case v := &lt;- ctx.Done():<br>            // this branch is only reached when the ch channel is closed, or when data is sent(either true or false)<br>            fmt.Printf(&quot;monitor1: %v, the received channel value is: %v, ending\n&quot;, index, v)<br>            return<br>        default:<br>            fmt.Printf(&quot;monitor1: %v in progress...\n&quot;, index)<br>            time.Sleep(2 * time.Second)<br>        }<br>    }<br>}</pre><pre>func main() {<br>    var ctx01 context.Context = nil<br>    var ctx02 context.Context = nil<br>    var cancel context.CancelFunc = nil<br>    ctx01, cancel = context.WithCancel(context.Background())<br>    ctx02, cancel = context.WithDeadline(ctx01, time.Now().Add(1 * time.Second)) // If it&#39;s WithTimeout, just change this line to &quot;ctx02, cancel = context.WithTimeout(ctx01, 1 * time.Second)&quot;</pre><pre>    defer cancel()</pre><pre>    for i := 1; i &lt;= 5; i = i + 1 {<br>        go monitor1(ctx02, i)<br>    }</pre><pre>    time.Sleep(5 * time.Second)</pre><pre>    if ctx02.Err() != nil {<br>        fmt.Println(&quot;the cause of cancel is: &quot;, ctx02.Err())<br>    }</pre><pre>    println(runtime.NumGoroutine())<br>    println(&quot;main program exit!!!!&quot;)<br>}</pre><h3>WithValue</h3><p>Some required metadata can also be passed through the Context, which is appended to the Context for use.<br>Metadata is passed in as Key-Value, but note that the Key must be comparable and the Value must be thread-safe.</p><pre>package main</pre><pre>import (<br>    &quot;runtime&quot;<br>    &quot;fmt&quot;<br>    &quot;time&quot;<br>    &quot;context&quot;<br>)</pre><pre>func monitor(ctx context.Context, index int) {<br>    for {<br>        select {<br>        case &lt;- ctx.Done():<br>            // this branch is only reached when the ch channel is closed, or when data is sent(either true or false)<br>            fmt.Printf(&quot;monitor %v, end of monitoring. \n&quot;, index)<br>            return<br>        default:<br>            var value interface{} = ctx.Value(&quot;Nets&quot;)<br>            fmt.Printf(&quot;monitor %v, is monitoring %v\n&quot;, index, value)<br>            time.Sleep(2 * time.Second)<br>        }<br>    }<br>}</pre><pre>func main() {<br>    var ctx01 context.Context = nil<br>    var ctx02 context.Context = nil<br>    var cancel context.CancelFunc = nil<br>    ctx01, cancel = context.WithCancel(context.Background())<br>    ctx02, cancel = context.WithTimeout(ctx01, 1 * time.Second)<br>    var ctx03 context.Context = context.WithValue(ctx02, &quot;Nets&quot;, &quot;Champion&quot;) // key: &quot;Nets&quot;, value: &quot;Champion&quot;<br>  <br>    defer cancel()</pre><pre>    for i := 1; i &lt;= 5; i = i + 1 {<br>        go monitor(ctx03, i)<br>    }</pre><pre>    time.Sleep(5 * time.Second)</pre><pre>    if ctx02.Err() != nil {<br>        fmt.Println(&quot;the cause of cancel is: &quot;, ctx02.Err())<br>    }</pre><pre>    println(runtime.NumGoroutine())<br>    println(&quot;main program exit!!!!&quot;)<br>}</pre><p>There are also some notes about context:</p><ul><li>Do not store Contexts in struct types, but pass the Context explicitly to each function that needs it, and the Context should be the first argument.</li><li>Do not pass a nil Context, even if the function allows it, or if you are not sure which Context to use, pass context.</li><li>Do not pass variables that could be passed as function arguments to the Value of the Context.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6a460f9e8d2f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Use of synchronization techniques in Golang]]></title>
            <link>https://lebum.medium.com/use-of-synchronization-techniques-in-golang-53d75bc0a646?source=rss-fba40115516a------2</link>
            <guid isPermaLink="false">https://medium.com/p/53d75bc0a646</guid>
            <category><![CDATA[concurrency]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[sync]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[channel]]></category>
            <dc:creator><![CDATA[It is Zion]]></dc:creator>
            <pubDate>Fri, 18 Dec 2020 07:35:54 GMT</pubDate>
            <atom:updated>2021-03-29T09:32:27.060Z</atom:updated>
            <content:encoded><![CDATA[<p>Golang synchronizes multiple goroutines via channels and sync packages to prevent multiple goroutines from competing for data.</p><h3><strong>Data competition problem</strong></h3><p>Due to the data competition problem, it is necessary to synchronize multiple goroutines concurrently, so we need to understand what data competition is before that.</p><p>To understand data competition, it is necessary to mention atomic operations.</p><p>When each task executed by the computer cannot be further subdivided (it cannot be split into smaller subtasks), it is an <strong>atomic task</strong>.</p><p>Atomic operations are operations that are directly implemented by a single CPU instruction (this instruction cannot be interrupted during execution).</p><p>Usually, <strong>num = num + 1</strong> looks like a single line of code, but it actually adds num to 1 and then writes num, which is not an atomic operation since there are two instructions after compilation.</p><p>Sometimes a single line of code is compiled with multiple CPU instructions, but there is only one write operation involving data, so it can also be considered an atomic operation (because data contention problems are usually generated by writing data).</p><p>Python, for example, officially supports atomic operations such as update for dictionaries:</p><pre>import dis</pre><pre>info = {}</pre><pre>def target():<br>    global info<br>    new = {&quot;Lebum&quot;: &quot;Champion&quot;}<br>    info.update(new) # focus on testing whether this sentence is an atomic operation</pre><pre>print(dis.dis(target), end = &quot;\n&quot;)</pre><p>After running this script, you can see the <strong>bytecode</strong> displayed in the console:</p><pre>...<br>LOAD_ATTR     1(update)<br>LOAD_FAST     0(new)<br>POP_TOP<br>LOAD_CONST    0(None)<br>...</pre><p>The sentence <strong>info.update(new) </strong>is also divided into several operations, but there is only one <strong>POP_TOP</strong> involved in writing, so the update of the dictionary can be considered as an atomic operation.</p><p>Once you understand atomic operations, data competition is easy to understand.</p><p>Data contention arises precisely because of non-atomic operations, some goroutines are fast and some are slow, so each execution of the same instruction may have a different result.</p><p>For example:</p><pre>import &quot;sync&quot;</pre><pre>func add(w *sync.WaitGroup, num *int) {<br>    defer w.Done()<br>    *num= *num + 1<br>}</pre><pre>func main() {<br>    var n int = 0<br>    var wg *sync.WaitGroup = new(sync.WaitGroup)</pre><pre>    wg.Add(1000)<br>    for i := 0; i &lt; 1000; i = i + 1 {<br>	go add(wg, &amp;n)<br>    } // spawn 1000 new goroutines<br>    wg.Wait()</pre><pre>    println(n)<br>}</pre><p>Although 1000 goroutines read and write to n concurrently would result in 1000 if <strong>*num = *num + 1 </strong>were an atomic operation, the fact is that when you run this code you will find that the result may not be the same each time you run it, and it may well be less than 1000. This is because <strong>*num = *num + 1</strong><em> </em>is not an atomic operation, and each goroutine may not execute at the same speed, and then there are preemption calls, so something needs to be done to control this situation.</p><p>Concurrency that does not generate data contention is called thread-safe concurrency. No one wants to run their code with an uncontrolled result every time, so let’s look at the concurrency synchronization techniques that Golang provides.</p><h3>sync/atomic</h3><p>The <strong>sync/atomic</strong> package provides support for atomic operations for synchronizing reads and writes of integers and pointers.</p><p>There are five types of operations: add, subtract, compare and swap, load, store, and swap.</p><p>The types supported by atomic operations include int32, int64, uint32, uint64, uintptr, unsafe.Pointer.</p><p>For example, for the above example, replace <em>*num = *num + 1</em> with the atomic operation provided by the <strong>sync/atomic</strong> package:</p><pre>import &quot;sync&quot;<br>import &quot;sync/atomic&quot;</pre><pre>func add(w *sync.WaitGroup, num *int32) {<br>    defer w.Done()<br>    atomic.AddInt32(num, 1)<br>}</pre><pre>func main() {<br>    var n int32 = 0<br>    var wg *sync.WaitGroup = new(sync.WaitGroup)</pre><pre>    wg.Add(1000)<br>    for i := 0; i &lt; 1000; i = i + 1 {<br>	go add(wg, &amp;n)<br>    } // create 1000 new goroutines<br>    wg.Wait()</pre><pre>    println(n)<br>}</pre><p>This guarantees that the result of n is 1000.(<strong>note: the instruction execution of atomic operations cannot be interrupted, so there is naturally no data contention</strong>)</p><p>Other methods can be found in the official Golang documentation, but I don’t want to explain too much here.</p><p>However, atomic operations are limited in the number of operations they can support, and most rely on sync packages and channels.</p><h3><strong>sync.WaitGroup</strong></h3><p>The <strong>sync.WaitGroup</strong> struct in the sync package is used to wait for a group of goroutines to finish executing, and control is blocked until the group of goroutines finishes executing.</p><p>Each <strong>sync.WaitGroup</strong> value maintains an internal count, which initially defaults to zero.</p><p>For an addressable <strong>sync.WaitGroup</strong> value <strong>wg</strong>:</p><ul><li><strong>wg.Add(delta)</strong> to change the value of the count maintained by <strong>wg</strong>, <strong>wg.Done()</strong> and <strong>wg.Add(-1)</strong> are exactly equivalent</li><li>If a <strong>wg.Add(delta)</strong> or <strong>wg.Done()</strong> call changes the count maintained by <strong>wg</strong> to a negative number, a panic will be generated</li><li>When <strong>wg.Wait()</strong> is called by a goroutine if the count maintained by <strong>wg</strong> is zero, the <strong>wg.Wait()</strong> operation is a null operation; otherwise (the count is a positive integer), the goroutine will go into a blocking state, and when some other goroutine later changes the count to zero (typically by calling <strong>wg.Done()</strong>), the concurrent process will re-enter the running state (i.e. <strong>wg.Wait()</strong> will return)</li></ul><p>For an example, see the <strong>sync/atomic</strong> example above. The main goroutine will go into a blocking state to wait for 1000 to complete, after which the main goroutine will unblock.</p><h3>sync.Once</h3><p>The <strong>sync.Once</strong> value is used to ensure that a piece of code is not executed by multiple goroutines.</p><p>Each <strong>*sync.Once</strong> value has a <strong>Do(f func())</strong> method.</p><p>For example:</p><pre>var once *sync.Once = new(sync.Once)</pre><pre>for i := 0; i &lt; 10; i = i + 1 {<br>    go func() {<br>        println(i)<br>        once.Do(doSomething())<br>    }()<br>}</pre><pre>// here are 10 goroutines, but doSomething() will only be executed by one goroutine, println(i) must be executed before once.Do(doSomething())</pre><h3>sync.Mutex</h3><p>A Mutex value is often referred to as a mutex lock, and a Mutex zero value is a mutex lock that has not yet been locked: <strong>var mutex *sync.Mutex = nil.</strong></p><p>For an addressable Mutex value <strong>m</strong>:</p><ul><li>If goroutine state is unlocked, call <strong>m.Lock()</strong> to change state to locked, call <strong>m.Unlock()</strong> will cause <strong>RuntimError</strong> exception.</li><li>If the goroutine state is locked, <strong>m.Lock()</strong> will be blocked until another goroutine calls <strong>m.Unlock()</strong> to release the lock, and <strong>m.Unlock()</strong> to change the state to unlocked.</li></ul><p>For example:</p><pre>import &quot;sync&quot;</pre><pre>var num int = 0</pre><pre>func add(lc *sync.Mutex, wg *sync.WaitGroup) {<br>    defer wg.Done()<br>    for i := 0; i &lt; 100000; i = i + 1 {<br>        lc.Lock()<br>        num = num + 1<br>        lc.Unlock()<br>    }<br>}</pre><pre>func minus(lc *sync.Mutex, wg *sync.WaitGroup) {<br>    defer wg.Done()<br>    for i := 0; i &lt; 100000; i = i + 1 {<br>        lc.Lock()<br>        num = num - 1<br>        lc.Unlock()<br>    }<br>}</pre><pre>func main() {<br>    var mutex *sync.Mutex = new(sync.Mutex)<br>    var wg *sync.WaitGroup = new(sync.WaitGroup)</pre><pre>    wg.Add(2)<br>    go add(mutex, wg)<br>    go minus(mutex, wg)<br>    wg.Wait()<br>    <br>    println(num) // 0<br>}</pre><p>For this code, after calling <strong>lc.Lock()</strong> in add, it is called the current goroutine wants to get the lock, if <strong>lc.Lock()</strong> has been called before in minus, then the goroutine calling add will be blocked until <strong>lc.Unlock()</strong> and vice versa.</p><h3>sync.RMutex</h3><p>For <strong>sync.RWMutex</strong>, when a goroutine is writing, the other goroutines can neither read nor write.</p><p><strong>*Mutex</strong> and <strong>*sync.RWMutex</strong> types both implement the sync:</p><pre>type Locker interface {<br>    Lock()<br>    Unlock()<br>}</pre><p><strong>sync.RWMutex</strong> type has two other methods: <strong>RLock()</strong> and <strong>RUnlock()</strong>.</p><p>So the total is:</p><ul><li><strong>func (rw *RWMutex) Lock()</strong>: Get a write lock</li><li><strong>func (rw *RWMutex) Unlock()</strong>: release write lock</li><li><strong>func (rw *RWMutex) RLock()</strong>: Get a read lock</li><li><strong>func (rw *RWMutex) RUnlock()</strong>: release read lock</li></ul><p><strong>sync.RWMutex</strong> is similar to <strong>sync.Mutex</strong>, so I won’t list it here. For <strong>sync.RWMutex</strong>, just remember:</p><blockquote>If a goroutine wants to get RLock and other goroutines have only RLock, it will not block the goroutine that wants to get RLock, if there is a goroutine with Lock, it will block the goroutine that wants to get RLock until all other goroutines with Lock have released their Lock.</blockquote><blockquote>If a goroutine wants to get Lock, other goroutines with RLock or Lock will block the current goroutine that wants to get Lock until all other goroutines have RLock and Lock released.</blockquote><h3>sync.Cond</h3><p>Unlike a mutex, a condition variable does not guarantee that only one goroutine has access to a shared data at the same time, but notifies other goroutines that are blocked when the state of the corresponding shared data changes.</p><p>Conditional variables are always used in combination with mutexes, which provide mutex support for accessing shared data, and conditional variables, which notify the relevant goroutine of a change in the state of the shared data.(That is, when defining variables with <strong>var cond *sync.Cond = sync.NewCond(new(sync.Mutex))</strong>)</p><p>After seizing a lock, it determines whether it satisfies the processing conditions, and if not, it releases the lock to other goroutines, then blocks itself, waits for other goroutines to notify it, then releases the block, and then goes back to acquire the lock.</p><p>For an addressable variable <strong>cond</strong> of type <strong>*sync.Cond</strong>, the following methods are commonly used:</p><ul><li><strong>cond.L.Lock()</strong> and <strong>cond.L.Unlock()</strong>: l<strong>ock()</strong> and <strong>lock.Unlock()</strong> can also be used, exactly the same.</li><li><strong>cond.Wait()</strong>: When this method is called, the outflow of the executed operation is: <strong>Unlock()</strong> -&gt; blocking waiting notification (i.e. waiting for notification from <strong>Signal()</strong> or <strong>Broadcast()</strong>) -&gt; receiving notification -&gt; <strong>Lock()</strong>.</li><li><strong>cond.Signal()</strong>: Notify a Wait goroutine, if there is no <strong>Wait()</strong>, no error will be reported, <strong>Signal()</strong> notification order is based on the original join notification list (<strong>Wait()</strong>) first in first out.</li><li><strong>cond.Broadcast()</strong>: Notify all Wait goroutines, if there is no <strong>Wait()</strong>, no error will be reported.</li></ul><p>For example:</p><pre>func main() {<br>    var cond *sync.Cond = sync.NewCond(new(sync.Mutex))<br>    var condition int = 0<br>  <br>    // Consumer<br>    go func() {<br>        for {<br>            cond.L.Lock()<br>            for condition == 0 {<br>                cond.Wait()<br>            }<br>            condition = condition - 1<br>            println(condition)<br>            cond.Signal()<br>            cond.L.Unlock()<br>        }<br>    }()</pre><pre>    // Producer<br>    for {<br>        time.Sleep(time.Second)<br>        cond.L.Lock()<br>        for condition == 3 {<br>            cond.Wait()<br>        }<br>        condition = condition + 1<br>        println(condition)<br>        cond.Signal()<br>        cond.L.Unlock()<br>    }<br>}</pre><h3>sync.Map</h3><p>IO for maps in Golang is not concurrently safe, reading and writing maps between multiple goroutines will result in a <strong>fatal error: concurrent map writes</strong>.</p><p>If you want to read and write maps concurrently, you can use <strong>sync.Map</strong>.</p><p>For example:</p><pre>import &quot;sync&quot;</pre><pre>func run(wg *sync.WaitGroup, sc *sync.Map) {<br>    defer wg.Done()<br>    var sceneIterate func(interface{}, interface{}) bool = func(k interface{}, v interface{}) bool {<br>        println(&quot;iterate:&quot;, k, v)<br>        return true<br>    }<br>    sc.Range(sceneIterate)<br>}</pre><pre>func main() {<br>    var scene *sync.Map = new(sync.Map)<br>    <br>    scene.Store(&quot;greece&quot;, 97)<br>    scene.Store(&quot;london&quot;, 100)<br>    scene.Store(&quot;egypt&quot;, 200)</pre><pre>    var v interface{} = nil<br>    var ok bool = false<br>    v, ok = scene.Load(&quot;london&quot;)<br>    println(v, ok)</pre><pre>    scene.Delete(&quot;london&quot;)<br>    <br>    var wg *sync.WaitGroup = new(sync.WaitGroup)<br>   <br>    wg.Add(1)<br>    go run(wg, scene)<br>    wg.Wait()<br>}</pre><p>Of course, other concurrency techniques can be used to concurrently operate map.</p><h3>sync.Pool</h3><p><strong>sync.Pool</strong> can be used to cache objects, since frequent use of heap memory can cause too much work for <strong>GC</strong>.</p><p><strong>sync.Pool</strong> can be reclaimed without notice to relieve <strong>GC</strong> pressure.</p><p>To initialize the pool, the only thing you need is to set up the <strong>New</strong> function so that when the <strong>Get</strong> method is called, if the pool has a cached object, it returns the cached object directly, and if it does not, the <strong>New</strong> function is called to create a new object.</p><p>For example:</p><pre>import &quot;sync&quot;</pre><pre>var intPool *sync.Pool = &amp;sync.Pool {<br>    New: func() interface{} {<br>        var b []int = make([]int, 8)<br>        return &amp;b<br>    },<br>}</pre><pre>func main() {</pre><pre>    // get the obj and do not put it back<br>    for i := 1; i &lt; 4; i = i + 1 {<br>        var obj *[]int = intPool.Get().(*[]int)<br>        (*obj)[i] = i<br>        println(obj)<br>    }</pre><pre>    println(&quot;=======&quot;)</pre><pre>    // get the obj and put it back<br>    for i := 1; i &lt; 4; i = i + 1 {<br>        var obj *[]int = intPool.Get().(*[]int)<br>        (*obj)[i] = i<br>        println(obj)<br>        intPool.Put(obj)<br>    }<br>}</pre><pre>console:<br>0xc00000c060<br>0xc00000c080<br>0xc00000c0a0<br>=======<br>0xc00000c0c0<br>0xc00000c0c0<br>0xc00000c0c0</pre><p>As you can see from the console, the structs fetched and put back are the same.</p><p>Pools are suitable for scenarios where there is a lot of memory and a lot of concurrency, but when there is little memory and little concurrency, using pools is counterproductive.</p><p>The best practice for using <strong>sync.Pool</strong> is: Empty Pool before <strong>Put</strong>, Empty Pool after <strong>Get</strong>.</p><h3>Channel</h3><p>Finally, the <strong>Channel</strong> is used for much more than just synchronizing multiple goroutines, they are also used to pass data between different goroutines, or to control the number of goroutines, etc. The previous synchronization techniques can only be used to synchronize, which is where <strong>Channel</strong> differs from them.</p><p>In the absence of extreme cases, the use of <strong>Channel</strong> is preferred in synchronization techniques.</p><p>For the <strong>sync.Cond</strong> example above, it can be implemented as a <strong>Channel</strong> instead:</p><pre>func main() {<br>    var ch chan int = make(chan int, 3)<br>    var v int = 0</pre><pre>    // Consumer<br>    go func() {<br>        for {<br>            println(&lt;- ch)<br>        }<br>    }()</pre><pre>    // Producer<br>    for {<br>        v = v + 1<br>        println(v)<br>        ch &lt;- v<br>        time.Sleep(time.Second)<br>    }<br>}</pre><p>There is a lot of information about the <strong>Channel</strong> mechanism on the web, so I don’t want to describe it here.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=53d75bc0a646" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Explaining the IO models in detail]]></title>
            <link>https://lebum.medium.com/explaining-the-io-models-in-detail-fdb7e9a0dab5?source=rss-fba40115516a------2</link>
            <guid isPermaLink="false">https://medium.com/p/fdb7e9a0dab5</guid>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[application]]></category>
            <category><![CDATA[io]]></category>
            <dc:creator><![CDATA[It is Zion]]></dc:creator>
            <pubDate>Sat, 12 Dec 2020 05:36:37 GMT</pubDate>
            <atom:updated>2021-04-07T06:46:01.243Z</atom:updated>
            <content:encoded><![CDATA[<h3>I/O from an application perspective</h3><p>The essence of IO operations is the mutual copying of user space buffers and kernel space buffers.</p><p>It generally consists of two steps:</p><ol><li>wait for network data to reach NIC (read-ready) / wait for NIC to be writable (write-ready) -&gt; read / write to kernel buffer</li><li>copy data from kernel buffer -&gt; user space (read) / copy data from user space -&gt; kernel buffer (write)</li></ol><p>In Linux, the objects of IO are files (because everything is a file).<br>IO for normal files involves DMA, disk and CPU. For socket files sendfile technology is also included.</p><h4>Synchronous IO and Asynchronous IO</h4><p>From the point of view of program execution, IO is divided into synchronous IO and asynchronous IO.</p><p>To determine whether an I/O model is synchronous or asynchronous, it depends on the second step: whether data copying between user and kernel space blocks the current process, if so, it is synchronous I/O, otherwise, it is asynchronous I/O.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*cmTCOxq2HXwJNBgL.png" /><figcaption>this pic from Unix network programming Chapter</figcaption></figure><p>Based on this principle, there is only one asynchronous I/O model among these five I/O models: asynchronous I/O. The rest are all synchronous I/O models.</p><p>Here is a brief description of these 5 IO models.</p><h4>Blocking IO</h4><p>When an application calls recvfrom to read data, its system call returns when it knows that the packet has been copied to the application buffer or sent in error, and waits during this time. <br>The process is blocked from the time of the call to the time of the return is called blocking IO.</p><h4>nonBlocking IO</h4><p>non-blocking IO returns an EWOULDBLOCK error if there is no data in the buffer when the application calls recvfrom to read the data, and does not keep the application waiting. The error flag is returned immediately when there is no data, which means that if the application wants to read data it needs to keep calling recvfrom requests (I think it’s an infinite loop internally) until it reads the data it wants.</p><h4>IO multiplexing</h4><p>First think about a problem, the application reads data from the TCP buffer. If in a concurrent environment, there may be N people sending messages to application. In this case our application will have to create multiple threads to read the data, and each thread will call recvfrom itself to read the data.</p><p>In this case, application needs to create millions of threads to read the data, and because the application threads do not know when the data will be read, they must keep sending recvfrom requests to the kernel to read the data in order to ensure that the messages are read in time.</p><p>So the question arises, so many threads keep calling recvfrom to request data, not to mention that the server can not carry so many threads, even if it can carry then it is obvious that this way is not too wasteful of resources, threads are valuable resources of our operating system, a large number of threads used to read data, then it means that the threads can do other things will be less.</p><p>So, is there a way to monitor multiple file descriptors by a single thread (the Linux system takes all network requests to a socket file descriptor), so that only one or a few threads can complete the operation of data status interrogation, and then allocate the corresponding thread to read data when there is data ready, so that you can save a lot of thread resources out, this is the idea of the IO reuse model.</p><p>The idea of the IO reuse model is that the system provides a function that can monitor multiple file descriptor operations at the same time, this function is often referred to as select, poll, epoll function, with this function, the application thread can monitor multiple file descriptors at the same time by calling this function, this function monitors the file descriptors as long as any one of the data state is ready, this function will return readable state, then the interrogation thread to notify the data processing thread, the corresponding thread then launched a recvfrom request to read the data.</p><p>So the basic pattern is that one thread is used to call select, epoll, etc., and another thread is used to call recvfrom to process the data.</p><p>The process blocks on the select operation by passing one or more file descriptors to select. select helps us to detect if multiple file descriptors are ready, when there are file descriptors ready, select returns the data readable status and the application then calls recvfrom to read the data.</p><h4>Signal-drive IO</h4><p>The multiplexed IO model solves the problem that a thread can monitor multiple file descriptors, but select uses polling to monitor multiple file descriptors, by constantly polling the file descriptors readable state to know if the data is readable, and the mindless polling is a bit violent, because in most cases the polling is invalid, so can I not always ask you if the data is ready, can the application send a request to notify the application when the data is ready, so the signal-driven IO model is derived.</p><p>When the sigaction is called, a SIGIO signal contact is established, and when the kernel data is ready, the thread is notified of the readable status of the data through the SIGIO signal, and when the thread receives the signal of the readable status, it then initiates a recvfrom request to the kernel to read the data, because under the signal-driven IO model, the application thread can signal the monitor and then So in this way, an application thread can also monitor multiple file descriptors at the same time.</p><h4>Asynchronous IO</h4><p>Can there be a one-and-done way where I just send a request and I tell the kernel I want to read the data and then I don’t care about anything and then the kernel does all the rest for me?</p><p>Some people have designed a scheme in which the application simply sends a read request to the kernel, tells the kernel that it wants to read the data and returns immediately; the kernel receives the request and establishes a signal contact, and when the data is ready, the kernel will actively copy the data from the kernel to the user space. We call this one-and-done model the asynchronous IO model.</p><p>The main difference between this model and the signal-driven model is that the signal-driven IO is only informed by the kernel that it is appropriate to start the next IO operation, while the asynchronous IO model is informed by the kernel when the operation is completed and that the asynchronous IO does not block the thread during the second step of the IO.</p><h4>Aobut asynchronous blocking</h4><p>After understanding all the IO models, let’s look at synchronous blocking and synchronous non-blocking, which differ only in the first step, but are the same in that they both block in the second step and require the application to monitor the entire data completion process itself. And why asynchronous non-blocking and not asynchronous blocking afterwards? Because in the asynchronous model, the request is sent and returned immediately without any subsequent process, so it is not destined to block, so there is only the asynchronous non-blocking model.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fdb7e9a0dab5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[About Git and GitHub permission(SSH and HTTPS, single account and multiple accounts)]]></title>
            <link>https://lebum.medium.com/about-git-and-github-permission-ssh-and-https-single-account-and-multiple-accounts-13747ca8764e?source=rss-fba40115516a------2</link>
            <guid isPermaLink="false">https://medium.com/p/13747ca8764e</guid>
            <category><![CDATA[github]]></category>
            <category><![CDATA[git]]></category>
            <category><![CDATA[ssh]]></category>
            <category><![CDATA[ssh-keys]]></category>
            <category><![CDATA[https]]></category>
            <dc:creator><![CDATA[It is Zion]]></dc:creator>
            <pubDate>Sat, 27 Jun 2020 06:46:00 GMT</pubDate>
            <atom:updated>2021-03-29T09:40:06.073Z</atom:updated>
            <content:encoded><![CDATA[<p>When the local computer communicates with GitHub, the transmission is mainly based on two protocols, HTTPS and SSH, and the corresponding repository addresses are HTTPS URLs and SSH URLs.</p><p>The first thing to emphasize is that HTTPS URLs and SSH URLs correspond to two completely independent permission verification methods. The main difference is that HTTPS URLs use account passwords for verification, and SSH URLs use SSH key pairs for verification. In normal use, we can choose one according to the actual situation.</p><h4>HTTPS URLs</h4><p>GitHub officially recommends the use of HTTPS URLs, because this method is more applicable (even in the case of a firewall or proxy), and is more convenient to use (easier configuration).</p><p>When using the HTTPS URLs address clone <strong>/fetch/pull/push</strong> repository, there is no need to configure the local system in advance, just enter the GitHub account and password. However, if you have to manually enter the account password every time, it is also a very tedious thing.</p><p>Fortunately, there are already multiple mechanisms to make operations less troublesome.</p><p>In the macOS, when the Keychain mechanism is enabled, after entering the GitHub account password for the first time, the authentication information will be automatically saved in the Keychain.app of the system, and the authentication information saved in the Keychain.app will be automatically read the next time the repository is accessed again.</p><p>On other systems, although there is no <strong>Keychain mechanism</strong>, Git provides a credential helper mechanism, which can cache the account password in memory for a period of time (default 15 minutes), or store it as a file (such as <strong>~/. git-credentials</strong>). Of course, if the macOS does not enable the Keychain mechanism, this method can also be used.</p><pre># cache credential in memory<br>$ git config --global credential.helper cache</pre><pre># store credential in ~/.git-credential<br>$ git config --global credential.helper store</pre><p>When <strong>credential.helper</strong> is set to store, after entering the GitHub account password for the first time, it will be automatically saved to the such as <strong>~/.git-credentials</strong> file in the form of <strong>https://user:pass@github.com </strong>; next time When accessing the repository, the authentication information saved in <strong>~/.git-credentials</strong> will be automatically read.</p><p>Or you can use the <strong>token</strong> mechanism. In the Developer Settings of the Github user’s Settings, you can create and manage tokens in the Personal access <strong>tokens</strong> column, as long as the <strong>token id</strong> is saved locally. Then in a repository, in the config file, just change the format of the remote url part to <strong>https://your_token_id@github.com/your_repo_name.git </strong>(<strong>note</strong>:<strong> </strong>later, when more remote repo interactions such as <strong>git clone</strong>, the URL must bring the <strong>token id) </strong>.</p><p>Another situation that needs to be explained is that if 2FA (two-factor authentication) is enabled in GitHub, when entering the GitHub account password in the local system, you cannot enter the original password (that is, the login password of the GitHub website), but need to Create a Personal<strong> </strong>access<strong> token</strong> in the GitHub website in advance, and then use the access <strong>token</strong> as a password to enter when accessing the code repository needs to be verified.</p><h4>SSH URLs</h4><p>In addition to HTTPS URLs, you can also use SSH URLs to access the GitHub code repository.</p><p>Before using SSH URLs, you need to generate an SSH<strong> </strong>key pair<strong> </strong>(secret key pair, including private and public keys) on the local computer. By default, the generated secret key is located in the such as <strong>$HOME/.ssh/</strong> directory, and the file are <strong>id_ed25519</strong> and <strong>id_ed25519.pub </strong>(if you choose ed25519 algorithm), respectively. Usually, you do not need to modify them and keep the default. However, if there are multiple key pairs in a computer, you need to modify the key file name. There is no mandatory naming convention for the name, so you can easily identify it.</p><pre>$ ssh-keygen -t ed25519<br>Generating public/private ed25519 key pair.<br>Enter file in which to save the key (/Users/xxx/.ssh/hehe_ed25519): /Users/xxx/.ssh/<strong>hehe_ed25519</strong> <strong># your files name # if enter directly, name becomes id_ed25519</strong><br>Enter passphrase (empty for no passphrase): <strong>&lt;myPassphrase&gt; # if enter directly, passphrase becomes empty</strong><br>Enter same passphrase again: <strong>&lt;myPassphrase&gt;</strong><br>Your identification has been saved in /Users/xxx/.ssh/<strong>hehe_ed25519</strong>.<br>Your public key has been saved in /Users/xxx/.ssh/<strong>hehe_ed25519</strong>.<strong>pub</strong>.<br>The key fingerprint is:<br>SHA256:jCyEEKjlCU1klROnuBg+UH08GJ1u252rQMADdD9kYMo<br>mail@outlook.com <strong># email is no use, you can remove this</strong><br>The key&#39;s randomart image is:<br>+--[ED25519 256]--+<br>|  ...            |<br>|.... .       o   |<br>|o..   . . o = o  |<br>|o..    . o.O +   |<br>|.o      SoOo+    |<br>|. .     o=.ooo   |<br>| o o   + .o..+.  |<br>|. . . o.o +o+ .  |<br>|     .E.+*oo++   |<br>+----[SHA256]-----+</pre><p>In the process of creating the key, the system also recommends creating a thing called passphrase, what is it for?</p><blockquote>First of all, using passwords alone is certainly not secure enough. If the password is too simple, it is easy to be brute-forced. If the password is too complicated, it is difficult for the user to remember, and it is even more insecure to record it in a small book.</blockquote><blockquote>Therefore, SSH keys were born. The reliability of the SSH key pair is very high, and there is basically no possibility of brute force cracking. However, this requires the user to keep the private key very carefully. If someone secretly copies your private key when using your computer, then it is like someone else has got your home key and can open your home door at any time.</blockquote><blockquote>Based on the above situation, the solution is to add a password in addition to SSH keys, that is, passphrase. Only when both SSH private key and passphrase are available, can the SSH permission check be passed, which greatly increases the security. Of course, this passphrase is not necessary, and you can not set the passphrase when creating the key pair.</blockquote><blockquote>In addition, if you need to enter a passphrase every time the authority is verified, this is quite troublesome. Fortunately, we don’t have to worry about this problem anymore, because <strong>ssh-agent</strong> can help us remember passphrase, <strong>macOS Keychain</strong> can also remember passphrase, so we don’t need to re-enter the password on the same computer.</blockquote><p>After the key pair is created, the private key is stored on the local computer (<strong>~/.ssh/hehe_ed25519</strong>), and the content of the public key (<strong>~/.ssh/hehe_ed25519.pub</strong>) is added to the GitHub account.</p><p>However, if the connection status between the local computer and GitHub is detected at this time, the system will still prompt that the permission verification fails.</p><pre>$ ssh -T git@github.com<br>Permission denied (publickey).</pre><p>This is because when the local computer establishes a connection with GitHub, the <strong>ssh-agent</strong> of the local computer actually communicates with the GitHub server. Although the local computer has a private key, <strong>ssh-agent</strong> does not know where the private key is stored. Therefore, to use the key pair normally, you need to add the private key to the local computer’s <strong>ssh-agent</strong> (passphrase is required during the addition process).</p><pre># start ssh-agent in the background<br>$ eval &quot;$(ssh-agent -s)&quot;<br>Agent pid 78370<br><br>$ ssh-add ~/.ssh/hehe_ed25519<br>Enter passphrase for /Users/xxx/.ssh/hehe_ed25519: &lt;myPassphrase&gt;<br>Identity added: /Users/xxx/.ssh/hehe_ed25519 (/Users/xxx/.ssh/hehe_ed25519)</pre><p>After the addition is complete, you can view the key stored in the current computer.</p><pre>$ ssh-add -l                <br>4096 SHA256:xRg49AgTxxxxxxxx8q2SPPOfxxxxxxxxRlBY /Users/xxx/.ssh/hehe_ed2519 (ED25519)</pre><p>Check the connection status of the local computer and GitHub again, and the verification passes normally.</p><pre>$ ssh -T git@github.com<br>Hi xxx! You&#39;ve successfully authenticated, but GitHub does not provide shell access.</pre><p>When you perform the clone <strong>/fetch/pull/</strong>push operation later, you can access the GitHub code repository normally, and you do not need to re-enter the account password.</p><p>Moreover, after adding the private key to the <strong>ssh-agent</strong>, even if the private key file is deleted, the local computer can still normally access the GitHub code repository.</p><pre>$ rm -rf ~/.ssh</pre><pre>$ ssh-add -l    <br>4096 SHA256:xRg49AgTxxxxxxxx8q2SPPOfxxxxxxxxRlBY /Users/xxx/.ssh/id_rsa (ED25519)</pre><pre>$ ssh -T git@github.com<br>The authenticity of host &#39;github.com (192.30.252.130)&#39; can&#39;t be established.<br>RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.<br>Are you sure you want to continue connecting (yes/no)? yes<br>Warning: Permanently added &#39;github.com,192.30.252.130&#39; (ED25519) to the list of known hosts.<br>Hi xxx! You&#39;ve successfully authenticated, but GitHub does not provide shell access.</pre><p>Only after executing the <strong>ssh-add -D</strong> or <strong>ssh-add -d pub_key</strong> command to delete the private key from the <strong>ssh-agent</strong> will the authentication information become invalid.</p><pre>$ ssh-add -d ~/.ssh/hehe_ed25519.pub<br>Identity removed: /Users/xxx/.ssh/hehe_ed25519.pub (mail@outlook.com)</pre><pre>$ ssh-add -l<br>The agent has no identities.</pre><pre>$ ssh -T git@github.com<br>Permission denied (publickey).</pre><h4><strong>Use multiple GitHub accounts simultaneously on one machine</strong></h4><p>After familiar with the two verification methods of <strong>HTTPS URLs</strong> and <strong>SSH URLs</strong>, let’s look at the problems encountered before. What do I need to do to use multiple GitHub accounts to access different repositories on the same computer?</p><p>For better demonstration, it is assumed that there are two GitHub accounts, one for work and one for own use, called <strong>worker</strong> and <strong>myself</strong>, and there is a repository in each of the two accounts, <strong>worker/ss</strong> and <strong>myself/tt</strong>.</p><p>As mentioned earlier, HTTPS URLs and SSH URLs correspond to two independent sets of permission verification methods, so both sets of methods should be able to achieve our needs independently.</p><p>But before explaining the problem of Git permission verification in detail, let’s review the priority of Git configuration file first.</p><ul><li><strong>/etc/gitconfig</strong>: stores the git configuration information of all users of the current system. When using git config with the — system option, the configuration information will be written to this file;</li><li><strong>~/.gitconfig</strong> or <strong>~/.config/git/config</strong>: store the git configuration information of the current user. When using git config with the — global option, the configuration information will be written to this file;</li><li>Keychain Access: When the Keychain mechanism is enabled, the account password will be automatically saved to Keychain Access after permission verification;</li><li>The config file in the Git directory of the repository (that is, <strong>repo/.git/config</strong>): stores the git configuration information of the current repository. When using git config with the — local option in the repository, the configuration information will be written to this file.</li></ul><p>In terms of priority, the priority of the above 4 configuration items rises from top to bottom, that is, <strong>repo/.git/config</strong> has the highest priority, then Keychain Access will overwrite the configuration in <strong>~/.gitconfig</strong>, and <strong>~/.gitconfig</strong> will Overwrite the configuration in<strong> /etc/gitconfig</strong>.</p><h4>Multi-account coexistence based on SSH protocol</h4><p>First look at how to use SSH URLs to achieve our needs.</p><pre># ssh-add -l<br>4096 SHA256:lqujbjkWM1xxxxxxxxxxG6ERK6DNYj9tXExxxxxx8ew /Users/xxx/.ssh/worker_ed25519 (ED25519)<br>4096 SHA256:II2O9vZutdQr8xxxxxxxxxxD7EYvxxxxxxbynx2hHtg /Users/xxx/.ssh/myslef_ed25519 (ED25519)</pre><p>Before explaining the problem of coexistence of multiple accounts in detail, let’s think back and forth about the scenario of interacting with the GitHub repository in terminal.</p><pre>$ git add .<br>$ git commit -m &quot;add README&quot;<br>$ git push forked HEAD<br>Counting objects: 3, done.<br>Delta compression using up to 4 threads.<br>Compressing objects: 100% (2/2), done.<br>Writing objects: 100% (3/3), 310 bytes | 0 bytes/s, done.<br>Total 3 (delta 0), reused 0 (delta 0)<br>To git@github.com: worker/ss.git<br>   7df6839..68d085b  nightly -&gt; nightly</pre><p>During the operation, the local computer’s <strong>ssh-agent</strong> established a connection with the GitHub server and checked account permissions.</p><p>When the local computer has only one GitHub account, this behavior is not difficult to understand, and the system should use this unique GitHub account for operation. So if there are multiple Github accounts on the local computer, what is the system to determine which account should be selected?</p><p>The actual situation is that the system cannot judge. The system will only have a default account, and then use this default account to operate all code repository. When the account and the repository do not match, it will report an error of permission verification failure.</p><p>How can the system distinguish the accounts correctly? This requires us to manually configure, the configuration file is <strong>~/.ssh/config</strong>.</p><p>Create <strong>~/.ssh/config</strong> file, fill in the following content.</p><pre>Host Worker<br>  AddKeysToAgent Yes<br>  HostName github.com<br>  PreferredAuthentications publickey<br>  IdentityFile ~/.ssh/Worker_ed25519</pre><pre>Host Myself<br>  AddKeysToAgent Yes<br>  HostName github.com<br>  PreferredAuthentications publickey<br>  IdentityFile ~/.ssh/Myself_ed25519</pre><p>To understand the meaning of the above configuration file is not difficult, we can compare the SSH URLs of the two projects:</p><pre>git@github.com:worker/ss.git<br>git@github.com:myself/tt.git</pre><p>Among them, git is the username (ie User) used by the local <strong>ssh-agen</strong>t to establish an SSH connection with the GitHub server, and http://github.com is the host (ie. HostName) of the GitHub server.</p><p>It can be seen that if the original SSH URLs are used, because the User and HostName are the same, the local computer does not know which SSH-key should be used to establish the connection.</p><p>Therefore, by creating <strong>~/.ssh/config</strong> file to distinguish in Host, and then mapping to HostName through NAME, and then pointing to different SSH-key, namely IdentityFile. Because HostName is the field that really specifies the host of the GitHub server, this configuration will not affect the local <strong>ssh-agent</strong> connection to the GitHub host, plus the Host alias points to a different SSH-key, thus achieving the two GitHub accounts Separate.</p><p>After the configuration is complete, the two GitHub accounts can be distinguished by the Host alias. In subsequent communication with the GitHub server, the host alias can be used instead of the original http://github.com. For example, to test the connectivity between the local <strong>ssh-agent</strong> and the GitHub server, you can use the following methods:</p><pre>$ sh -T git@Worker<br>Hi Worker! You have successfully authenticated, but GitHub does not provide shell access.</pre><pre>$ ssh -T git@Myself<br>Hi Myself! You have successfully authenticated, but GitHub does not provide shell access.</pre><p>It can be seen that at this time, the two accounts are responsible for their duties, and there will be no confusion.</p><p>However, we have missed a very important point. When performing <strong>push/pull/fetch</strong> and other operations in the local code repository, the command does not contain Host information. How does the system know which GitHub account we want to use for operation?</p><p>The answer is that the system is still unable to judge, and we need to specify the configuration.</p><p>Obviously, different repositories may correspond to different GitHub accounts, so this configuration cannot be configured globally, but can only be configured separately in each project, that is, the <strong>repo/.git/config</strong> file.</p><p>The configuration method is as follows:</p><p>In the worker/ss repository:</p><pre>$ git remote add forked git@worker:worker/ss.git</pre><p>In the myself/tt repository:</p><pre>$ git remote add forked git@myself:myself/tt.git</pre><p>The principle of configuration is also easy to understand, which is to replace the host of the repository with the alias set previously. After the addition is complete, when performing any git operations in the two repositories, the system can select the correct SSH-key to interact with the GitHub server.</p><h4>Multi-account coexistence based on HTTPS protocol</h4><p>With the previous experience, our thinking is much clearer. After verification of Git permissions using HTTPS URLs, the system will store the GitHub account password in the Keychain, or in the <strong>~/.git-credentials</strong> file (Git credential helper).</p><p>No matter where it is stored, the problem we face is the same, that is, how to distinguish which GitHub account is used in the code repository.</p><p>The configuration method is actually very simple:</p><p>In the worker/ss repository:</p><pre>$ git remote add origin https://worker@github.com/worker/ss.git</pre><p>The principle of configuration is also easy to understand. Add the GitHub user name to the Git address of the repository, so that when the git command is executed, the system will use the specified GitHub user name to find the corresponding keychain or <strong>~/.git-credentials</strong> With authentication information, the problem of messy account usage is gone.</p><p>For <strong>tokens</strong>, just add the <strong>token</strong> id to the URL, and the system can naturally distinguish users based on the id.</p><p>Finally, it is very important that git account corresponds to GitHub account. If you change your global git name and email to a non-existent name and email locally, after <strong>git push</strong> you will find that the commit author displayed on GitHub is a ghost account.</p><p>Therefore, the name and email of git should be the same as the GitHub account. For different GitHub accounts, it is best to configure the name and email of git in each <strong>repo/.git/config</strong>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=13747ca8764e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>