Skip to content

Instantly share code, notes, and snippets.

@doron2402
Last active April 7, 2020 05:41

Revisions

  1. doron2402 revised this gist Apr 7, 2020. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion main.go
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,8 @@
    package main

    /**
    A cool way to find out if there are any race condition is using
    `go run --race .`
    **/
    import (
    "fmt"
    "math/rand"
  2. doron2402 revised this gist Apr 7, 2020. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion main.go
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,9 @@ import (
    "time"
    )

    // cache - key value (key = book id, value = book object)
    var cache = map[int]Book{}
    // get random instance
    var rnd = rand.New(rand.NewSource(time.Now().UnixNano()))

    func main() {
    @@ -17,26 +19,30 @@ func main() {

    for i := 0; i < 10; i++ {
    id := rnd.Intn(10) + 1
    // set 2 waiting group (1. cache, 2. db)
    wg.Add(2)
    go func(wg *sync.WaitGroup, m *sync.RWMutex) {
    if b, ok := queryCache(id, m); ok {
    fmt.Println("from cache")
    fmt.Println(b)
    }
    // if the book found in cache finish the waiting group
    wg.Done()
    }(wg, m)

    go func(wg *sync.WaitGroup, m *sync.RWMutex) {
    // get book from db
    if b, ok := queryDatabase(id, m); ok {
    fmt.Println("from database")
    fmt.Println(b)
    }
    // finish waiting group
    wg.Done()
    }(wg, m)

    // time.Sleep(150 * time.Millisecond)
    }

    // wait for all concurrent functions
    wg.Wait()
    }

  3. doron2402 created this gist Apr 7, 2020.
    150 changes: 150 additions & 0 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,150 @@
    package main

    import (
    "fmt"
    "math/rand"
    "sync"
    "time"
    )

    var cache = map[int]Book{}
    var rnd = rand.New(rand.NewSource(time.Now().UnixNano()))

    func main() {

    wg := &sync.WaitGroup{}
    m := &sync.RWMutex{}

    for i := 0; i < 10; i++ {
    id := rnd.Intn(10) + 1
    wg.Add(2)
    go func(wg *sync.WaitGroup, m *sync.RWMutex) {
    if b, ok := queryCache(id, m); ok {
    fmt.Println("from cache")
    fmt.Println(b)
    }
    wg.Done()
    }(wg, m)

    go func(wg *sync.WaitGroup, m *sync.RWMutex) {
    if b, ok := queryDatabase(id, m); ok {
    fmt.Println("from database")
    fmt.Println(b)
    }
    wg.Done()
    }(wg, m)

    // time.Sleep(150 * time.Millisecond)
    }

    wg.Wait()
    }

    func insertBook(books Book[], b Book) Book[] {
    books = append(books, book)
    return books
    }

    func queryCache(id int, m *sync.RWMutex) (Book, bool) {
    m.RLock()
    b, ok := cache[id]
    m.RUnlock()
    return b, ok
    }

    func queryDatabase(id int, m *sync.RWMutex) (Book, bool) {
    time.Sleep(100 * time.Millisecond)
    for _, b := range books {
    if b.ID == id {
    m.Lock()
    cache[id] = b
    m.Unlock()
    return b, true
    }
    }

    return Book{}, false
    }

    // books.go
    package main

    import "fmt"

    type Book struct {
    ID int
    Title string
    Author string
    YearPublished int
    }

    func (b Book) String() string {
    return fmt.Sprintf(
    "Title:\t\t%q\n"+
    "Author:\t\t%q\n"+
    "Published:\t%v\n", b.Title, b.Author, b.YearPublished)
    }

    var books = []Book{
    Book{
    ID: 1,
    Title: "The Hitchhiker's Guide to the Galaxy",
    Author: "Douglas Adams",
    YearPublished: 1979,
    },
    Book{
    ID: 2,
    Title: "The Hobbit",
    Author: "J.R.R. Tolkien",
    YearPublished: 1937,
    },
    Book{
    ID: 3,
    Title: "A Tale of Two Cities",
    Author: "Charles Dickens",
    YearPublished: 1859,
    },
    Book{
    ID: 4,
    Title: "Les Misérables",
    Author: "Victor Hugo",
    YearPublished: 1862,
    },
    Book{
    ID: 5,
    Title: "Harry Potter and the Philosopher's Stone",
    Author: "J.K. Rowling",
    YearPublished: 1997,
    },
    Book{
    ID: 6,
    Title: "I, Robot",
    Author: "Isaac Asimov",
    YearPublished: 1950,
    },
    Book{
    ID: 7,
    Title: "The Gods Themselves",
    Author: "Isaac Asimov",
    YearPublished: 1973,
    },
    Book{
    ID: 8,
    Title: "The Moon is a Harsh Mistress",
    Author: "Robert A. Heinlein",
    YearPublished: 1966,
    },
    Book{
    ID: 9,
    Title: "On Basilisk Station",
    Author: "David Weber",
    YearPublished: 1993,
    },
    Book{
    ID: 10,
    Title: "The Android's Dream",
    Author: "John Scalzi",
    YearPublished: 2006,
    },
    }