Skip to content

Commit

Permalink
Fix: multi-client method names (#6)
Browse files Browse the repository at this point in the history
* fixing double-client bug

* Updating changelog

* Updating changelog

* Updating trunk and fixing lint

* updating changelog
  • Loading branch information
ShivanshVij authored Aug 24, 2022
1 parent 4da9798 commit 8ebbd0d
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 24 deletions.
3 changes: 2 additions & 1 deletion .trunk/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*out
*logs
*logs
external
1 change: 1 addition & 0 deletions .trunk/plugins/trunk
2 changes: 1 addition & 1 deletion .trunk/trunk.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
version: 0.1
cli:
version: 0.15.0-beta
version: 0.16.1-beta
lint:
enabled:
- actionlint@1.6.13
Expand Down
14 changes: 13 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [v0.6.0] - 2022-08-24 (Beta)

## Changes

- Refactoring the generated code to use the [polyglot-go](https://github.com/loopholelabs/polyglot-go) library to generate message encode/decode functions. ([#3](https://github.com/loopholelabs/frpc-go/pull/3))

## Fixes

- Fixed an issue with the generated code that caused compilation issues when the names of two methods in different services
were the same ([#5](https://github.com/loopholelabs/frpc-go/issues/5))

## [v0.5.1] - 2022-07-20 (Beta)

## Fixes
Expand All @@ -16,6 +27,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

> Changelogs for [v0.5.0] and before can be found at https://github.com/loopholelabs/frisbee-go
[unreleased]: https://github.com/loopholelabs/frpc-go/compare/v0.5.1...HEAD
[unreleased]: https://github.com/loopholelabs/frpc-go/compare/v0.6.0...HEAD
[v0.6.0]: https://github.com/loopholelabs/frpc-go/releases/tag/v0.6.0
[v0.5.1]: https://github.com/loopholelabs/frpc-go/releases/tag/v0.5.1
[v0.5.0]: https://github.com/loopholelabs/frisbee-go/compare/v0.4.6...v0.5.0
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ same is true for selected other new features explicitly marked as

Usage instructions and documentation for fRPC is available at [https://frpc.io/](https://frpc.io/).

fRPC is still in very early **Alpha*. There may be bug in the library that will be fixed
fRPC is still in very early \*_Alpha_. There may be bug in the library that will be fixed
as the library matures and usage of fRPC grows. One of the major benefits to fRPC is that reading the generated code
is extremely straight forward, making it easy to debug potential issues down the line.

Expand Down
11 changes: 10 additions & 1 deletion examples/echo/echo.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,17 @@ service EchoService {
rpc Echo(Request) returns (Response);
}

service TestService {
rpc Echo(Request) returns (Response);
}

message Array {
repeated string value = 1;
}

message Request {
string Message = 1;
map <string, Array> metadata = 1;
string Message = 2;
}

message Response{
Expand Down
2 changes: 1 addition & 1 deletion internal/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
package version

const (
Version = "v0.5.1"
Version = "v0.6.0"
)
50 changes: 32 additions & 18 deletions templates/client.templ
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
{{define "client"}}
{{ range $i, $v := (MakeIterable .services.Len) -}}
{{ $service := $.services.Get $i -}}
type sub{{ CamelCaseName $service.Name }}Client struct {
client *frisbee.Client
{{ range $i, $v := (MakeIterable $service.Methods.Len) -}}
{{ $method := $service.Methods.Get $i -}}
next{{ CamelCaseName $method.Name }} uint16
next{{ CamelCaseName $method.Name }}Mu sync.RWMutex
inflight{{ CamelCaseName $method.Name }} map[uint16]chan *{{ CamelCase $method.Output.FullName }}
inflight{{ CamelCaseName $method.Name }}Mu sync.RWMutex
{{end -}}
}
{{end -}}


type Client struct {
*frisbee.Client
{{ range $i, $v := (MakeIterable .services.Len) -}}
{{ $service := $.services.Get $i -}}
{{ range $i, $v := (MakeIterable $service.Methods.Len) -}}
{{ $method := $service.Methods.Get $i -}}
next{{ CamelCaseName $method.Name }} uint16
next{{ CamelCaseName $method.Name }}Mu sync.RWMutex
inflight{{ CamelCaseName $method.Name }} map[uint16]chan *{{ CamelCase $method.Output.FullName }}
inflight{{ CamelCaseName $method.Name }}Mu sync.RWMutex
{{end -}}
{{ CamelCaseName $service.Name }} *sub{{ CamelCaseName $service.Name }}Client
{{end -}}
}

Expand All @@ -33,13 +42,18 @@ func NewClient (tlsConfig *tls.Config, logger *zerolog.Logger) (*Client, error)

{{ range $i, $v := (MakeIterable .services.Len) -}}
{{ $service := $.services.Get $i -}}
c.{{ CamelCaseName $service.Name }} = new(sub{{ CamelCaseName $service.Name }}Client)
c.{{ CamelCaseName $service.Name }}.client = c.Client
{{ range $i, $v := (MakeIterable $service.Methods.Len) -}}
{{ $method := $service.Methods.Get $i -}}

c.next{{ CamelCaseName $method.Name }}Mu.Lock()
c.next{{ CamelCaseName $method.Name }} = 0
c.next{{ CamelCaseName $method.Name }}Mu.Unlock()
c.inflight{{ CamelCaseName $method.Name }} = make(map[uint16]chan *{{ CamelCase $method.Output.FullName }})
c.{{ CamelCaseName $service.Name }}.inflight{{ CamelCaseName $method.Name }} = make(map[uint16]chan *{{ CamelCase $method.Output.FullName }})
{{end -}}
{{ range $i, $v := (MakeIterable $service.Methods.Len) -}}
{{ $method := $service.Methods.Get $i -}}
c.{{ CamelCaseName $service.Name }}.next{{ CamelCaseName $method.Name }}Mu.Lock()
c.{{ CamelCaseName $service.Name }}.next{{ CamelCaseName $method.Name }} = 0
c.{{ CamelCaseName $service.Name }}.next{{ CamelCaseName $method.Name }}Mu.Unlock()
c.{{ CamelCaseName $service.Name }}.inflight{{ CamelCaseName $method.Name }} = make(map[uint16]chan *{{ CamelCase $method.Output.FullName }})
{{end -}}
{{end -}}
return c, nil
Expand All @@ -56,14 +70,14 @@ func NewClient (tlsConfig *tls.Config, logger *zerolog.Logger) (*Client, error)
{{ $method := $service.Methods.Get $i -}}
{{ $count := call $counter -}}
table[{{ $count }}] = func(ctx context.Context, incoming *packet.Packet) (outgoing *packet.Packet, action frisbee.Action) {
c.inflight{{ CamelCaseName $method.Name }}Mu.RLock()
if ch, ok := c.inflight{{ CamelCaseName $method.Name }}[incoming.Metadata.Id]; ok {
c.inflight{{ CamelCaseName $method.Name }}Mu.RUnlock()
c.{{ CamelCaseName $service.Name }}.inflight{{ CamelCaseName $method.Name }}Mu.RLock()
if ch, ok := c.{{ CamelCaseName $service.Name }}.inflight{{ CamelCaseName $method.Name }}[incoming.Metadata.Id]; ok {
c.{{ CamelCaseName $service.Name }}.inflight{{ CamelCaseName $method.Name }}Mu.RUnlock()
res := New{{ CamelCase $method.Output.FullName }}()
res.Decode((*incoming.Content)[:incoming.Metadata.ContentLength])
ch <- res
} else {
c.inflight{{ CamelCaseName $method.Name }}Mu.RUnlock()
c.{{ CamelCaseName $service.Name }}.inflight{{ CamelCaseName $method.Name }}Mu.RUnlock()
}
return
}
Expand All @@ -78,7 +92,7 @@ func NewClient (tlsConfig *tls.Config, logger *zerolog.Logger) (*Client, error)
{{ range $i, $v := (MakeIterable $service.Methods.Len) }}
{{ $method := $service.Methods.Get $i -}}
{{ $opIndex := call $counter -}}
func (c *Client) {{ CamelCaseName $method.Name }}(ctx context.Context, req *{{ CamelCase $method.Input.FullName }}) (res *{{ CamelCase $method.Output.FullName }}, err error) {
func (c *sub{{ CamelCaseName $service.Name }}Client) {{ CamelCaseName $method.Name }}(ctx context.Context, req *{{ CamelCase $method.Input.FullName }}) (res *{{ CamelCase $method.Output.FullName }}, err error) {
ch := make(chan *{{ CamelCase $method.Output.FullName }}, 1)
p := packet.Get()
p.Metadata.Operation = {{ $opIndex }}
Expand All @@ -94,7 +108,7 @@ func NewClient (tlsConfig *tls.Config, logger *zerolog.Logger) (*Client, error)
c.inflight{{ CamelCaseName $method.Name }}Mu.Lock()
c.inflight{{ CamelCaseName $method.Name }}[id] = ch
c.inflight{{ CamelCaseName $method.Name }}Mu.Unlock()
err = c.Client.WritePacket(p)
err = c.client.WritePacket(p)
if err != nil {
packet.Put(p)
return
Expand Down

0 comments on commit 8ebbd0d

Please sign in to comment.