Skip to content

Commit

Permalink
Don't return ErrServerClosed
Browse files Browse the repository at this point in the history
  • Loading branch information
tmaxmax committed Sep 2, 2021
1 parent 5131f1a commit a7c3dd6
Showing 1 changed file with 8 additions and 17 deletions.
25 changes: 8 additions & 17 deletions cmd/complex_client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,23 @@ func main() {
defer cancel()

r, _ := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost:8080/events", nil)
conn := client.NewConnection(r)
done := make(chan struct{})
conn, done, events := client.NewConnection(r), make(chan struct{}), make(chan *client.Event, 1)

go func() {
ch := make(chan *client.Event, 1)
conn.SubscribeToAll(ch)
conn.SubscribeToAll(events)

for ev := range ch {
switch ev.Name {
go func() {
for event := range events {
switch event.Name {
case "cycles", "ops":
handleMetric(ev.Name, ev.String())
fmt.Printf("Metric %s: %s\n", event.Name, event)
case "close":
fmt.Println("Server closed!")
cancel()
default: // no event name
numbers := strings.Split(ev.String(), "\n")
sum := big.NewInt(0)

for _, n := range numbers {
for _, n := range strings.Split(event.String(), "\n") {
v, _ := strconv.ParseInt(n, 10, 64)
sum = sum.Add(sum, big.NewInt(v))
sum.Add(sum, big.NewInt(v))
}

fmt.Printf("Sum of random numbers: %s\n", sum)
Expand All @@ -57,8 +53,3 @@ func main() {

<-done
}

func handleMetric(metric, value string) {
v, _ := strconv.ParseInt(value, 10, 64)
fmt.Printf("Metric %s: %d\n", metric, v)
}

0 comments on commit a7c3dd6

Please sign in to comment.