Skip to content

Commit

Permalink
docs: fix some problems in docs/kitex/Tutorials/basic-feature/message…
Browse files Browse the repository at this point in the history
…_type (#1040)
  • Loading branch information
zhu-mi-shan authored Mar 18, 2024
1 parent 38ed7b8 commit da8ee87
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 73 deletions.
124 changes: 90 additions & 34 deletions content/en/docs/kitex/Tutorials/basic-feature/message_type.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ The handler code on server side:
package main

import (
"context"

"xx/echo"
"xx/echo/echoservice"
)
Expand All @@ -75,7 +77,7 @@ type handler struct {}

func (handler) Echo(ctx context.Context, req *echo.Request) (r *echo.Response, err error) {
//...
return &echo.Response{ Msg: "world" }
return &echo.Response{ Msg: "world" }, err
}

func (handler) VisitOneway(ctx context.Context, req *echo.Request) (err error) {
Expand All @@ -84,11 +86,11 @@ func (handler) VisitOneway(ctx context.Context, req *echo.Request) (err error) {
}

func main() {
svr, err := echoservice.NewServer(handler{})
svr := echo.NewServer(handler{})
err := svr.Run()
if err != nil {
panic(err)
}
svr.Run()
}
```

Expand All @@ -100,21 +102,28 @@ The code on client side:
package main

import (
"context"
"fmt"

"xx/echo"
"xx/echo/echoservice"

"github.com/cloudwego/kitex/client"
)

func main() {
cli, err := echoservice.NewClient("destServiceName")
cli, err := echoservice.NewClient("destServiceName", client.WithHostPorts("0.0.0.0:8888"))
if err != nil {
panic(err)
}
req := echo.NewRequest()
req.Msg = "hello"
resp, err := cli.Echo(req)
resp, err := cli.Echo(context.Background(), req)
if err != nil {
panic(err)
}

fmt.Println(resp.Msg)
// resp.Msg == "world"
}
```
Expand All @@ -127,18 +136,22 @@ The code on client side:
package main

import (
"context"

"xx/echo"
"xx/echo/echoservice"

"github.com/cloudwego/kitex/client"
)

func main() {
cli, err := echoservice.NewClient("destServiceName")
cli, err := echoservice.NewClient("destServiceName", client.WithHostPorts("0.0.0.0:8888"))
if err != nil {
panic(err)
}
req := echo.NewRequest()
req.Msg = "hello"
err = cli.VisitOneway(req)
err = cli.VisitOneway(context.Background(), req)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -190,6 +203,7 @@ The generated code:
└── kitex_gen
└── echo
├── echo.pb.go
├── echo.pb.fast.go
└── echoservice
├── client.go
├── echoservice.go
Expand All @@ -203,7 +217,9 @@ The handler code on server side:
package main

import (
"sync"
"log"
"time"
"context"

"xx/echo"
"xx/echo/echoservice"
Expand All @@ -217,6 +233,7 @@ func (handler) ClientSideStreaming(stream echo.EchoService_ClientSideStreamingSe
if err != nil {
return err
}
log.Println("received:" , req.GetMsg())
}
}

Expand All @@ -231,37 +248,55 @@ func (handler) ServerSideStreaming(req *echo.Request, stream echo.EchoService_Se
}

func (handler) BidiSideStreaming(stream echo.EchoService_BidiSideStreamingServer) (err error) {
var once sync.Once
ctx, cancel := context.WithCancel(context.Background())
errChan := make(chan error, 1)

go func() {
for {
req, err2 := stream.Recv()
log.Println("received:", req.GetMsg())
if err2 != nil {
once.Do(func() {
err = err2
})
break
select {
case <- ctx.Done():
return
default:
req,err := stream.Recv()
if err != nil {
errChan <- err
cancel()
return
}
log.Println("received:", req.GetMsg())
}
}
}()
for {
resp := &echo.Response{Msg: "world"}
if err2 := stream.Send(resp); err2 != nil {
once.Do(func() {
err = err2
})
return
go func() {
for {
select {
case <- ctx.Done():
return
default:
resp := &echo.Response{Msg: "world"}
if err := stream.Send(resp); err != nil {
errChan <- err
cancel()
return
}
}
time.Sleep(time.Second)
}
}
return
}()

err = <-errChan
cancel()
return err
}

func main() {
svr, err := echoservice.NewServer(handler{})
if err != nil {
panic(err)
}
svr.Run()
svr := echo.NewServer(handler{})

err := svr.Run()

if err != nil {
log.Println(err.Error())
}
}
```

Expand All @@ -273,12 +308,17 @@ ClientSideStreaming:
package main

import (
"context"
"time"

"xx/echo"
"xx/echo/echoservice"

"github.com/cloudwego/kitex/client"
}

func main() {
cli, err := echoservice.NewClient("destServiceName")
cli, err := echoservice.NewClient("destServiceName", client.WithHostPorts("0.0.0.0:8888"))
if err != nil {
panic(err)
}
Expand All @@ -291,6 +331,7 @@ func main() {
if err := cliStream.Send(req); err != nil {
panic(err)
}
time.Sleep(time.Second)
}

}
Expand All @@ -302,12 +343,18 @@ ServerSideStreaming:
package main

import (
"context"
"log"
"time"

"xx/echo"
"xx/echo/echoservice"

"github.com/cloudwego/kitex/client"
}

func main() {
cli, err := echoseervice.NewClient("destServiceName")
cli, err := echoseervice.NewClient("destServiceName", client.WithHostPorts("0.0.0.0:8888"))
if err != nil {
panic(err)
}
Expand All @@ -318,9 +365,11 @@ func main() {
}
for {
resp, err := svrStream.Recv()
log.Println("response:",resp.GetMsg())
if err != nil {
panic(err)
}
time.Sleep(time.Second)
// resp.Msg == "world"
}

Expand All @@ -333,16 +382,21 @@ BidiSideStreaming:
package main

import (
"context"
"log"
"time"

"xx/echo"
"xx/echo/echoservice"

"github.com/cloudwego/kitex/client"
}

func main() {
cli, err := echoseervice.NewClient("destServiceName")
cli, err := echoservice.NewClient("destServiceName", client.WithHostPorts("0.0.0.0:8888"))
if err != nil {
panic(err)
}
req := &echo.Request{Msg: "hello"}
bidiStream, err := cli.BidiSideStreaming(context.Background())
if err != nil {
panic(err)
Expand All @@ -354,13 +408,15 @@ func main() {
if err != nil {
panic(err)
}
time.Sleep(time.Second)
}
}()
for {
resp, err := bidiStream.Recv()
if err != nil {
panic(err)
}
log.Println(resp.GetMsg())
// resp.Msg == "world"
}
}
Expand Down
Loading

0 comments on commit da8ee87

Please sign in to comment.