Skip to content

Commit

Permalink
Remove RespStream type and replace with chan-based API for download-s…
Browse files Browse the repository at this point in the history
…tyle rpcs
  • Loading branch information
mikeylemmon committed Jun 20, 2018
1 parent 8f561c5 commit 88bcfbb
Show file tree
Hide file tree
Showing 7 changed files with 269 additions and 498 deletions.
14 changes: 5 additions & 9 deletions example/cmd/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ package main

import (
"context"
"io"
"log"
"net/http"
"time"
Expand Down Expand Up @@ -75,19 +74,16 @@ func main() {
khps := float64(ii-1) / took.Seconds() / 1000
log.Printf("Received %.1f kHats per second (%d hats in %f seconds)\n", khps, ii-1, took.Seconds())
}
for ; true; ii++ { // Receive all the hats
hat, err = hatStream.Next(context.Background())
if err != nil {
if err == io.EOF {
break
}
for hatOrErr := range hatStream {
if hatOrErr.Err != nil {
printResults()
log.Fatal(err)
log.Fatal(hatOrErr.Err)
}
if ii%printEvery == 0 {
khps := float64(ii) / time.Now().Sub(reqSentAt).Seconds() / 1000
log.Printf("\t[%4.1f khps] %6d %+v\n", khps, ii, hat)
log.Printf("\t[%4.1f khps] %6d %+v\n", khps, ii, hatOrErr.Msg)
}
ii++
}
printResults()
}
6 changes: 3 additions & 3 deletions example/cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (h *randomHaberdasher) MakeHat(ctx context.Context, size *example.Size) (*e
return newRandomHat(size.Inches)
}

func (h *randomHaberdasher) MakeHats(ctx context.Context, req *example.MakeHatsReq) (example.HatStream, error) {
func (h *randomHaberdasher) MakeHats(ctx context.Context, req *example.MakeHatsReq) (<-chan example.HatOrError, error) {
if req.Quantity < 0 {
return nil, errNegativeQuantity
}
Expand All @@ -63,12 +63,12 @@ func (h *randomHaberdasher) MakeHats(ctx context.Context, req *example.MakeHatsR
select {
case <-ctx.Done():
return
case ch <- example.HatOrError{Hat: hat, Err: err}:
case ch <- example.HatOrError{Msg: hat, Err: err}:
}
}
close(ch)
}()
return example.NewHatStream(ch), nil
return ch, nil
}

func main() {
Expand Down
27 changes: 10 additions & 17 deletions example/cmd/server/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package main
import (
"context"
"fmt"
"io"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -116,8 +115,8 @@ func TestMakeHatsTooSmall(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, err = hatStream.Next(context.Background())
err = compareErrors(err, errTooSmall)
hatOrErr := <-hatStream
err = compareErrors(hatOrErr.Err, errTooSmall)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -148,14 +147,11 @@ func TestMakeHatsLargeQuantities(t *testing.T) {
t.Fatalf(`MakeHats request failed: %#v (hatStream=%#v)`, err, hatStream)
}
ii := int32(0)
for ; true; ii++ {
_, err = hatStream.Next(context.Background())
if err == io.EOF {
break
}
if err != nil {
t.Fatal(err)
for hatOrErr := range hatStream {
if hatOrErr.Err != nil {
t.Fatal(hatOrErr.Err)
}
ii++
}
if ii != re.req.Quantity {
t.Fatalf(`Expected to receive %d hats, got %d`, re.req.Quantity, ii)
Expand Down Expand Up @@ -189,14 +185,11 @@ func benchmarkMakeHats(b *testing.B, cc example.Haberdasher) {
b.Fatal(err)
}
ii := 0
for ; true; ii++ {
_, err = hatStream.Next(context.Background())
if err != nil {
if err == io.EOF {
break
}
b.Fatal(err)
for hatOrErr := range hatStream {
if hatOrErr.Err != nil {
b.Fatal(hatOrErr.Err)
}
ii++
}
if ii != b.N {
b.Fatalf(`Expected to receive %d hats, got %d`, b.N, ii)
Expand Down
169 changes: 72 additions & 97 deletions example/service.twirp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 88bcfbb

Please sign in to comment.