Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
222 changes: 218 additions & 4 deletions _examples/golang-basics/example.gen.go

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

3 changes: 3 additions & 0 deletions _examples/golang-basics/example.ridl
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,6 @@ service Example

# Article endpoint
- GetArticle(GetArticleRequest) => (GetArticleResponse)

# Streaming article endpoint
- StreamNewArticles() => stream (GetArticleResponse)
16 changes: 16 additions & 0 deletions _examples/golang-basics/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"log"
"net/http"
"time"

"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
Expand Down Expand Up @@ -127,3 +128,18 @@ func (rpc *ExampleServiceRPC) GetArticle(ctx context.Context, req GetArticleRequ
Content: &content,
}, nil
}

func (rpc *ExampleServiceRPC) StreamNewArticles(ctx context.Context, stream StreamNewArticlesStreamWriter) error {
for i := 0; i < 4; i++ {
// content := fmt.Sprintf("This is the content of the article, %d", i)
stream.Write(&GetArticleResponse{
Title: fmt.Sprintf("Article %d", i),
// Content: &content,
})
stream.Write(nil)
time.Sleep(1 * time.Second)
}
return nil
// TODO: 1. do we not close the connection on return..? .. hmmpf.. review..
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it happens automatically in the webrpc code that calls this method

// TODO: 2. remove the nested type when using succinct mode for the writer and the reader (server/client)
}
4 changes: 2 additions & 2 deletions server.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,13 @@ func (s *{{$serviceName}}) serve{{firstLetterToUpper $method.Name}}JSONStream(ct
return
}

ctx, cancel := context.WithCancel(r.Context())
ctx, cancel := context.WithCancel(ctx)
defer cancel()

go streamWriter.keepAlive(ctx)

// Call service method implementation.
if err {{if or (eq (len $method.Inputs) 0) (gt (len $method.Outputs) 0)}}:{{end}}= s.{{$name}}.{{$method.Name}}(ctx{{range $i, $_ := $method.Inputs}}, reqPayload.Arg{{$i}}{{end}}, streamWriter); err != nil {
if err {{if or (eq (len $method.Inputs) 0) (gt (len $method.Outputs) 0)}}:{{end}}= s.{{$name}}Server.{{$method.Name}}(ctx{{range $i, $_ := $method.Inputs}}, reqPayload.Arg{{$i}}{{end}}, streamWriter); err != nil {
cancel()
rpcErr, ok := err.(WebRPCError)
if !ok {
Expand Down
6 changes: 5 additions & 1 deletion types.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,15 @@ type streamWriter struct {
mu sync.Mutex // Guards concurrent writes to w.
w http.ResponseWriter
f http.Flusher
e *json.Encoder
e jsonEncoder

sendError func(w http.ResponseWriter, r *http.Request, rpcErr WebRPCError)
}

type jsonEncoder interface {
Encode(v any) error
}

const StreamKeepAliveInterval = 10*time.Second

func (w *streamWriter) keepAlive(ctx context.Context) {
Expand Down
Loading