Skip to content

Commit

Permalink
Update doc
Browse files Browse the repository at this point in the history
  • Loading branch information
johningve authored and meling committed Jul 10, 2020
1 parent a4b5977 commit a490738
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
4 changes: 2 additions & 2 deletions doc/ordering.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type Server interface {
// Runs synchronously, but may spawn goroutines and return early.
// This allows the server to start processing the next request.
// Goroutines can then asynchronously send response back using the func.
RPC(context.Context, *Request, func(*Response))
RPC(context.Context, *Request, func(*Response, error))
}
```

Expand Down Expand Up @@ -97,7 +97,7 @@ func (s *testSrv) AsyncHandler(_ context.Context, req *Request, ret func(*Respon
time.Sleep(10 * time.Millisecond)
// at some point later, the response passed back to Gorums through the `ret` function,
// and gets sent back to the client.
ret(response)
ret(response, nil)
}()
}
```
Expand Down
19 changes: 10 additions & 9 deletions doc/userguide.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ And this is our server interface:

```go
type Storage interface {
Read(context.Context, *ReadRequest, func(*State))
Write(context.Context, *State, func(*WriteResponse))
Read(context.Context, *ReadRequest, func(*State, error))
Write(context.Context, *State, func(*WriteResponse, error))
}
```

Expand All @@ -138,23 +138,23 @@ type storageSrv struct {
state *State
}

func (srv *storageSrv) Read(_ context.Context, req *ReadRequest, ret func(*State)) {
func (srv *storageSrv) Read(_ context.Context, req *ReadRequest, ret func(*State, error)) {
srv.mut.Lock()
defer srv.mut.Unlock()
fmt.Println("Got Read()")
ret(srv.state)
ret(srv.state, nil)
}

func (srv *storageSrv) Write(_ context.Context, req *State, ret func(*WriteResponse)) {
func (srv *storageSrv) Write(_ context.Context, req *State, ret func(*WriteResponse, error)) {
srv.mut.Lock()
defer srv.mut.Unlock()
if srv.state.Timestamp < req.Timestamp {
srv.state = req
fmt.Println("Got Write(", req.Value, ")")
ret(&WriteResponse{New: true})
ret(&WriteResponse{New: true}, nil)
return
}
ret(&WriteResponse{New: false})
ret(&WriteResponse{New: false}, nil)
}
```

Expand All @@ -168,14 +168,15 @@ There are some important things to note about implementing the server interfaces
* The context that is passed to the handlers is the gRPC stream context of the underlying gRPC stream.
You can use this context to retrieve [metadata](https://github.com/grpc/grpc-go/blob/master/Documentation/grpc-metadata.md)
and [peer](https://godoc.org/google.golang.org/grpc/peer) information from the client.
* You can return errors using the [`status` package](https://pkg.go.dev/google.golang.org/grpc/status?tab=doc).

```go
func (srv *storageSrv) Read(_ context.Context, req *ReadRequest, ret func(*State)) {
func (srv *storageSrv) Read(_ context.Context, req *ReadRequest, ret func(*State), error) {
go func() {
srv.mut.Lock()
defer srv.mut.Unlock()
fmt.Println("Got Read()")
ret(srv.state)
ret(srv.state, nil)
}()
}
```
Expand Down

0 comments on commit a490738

Please sign in to comment.