internal
packageThis package is not in the latest version of its module.
Details
Validgo.mod file
The Go module system was introduced in Go 1.11 and is the official dependency management solution for Go.
Redistributable license
Redistributable licenses place minimal restrictions on how software can be used, modified, and redistributed.
Tagged version
Modules with tagged versions give importers more predictable builds.
Stable version
When a project reaches major version v1 it is considered stable.
- Learn more about best practices
Repository
Links
Documentation¶
Index¶
Examples¶
Constants¶
This section is empty.
Variables¶
This section is empty.
Functions¶
This section is empty.
Types¶
typeReadHandler¶
type ReadHandler interface {// GetReader provides an io.ReaderAt, which will not be retained by the Server after the pb.ReadRequest.GetReader(ctxcontext.Context, namestring) (io.ReaderAt,error)// Close does not have to do anything, but is here for if the io.ReaderAt wants to call Close().Close(ctxcontext.Context, namestring)error}ReadHandler reads from the Bytestream.Note: error returns must return an instance of grpc.rpcError unless otherwise handled in grpc-go/rpc_util.go.http://google.golang.org/grpc provides Errorf(code, fmt, ...) to create instances of grpc.rpcError.Note: Cancelling the context will abort the stream ("drop the connection"). Consider returning a non-nil error instead.
typeServer¶
type Server struct {// AllowOverwrite controls Server behavior when a WriteRequest with finish_write = true is followed by another WriteRequest.AllowOverwritebool// Bytestream allows a WriteRequest to omit the resource name, in which case it will be appended to the last WriteRequest.LastWrittenResourcestring// contains filtered or unexported fields}Server wraps the RPCs in pb. Use bytestream.NewServer() to create a Server.
funcNewServer¶
func NewServer(gsrv *grpc.Server, readHandlerReadHandler, writeHandlerWriteHandler) (*Server,error)
NewServer creates a new bytestream.Server using gRPC.gsrv is the *grpc.Server this bytestream.Server will listen on.readHandler handles any incoming pb.ReadRequest or nil which means all pb.ReadRequests will be rejected.writeHandler handles any incoming pb.WriteRequest or nil which means all pb.WriteRequests will be rejected.readHandler and writeHandler cannot both be nil.
Example¶
package mainimport ("bytes""context""io""log""google.golang.org/grpc""google.golang.org/grpc/codes")type ExampleReadHandler struct {buf []bytename string // In this example, the service can handle one name only.}func (mr *ExampleReadHandler) GetReader(ctx context.Context, name string) (io.ReaderAt, error) {if mr.name == "" {mr.name = namelog.Printf("read from name: %q", name)} else if mr.name != name {return nil, grpc.Errorf(codes.NotFound, "reader has name %q, name %q not allowed", mr.name, name)}return bytes.NewReader(mr.buf), nil}// Close can be a no-op.func (mr *ExampleReadHandler) Close(ctx context.Context, name string) error {return nil}type ExampleWriteHandler struct {buf bytes.Buffer // bytes.Buffer implements io.Writername string // In this example, the service can handle one name only.}// Handle writes to a given name.func (mw *ExampleWriteHandler) GetWriter(ctx context.Context, name string, initOffset int64) (io.Writer, error) {if mw.name == "" {mw.name = namelog.Printf("write to name: %q", name)} else if mw.name != name {return nil, grpc.Errorf(codes.NotFound, "reader has name %q, name=%q not allowed", mw.name, name)}// TODO: initOffset is ignored.return &mw.buf, nil}// Close can be a no-op.func (mw *ExampleWriteHandler) Close(ctx context.Context, name string) error {return nil}func main() {reader := &ExampleReadHandler{buf: []byte("Hello World!"),name: "foo",}writer := &ExampleWriteHandler{}gsrv := grpc.NewServer()bytestreamServer, err := NewServer(gsrv, reader, writer)if err != nil {log.Printf("NewServer: %v", err)return}// Start accepting incoming connections.// See gRPC docs and newGRPCServer in google.golang.org/api/transport/bytestream/client_test.go._ = bytestreamServer}typeWriteHandler¶
type WriteHandler interface {// GetWriter provides an io.Writer that is ready to write at initOffset.// The io.Writer will not be retained by the Server after the pb.WriteRequest.GetWriter(ctxcontext.Context, namestring, initOffsetint64) (io.Writer,error)// Close does not have to do anything, but is related to Server.AllowOverwrite. Or if the io.Writer simply wants a Close() call.// Close is called when the server receives a pb.WriteRequest with finish_write = true.// If Server.AllowOverwrite == true then Close() followed by GetWriter() for the same name indicates the name is being overwritten, even if the initOffset is different.Close(ctxcontext.Context, namestring)error}WriteHandler handles writes from the Bytestream. For example:Note: error returns must return an instance of grpc.rpcError unless otherwise handled in grpc-go/rpc_util.go.grpc-go/rpc_util.go provides the helper func Errorf(code, fmt, ...) to create instances of grpc.rpcError.Note: Cancelling the context will abort the stream ("drop the connection"). Consider returning a non-nil error instead.