Skip to content

Commit 286afe2

Browse files
committed
feat(go): implement TCP serve
Signed-off-by: Roman Volosatovs <rvolosatovs@riseup.net>
1 parent e8d8356 commit 286afe2

34 files changed

Lines changed: 730 additions & 163 deletions

File tree

crates/wit-bindgen-go/src/interface.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2146,12 +2146,12 @@ impl InterfaceGenerator<'_> {
21462146
}}
21472147
return func(w {wrpc}.IndexWriter) (err error) {{
21482148
defer func() {{
2149-
{slog}.Debug("closing stream writer")
2149+
{slog}.Debug("closing outgoing pending stream")
21502150
if cErr := v.Close(); cErr != nil {{
21512151
if err == nil {{
2152-
err = {fmt}.Errorf("failed to close pending stream: %w", cErr)
2152+
err = {fmt}.Errorf("failed to close outgoing pending stream: %w", cErr)
21532153
}} else {{
2154-
{slog}.Warn("failed to close pending stream", "err", cErr)
2154+
{slog}.Warn("failed to close outgoing pending stream", "err", cErr)
21552155
}}
21562156
}}
21572157
}}()
@@ -2637,14 +2637,14 @@ func ServeInterface(s {wrpc}.Server, h Handler) (stop func() error, err error) {
26372637
self.src,
26382638
r#"
26392639
case {idx}:
2640-
w, err := w.Index("#,
2640+
w, err := w.Index({i}"#,
26412641
);
26422642
if is_tuple(self.resolve, ty) {
2643-
uwrite!(self.src, "{i}, ");
2643+
uwrite!(self.src, ", {j}");
26442644
}
26452645
uwrite!(
26462646
self.src,
2647-
r#"{j})
2647+
r#")
26482648
if err != nil {{
26492649
{slog}.ErrorContext(ctx, "failed to index result writer", "instance", "{instance}", "name", "{name}", "err", err)
26502650
return
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
// Generated by `wit-bindgen-wrpc-go` 0.11.0. DO NOT EDIT!
1+
// Generated by `wit-bindgen-wrpc-go` 0.12.0. DO NOT EDIT!
22
// client package contains wRPC bindings for `client` world
33
package client

examples/go/hello-client/bindings/wrpc_examples/hello/handler/bindings.wrpc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Generated by `wit-bindgen-wrpc-go` 0.11.0. DO NOT EDIT!
1+
// Generated by `wit-bindgen-wrpc-go` 0.12.0. DO NOT EDIT!
22
package handler
33

44
import (

examples/go/hello-client/cmd/hello-client-tcp/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
func run() error {
1111
addr := "[::1]:7761"
12-
return app.Run(addr, wrpctcp.NewClient(addr))
12+
return app.Run(addr, wrpctcp.NewInvoker(addr))
1313
}
1414

1515
func main() {

examples/go/hello-server/bindings/exports/wrpc_examples/hello/handler/bindings.wrpc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Generated by `wit-bindgen-wrpc-go` 0.11.0. DO NOT EDIT!
1+
// Generated by `wit-bindgen-wrpc-go` 0.12.0. DO NOT EDIT!
22
package handler
33

44
import (

examples/go/hello-server/bindings/server.wrpc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Generated by `wit-bindgen-wrpc-go` 0.11.0. DO NOT EDIT!
1+
// Generated by `wit-bindgen-wrpc-go` 0.12.0. DO NOT EDIT!
22
// server package contains wRPC bindings for `server` world
33
package server
44

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"errors"
6+
"fmt"
7+
"log"
8+
"log/slog"
9+
"net"
10+
"os"
11+
"os/signal"
12+
"syscall"
13+
14+
app "wrpc.io/examples/go/hello-server"
15+
server "wrpc.io/examples/go/hello-server/bindings"
16+
wrpctcp "wrpc.io/go/x/tcp"
17+
)
18+
19+
func run() (err error) {
20+
l, err := net.ListenTCP("tcp", &net.TCPAddr{
21+
IP: net.IPv6loopback,
22+
Port: 7761,
23+
})
24+
if err != nil {
25+
return fmt.Errorf("failed to listen on TCP socket: %w", err)
26+
}
27+
28+
ctx := context.Background()
29+
ctx, cancel := context.WithCancel(ctx)
30+
defer cancel()
31+
32+
srv := wrpctcp.NewServerWithContext(ctx, l)
33+
stop, err := server.Serve(srv, app.Handler{})
34+
if err != nil {
35+
return fmt.Errorf("failed to serve `server` world: %w", err)
36+
}
37+
38+
go func() {
39+
for {
40+
select {
41+
case <-ctx.Done():
42+
return
43+
default:
44+
slog.Debug("accepting invocation")
45+
if err := srv.Accept(); err != nil {
46+
if !errors.Is(err, net.ErrClosed) {
47+
slog.Error("failed to accept invocation", "err", err)
48+
}
49+
}
50+
}
51+
}
52+
}()
53+
54+
signalCh := make(chan os.Signal, 1)
55+
signal.Notify(signalCh, syscall.SIGINT)
56+
<-signalCh
57+
58+
if err = stop(); err != nil {
59+
return fmt.Errorf("failed to stop `server` world: %w", err)
60+
}
61+
return nil
62+
}
63+
64+
func main() {
65+
if err := run(); err != nil {
66+
log.Fatal(err)
67+
}
68+
}

examples/go/resources-server/bindings/exports/wrpc_examples/resources/resources/bindings.wrpc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Generated by `wit-bindgen-wrpc-go` 0.11.0. DO NOT EDIT!
1+
// Generated by `wit-bindgen-wrpc-go` 0.12.0. DO NOT EDIT!
22
package resources
33

44
import (

examples/go/resources-server/bindings/server.wrpc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Generated by `wit-bindgen-wrpc-go` 0.11.0. DO NOT EDIT!
1+
// Generated by `wit-bindgen-wrpc-go` 0.12.0. DO NOT EDIT!
22
// server package contains wRPC bindings for `server` world
33
package server
44

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
// Generated by `wit-bindgen-wrpc-go` 0.11.0. DO NOT EDIT!
1+
// Generated by `wit-bindgen-wrpc-go` 0.12.0. DO NOT EDIT!
22
// client package contains wRPC bindings for `client` world
33
package client

0 commit comments

Comments
 (0)