GithubHelp home page GithubHelp logo

loong / go-concurrency-exercises Goto Github PK

View Code? Open in Web Editor NEW
926.0 13.0 330.0 1.4 MB

Hands on exercises with real-life examples to study and practice Go concurrency patterns. Test-cases are provided to verify your answers.

License: Other

Go 100.00%

go-concurrency-exercises's Introduction

Go Concurrency Exercises Build Status Go Report Card

Exercises for Golang's concurrency patterns.

Why

The Go community has plenty resources to read about go's concurrency model and how to use it effectively. But who actually wants to read all this!? This repo tries to teach concurrency patterns by following the 'learning by doing' approach.

Image of excited gopher

How to take this challenge

  1. Only edit main.go to solve the problem. Do not touch any of the other files.
  2. If you find a *_test.go file, you can test the correctness of your solution with go test
  3. If you get stuck, join us on Discord or Slack! Surely there are people who are happy to give you some code reviews (if not, find me via @loong ;) )

Overview

# Name of the Challenge + URL
0 Limit your Crawler
1 Producer-Consumer
2 Race Condition in Caching Cache
3 Limit Service Time for Free-tier Users
4 Graceful SIGINT Killing
5 Clean Inactive Sessions to Prevent Memory Overflow

License

 DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 
                    Version 2, December 2004 

 Copyleft from 2017 Long Hoang

 Everyone is permitted to copy and distribute verbatim or modified 
 copies of this license document, and changing it is allowed as long 
 as the name is changed.

            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 
   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 

  0. You just DO WHAT THE FUCK YOU WANT TO.

go-concurrency-exercises's People

Contributors

arvin-gao avatar benprew avatar iartarisi avatar loong avatar malfple avatar rationull avatar tushar00jain avatar tyronehou avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

go-concurrency-exercises's Issues

More hints?

Hey,

First off, thanks a lot for this project. It's a great help for learning exactly those things that I'm interested in learning more about with go!

Now I'm new to the language and I don't know most of the good practices and sometimes not even the basic concurrency primitives. I've gone through the first three problems and have some solutions to them in my own fork, but I don't know if the solutions I come up with are idiomatic, or even decent. It would be great if there was a way to find a hint for solutions, that was somehow hidden away from the main readme. Maybe something as basic as a spoiler.md file that would contain something like: "you can do this with three lines of code using a Mutex. Btw, you don't need to put the mutex around the whole function, just lines 11-13 works".

What do you think about including some sort of hints to help newbies know that they're on the right path and not just hacking poorly-thought solutions?

Exercise 0: Test fail for correct solution when crawling is started immediately

Hi, I used channel and time.Sleep to work out this exercise,
If i write like this

<-ch
fetcher.Fetch(url)
time.Sleep(1s)
ch <- 1

test would fail, and code below would work

<-ch
Sleep()
Fetch
ch <- 1

I guess that check_test shouldn't check time gap when first crawl,
or may initial start in check_test to a very small value to avoid this case :)
(it is my first time to open an issue, i'm bit of nervous :S, sorry if i'm not wrting in the proper way)

How to measure the test duration in exercise 2?

As mentioned in /2-race-in-cache/README.md

If possible, get your solution down to less than 5 seconds for all tests.

And you also mention to run the test, we run the following command

Use the following command to test for race conditions and correct functionality:

go test -race

Does the "5 second for all tests." should happen when running go test or go test -race?

Because this is what happened in my local

➜  2-race-in-cache git:(main) ✗ go test -race
PASS
ok      github.com/loong/go-concurrency-exercises/2-race-in-cache       5.178s
➜  2-race-in-cache git:(main) ✗ go test      
PASS
ok      github.com/loong/go-concurrency-exercises/2-race-in-cache       4.122s

I'm quite confused if I have passed the 5 seconds threshold or nah

Thanks!

Revamping repo, suggestions welcome!

Hello everyone,

I'm grateful to come back to this project and see we have passed 500 stars! This project was originally made for fun to show at a Go Meetup in Hsinchu, Taiwan (where only 2 other people showed up).

This repo def deserves some revamping! Most of this was made back in 2016, and Go as a language has evolved since then!

Let me know if you have any wishes! I keep a list of ideas below.

Ideas

  • Introduce go modules

2-race-in-cache Test Cache/Page size fail

Hi, I'm trying with this solution

// Get gets the key from cache, loads it from the source if needed
func (k *KeyStoreCache) Get(key string) string {
	//mutex.Lock()
	//defer mutex.Unlock()

	k.rw.RLock()
	e, ok := k.cache[key]
	k.rw.RUnlock()

	if !ok {
		k.rw.Lock()

		// Miss - load from database and save it in cache
		p := page{key, k.load(key)}
		// if cache is full remove the least used item
		if len(k.cache) >= CacheSize {
			end := k.pages.Back()
			// remove from map
			delete(k.cache, end.Value.(page).Key)
			// remove from list
			k.pages.Remove(end)
		}
		k.pages.PushFront(p)
		k.cache[key] = k.pages.Front()

		e = k.pages.Front()
		k.rw.Unlock()
	} else {
		k.rw.Lock()
		k.pages.MoveToFront(e)
		k.rw.Unlock()
	}

	return e.Value.(page).Value
}

But after run the tests, it throws me:

--- FAIL: TestMain (3.75s)
check_test.go:20: Incorrect cache size 99
check_test.go:23: Incorrect pages size 181
FAIL
exit status 1
FAIL github.com/loong/go-concurrency-exercises/2-race-in-cache 5.811s

After lookin that rule, I see it checks that cache and pages are always on their max length (100) but if the Get functions is executed concurrently adding and removing items, could be possible that pages and cache size can not always be 100?

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.