This commit is contained in:
wagslane
2023-05-01 15:25:27 -06:00
parent f8912668b8
commit 9be3074de6
868 changed files with 58698 additions and 2 deletions

View File

@@ -0,0 +1,38 @@
package main
import (
"fmt"
"time"
)
func waitForDbs(numDBs int, dbChan chan struct{}) {
// ?
}
// don't touch below this line
func test(numDBs int) {
dbChan := getDatabasesChannel(numDBs)
fmt.Printf("Waiting for %v databases...\n", numDBs)
waitForDbs(numDBs, dbChan)
time.Sleep(time.Millisecond * 10) // ensure the last print statement happens
fmt.Println("All databases are online!")
fmt.Println("=====================================")
}
func main() {
test(3)
test(4)
test(5)
}
func getDatabasesChannel(numDBs int) chan struct{} {
ch := make(chan struct{})
go func() {
for i := 0; i < numDBs; i++ {
ch <- struct{}{}
fmt.Printf("Database %v is online\n", i+1)
}
}()
return ch
}

View File

@@ -0,0 +1,40 @@
package main
import (
"fmt"
"time"
)
func waitForDbs(numDBs int, dbChan chan struct{}) {
for i := 0; i < numDBs; i++ {
<-dbChan
}
}
// don't touch below this line
func test(numDBs int) {
dbChan := getDatabasesChannel(numDBs)
fmt.Printf("Waiting for %v databases...\n", numDBs)
waitForDbs(numDBs, dbChan)
time.Sleep(time.Millisecond * 10) // ensure the last print statement happens
fmt.Println("All databases are online!")
fmt.Println("=====================================")
}
func main() {
test(3)
test(4)
test(5)
}
func getDatabasesChannel(numDBs int) chan struct{} {
ch := make(chan struct{})
go func() {
for i := 0; i < numDBs; i++ {
ch <- struct{}{}
fmt.Printf("Database %v is online\n", i+1)
}
}()
return ch
}

View File

@@ -0,0 +1,21 @@
Waiting for 3 databases...
Database 1 is online
Database 2 is online
Database 3 is online
All databases are online!
=====================================
Waiting for 4 databases...
Database 1 is online
Database 2 is online
Database 3 is online
Database 4 is online
All databases are online!
=====================================
Waiting for 5 databases...
Database 1 is online
Database 2 is online
Database 3 is online
Database 4 is online
Database 5 is online
All databases are online!
=====================================

View File

@@ -0,0 +1,17 @@
# Channels
Empty structs are often used as `tokens` in Go programs. In this context, a token is a [unary](https://en.wikipedia.org/wiki/Unary_operation) value. In other words, we don't care *what* is passed through the channel. We care *when* and *if* it is passed.
We can block and wait until *something* is sent on a channel using the following syntax
```go
<-ch
```
This will block until it pops a single item off the channel, then continue, discarding the item.
## Assignment
Our Mailio server isn't able to boot up until it receives the signal that its databases are all online, and it learns about them being online by waiting for tokens (empty structs) on a channel.
Complete the `waitForDbs` function. It should block until it receives `numDBs` tokens on the `dbChan` channel. Each time it reads a token the `getDatabasesChannel` goroutine will print a message to the console for you.