vitess.io/vitess@v0.16.2/go/vt/binlog/grpcbinlogstreamer/streamer.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  // Package grpcbinlogstreamer contains the gRPC implementation of the binlog
    18  // streamer server component.
    19  package grpcbinlogstreamer
    20  
    21  import (
    22  	"vitess.io/vitess/go/vt/binlog"
    23  	"vitess.io/vitess/go/vt/servenv"
    24  
    25  	binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata"
    26  	binlogservicepb "vitess.io/vitess/go/vt/proto/binlogservice"
    27  )
    28  
    29  // UpdateStream is the gRPC UpdateStream server
    30  type UpdateStream struct {
    31  	binlogservicepb.UnimplementedUpdateStreamServer
    32  	updateStream binlog.UpdateStream
    33  }
    34  
    35  // New returns a new go rpc server implementation stub for UpdateStream
    36  func New(updateStream binlog.UpdateStream) *UpdateStream {
    37  	return &UpdateStream{updateStream: updateStream}
    38  }
    39  
    40  // StreamKeyRange is part of the binlogservicepb.UpdateStreamServer interface
    41  func (server *UpdateStream) StreamKeyRange(req *binlogdatapb.StreamKeyRangeRequest, stream binlogservicepb.UpdateStream_StreamKeyRangeServer) (err error) {
    42  	defer server.updateStream.HandlePanic(&err)
    43  	return server.updateStream.StreamKeyRange(stream.Context(), req.Position, req.KeyRange, req.Charset, func(reply *binlogdatapb.BinlogTransaction) error {
    44  		return stream.Send(&binlogdatapb.StreamKeyRangeResponse{
    45  			BinlogTransaction: reply,
    46  		})
    47  	})
    48  }
    49  
    50  // StreamTables is part of the binlogservicepb.UpdateStreamServer interface
    51  func (server *UpdateStream) StreamTables(req *binlogdatapb.StreamTablesRequest, stream binlogservicepb.UpdateStream_StreamTablesServer) (err error) {
    52  	defer server.updateStream.HandlePanic(&err)
    53  	return server.updateStream.StreamTables(stream.Context(), req.Position, req.Tables, req.Charset, func(reply *binlogdatapb.BinlogTransaction) error {
    54  		return stream.Send(&binlogdatapb.StreamTablesResponse{
    55  			BinlogTransaction: reply,
    56  		})
    57  	})
    58  }
    59  
    60  // registration mechanism
    61  
    62  func init() {
    63  	binlog.RegisterUpdateStreamServices = append(binlog.RegisterUpdateStreamServices, func(updateStream binlog.UpdateStream) {
    64  		if servenv.GRPCCheckServiceMap("updatestream") {
    65  			binlogservicepb.RegisterUpdateStreamServer(servenv.GRPCServer, New(updateStream))
    66  		}
    67  	})
    68  }