-
-
Save emretanriverdi/0d070a7778dd7261d1b35f886ba62d6c to your computer and use it in GitHub Desktop.
channels.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func listenAndUpsert() { | |
c := make(chan User) // create a channel | |
go listenFromKafka(c) // pass channel to a goroutine | |
for msg := range c { | |
upsertToCouchbase(msg, "key") | |
} | |
} | |
func listenFromKafka(c chan User) { | |
for { | |
random := rand.Intn(100) // simulating incoming message | |
time.Sleep(2 * time.Millisecond) // simulating kafka with 2ms | |
fmt.Println("# ID " + strconv.Itoa(random) + | |
" received from Kafka") | |
c <- User{Id: random} | |
} | |
} | |
func upsertToCouchbase(user User, key string) { | |
fmt.Println("# upserting to Couchbase for " + | |
strconv.Itoa(user.Id)) | |
time.Sleep(5 * time.Millisecond) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment