|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "sync" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +// for multi threading we need wait groups |
| 11 | +var wg sync.WaitGroup // pointer |
| 12 | + |
| 13 | + |
| 14 | +// understanding mutex |
| 15 | +var mut sync.Mutex |
| 16 | +var signals = []string{} |
| 17 | + |
| 18 | +func main() { |
| 19 | + // its concurrently works |
| 20 | + go greeter("Getting into") |
| 21 | + greeter("Go routines 🙏") |
| 22 | + |
| 23 | + waitlist := []string{ |
| 24 | + "https://google.com", |
| 25 | + "https://aniketpal.vercel.app", |
| 26 | + "https://github.com", |
| 27 | + } |
| 28 | + |
| 29 | + // single thread operation |
| 30 | + for _, website := range waitlist{ |
| 31 | + GetStatusCode(website) |
| 32 | + } |
| 33 | + |
| 34 | + // multi thread operation - go routines |
| 35 | + // using waitgroups for time sync |
| 36 | + for _, website := range waitlist{ |
| 37 | + go GetStatusCodeWait(website) |
| 38 | + wg.Add(1) |
| 39 | + } |
| 40 | + wg.Wait() |
| 41 | +} |
| 42 | + |
| 43 | +func greeter(s string) { |
| 44 | + for i :=0; i<3;i++{ |
| 45 | + /* |
| 46 | + using sleep so that concurrency breaks |
| 47 | + and func start to execute the statement again |
| 48 | + else the function which goes works on thread |
| 49 | + never gets excecuted as we are not providing a restart sort of thing for the server. |
| 50 | + */ |
| 51 | + time.Sleep(2 * time.Millisecond) |
| 52 | + fmt.Println(s) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// single thread |
| 57 | +func GetStatusCode(endpoint string) { |
| 58 | + res , err := http.Get(endpoint) |
| 59 | + |
| 60 | + if err!= nil{ |
| 61 | + fmt.Println("Endpoint not reachable") |
| 62 | + }else{ |
| 63 | + fmt.Printf("%d status code for %s\n",res.StatusCode,endpoint) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +// multi thread |
| 68 | +func GetStatusCodeWait(endpoint string) { |
| 69 | + defer wg.Done() |
| 70 | + res , err := http.Get(endpoint) |
| 71 | + |
| 72 | + if err!= nil{ |
| 73 | + fmt.Println("Endpoint not reachable") |
| 74 | + }else{ |
| 75 | + // until one thread is executed it will not do anything more |
| 76 | + mut.Lock() |
| 77 | + signals = append(signals, endpoint) |
| 78 | + mut.Unlock() |
| 79 | + fmt.Printf("%d status code for %s\n",res.StatusCode,endpoint) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +/* |
| 84 | +In cloud there is no shortage of threads |
| 85 | +and go routines are not absolutely dependent on OS |
| 86 | +to generate a thread to excecute. So it becomes efficient to |
| 87 | +use Go in cloud native architecture. |
| 88 | +
|
| 89 | +
|
| 90 | +--- |
| 91 | +
|
| 92 | +Thread: |
| 93 | +- Managed by OS |
| 94 | +- Fixed Stack 1MB |
| 95 | +
|
| 96 | +Go Routines |
| 97 | +- Managed by Go Runtime |
| 98 | +- Flexible length 2KB |
| 99 | +*/ |
| 100 | + |
| 101 | + |
| 102 | +/* |
| 103 | +Mutex is a mutual exclusion lock. The zero |
| 104 | +value fo Mutex is an unlocked mutex. |
| 105 | +
|
| 106 | +It looks the memory , so when one goroutine is writing |
| 107 | +something in the memory it will not allow anything else to write on it |
| 108 | +
|
| 109 | +Similarly for RWMutex |
| 110 | +While one operation is being running it will run until interrupted |
| 111 | +*/ |
0 commit comments