Skip to content

Commit

Permalink
Making sure goroutines exit properly, adding stream context handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ShivanshVij committed Sep 8, 2024
1 parent 2a7e437 commit 5f96233
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 79 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/loopholelabs/frpc-go
go 1.22

require (
github.com/loopholelabs/frisbee-go v0.9.2
github.com/loopholelabs/frisbee-go v0.9.3-0.20240908010142-de266dc4d891
github.com/loopholelabs/logging v0.3.0
github.com/loopholelabs/polyglot/v2 v2.0.2
github.com/loopholelabs/testing v0.2.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/loopholelabs/common v0.4.9 h1:9MPUYlZZ/qx3Kt8LXgXxcSXthrM91od8026c4DlGpAU=
github.com/loopholelabs/common v0.4.9/go.mod h1:Wop5srN1wYT+mdQ9gZ+kn2I9qKAyVd0FB48pThwIa9M=
github.com/loopholelabs/frisbee-go v0.9.2 h1:TiihZMh/aq82olM3JEY0h5WiSHOji9lheaQ26V35zK0=
github.com/loopholelabs/frisbee-go v0.9.2/go.mod h1:RwKglItbNcQq9UW6Vm2aOwOWXR6wTxUNjvuXcCIyJkU=
github.com/loopholelabs/frisbee-go v0.9.3-0.20240908010142-de266dc4d891 h1:4Kt+YuiZYGB7BIZtS41Ynzjbqq7ekLLKB6yYhd1upcA=
github.com/loopholelabs/frisbee-go v0.9.3-0.20240908010142-de266dc4d891/go.mod h1:RwKglItbNcQq9UW6Vm2aOwOWXR6wTxUNjvuXcCIyJkU=
github.com/loopholelabs/logging v0.3.0 h1:Rfo9fGdBk4nwbNdiLrVUF7JkYoC67dvGDodkRe81xF0=
github.com/loopholelabs/logging v0.3.0/go.mod h1:uRDUydiqPqKbZkb0WoQ3dfyAcJ2iOMhxdEafZssLVv0=
github.com/loopholelabs/polyglot/v2 v2.0.2 h1:v308fg2ZKSvkKDnWgBnDvvmiu4YypCxcDe5Ih5GUVnY=
Expand Down
1 change: 0 additions & 1 deletion pkg/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ func (g *Generator) Generate(req *pluginpb.CodeGeneratorRequest) (res *pluginpb.
"package": packageName,
"requiredImports": requiredImports,
"serviceImports": serviceImports,
"methodImports": methodImports,
"streamMethodImports": streamMethodImports,
"numServices": numServices,
"numMethods": numMethods,
Expand Down
7 changes: 2 additions & 5 deletions pkg/generator/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ var (
requiredImports = []string{
"errors",
"net",
"sync",
"context",
"github.com/loopholelabs/polyglot/v2",
}

serviceImports = []string{
"context",
"crypto/tls",
"github.com/loopholelabs/frisbee-go",
"github.com/loopholelabs/frisbee-go/pkg/packet",
Expand All @@ -21,8 +22,4 @@ var (
"sync/atomic",
"io",
}

methodImports = []string{
"sync",
}
)
6 changes: 3 additions & 3 deletions pkg/generator/test/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (s svc) Echo(_ context.Context, request *Request) (*Response, error) {
}}, nil
}

func (s svc) EchoStream(srv *EchoStreamServer) error {
func (s svc) EchoStream(_ context.Context, srv *EchoStreamServer) error {
for {
request, err := srv.Recv()
if err == io.EOF {
Expand Down Expand Up @@ -52,7 +52,7 @@ func (s svc) Testy(_ context.Context, _ *SearchResponse) (*StockPricesWrapper, e
panic("not implemented")
}

func (s svc) Search(req *SearchResponse, srv *SearchServer) error {
func (s svc) Search(_ context.Context, req *SearchResponse, srv *SearchServer) error {
assert.Equal(s.t, 1, len(req.Results))
for i := 0; i < 10; i++ {
err := srv.Send(&Response{Message: "Hello World", Test: &Data{
Expand All @@ -64,7 +64,7 @@ func (s svc) Search(req *SearchResponse, srv *SearchServer) error {
return srv.CloseSend()
}

func (s svc) Upload(srv *UploadServer) error {
func (s svc) Upload(_ context.Context, srv *UploadServer) error {
received := 0
for {
res, err := srv.Recv()
Expand Down
91 changes: 56 additions & 35 deletions pkg/generator/test/test.frpc.go

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

5 changes: 0 additions & 5 deletions templates/imports.templ
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ import (
"{{$im}}"
{{end -}}
{{end}}
{{ if .numMethods }}
{{range $im := .methodImports -}}
"{{$im}}"
{{end -}}
{{end}}
{{ if .numStreamMethods -}}
{{range $im := .streamMethodImports -}}
"{{$im}}"
Expand Down
4 changes: 2 additions & 2 deletions templates/interfaces.templ
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ type {{ CamelCaseName .Name }} interface {
{{ range $i, $v := MakeIterable .Methods.Len -}}
{{ $method := $.Methods.Get $i -}}
{{ if $method.IsStreamingClient }}
{{ CamelCaseName $method.Name }} (srv *{{ CamelCaseName $method.Name }}Server) error
{{ CamelCaseName $method.Name }} (context.Context, *{{ CamelCaseName $method.Name }}Server) error
{{ else if $method.IsStreamingServer }}
{{ CamelCaseName $method.Name }} (req *{{ CamelCase $method.Input.FullName }}, srv *{{ CamelCaseName $method.Name }}Server) error
{{ CamelCaseName $method.Name }} (context.Context, *{{ CamelCase $method.Input.FullName }}, *{{ CamelCaseName $method.Name }}Server) error
{{else -}}
{{ CamelCaseName $method.Name }} (context.Context, *{{ CamelCase $method.Input.FullName }}) (*{{ CamelCase $method.Output.FullName }}, error)
{{end -}}
Expand Down
Loading

0 comments on commit 5f96233

Please sign in to comment.