63 Star 183 Fork 3

Gitee 极速下载/hyperledger-fabric

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/hyperledger/fabric
Clone or Download
service.go 2.52 KB
Copy Edit Raw Blame History
/*
Copyright IBM Corp. 2017 All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package cluster
import (
"context"
"io"
"github.com/hyperledger/fabric/common/util"
"github.com/hyperledger/fabric/protos/orderer"
"github.com/op/go-logging"
"google.golang.org/grpc"
)
//go:generate mockery -dir . -name Dispatcher -case underscore -output ./mocks/
// Dispatcher dispatches requests
type Dispatcher interface {
DispatchSubmit(ctx context.Context, request *orderer.SubmitRequest) (*orderer.SubmitResponse, error)
DispatchStep(ctx context.Context, request *orderer.StepRequest) (*orderer.StepResponse, error)
}
//go:generate mockery -dir . -name SubmitStream -case underscore -output ./mocks/
// SubmitStream defines the gRPC stream for sending
// transactions, and receiving corresponding responses
type SubmitStream interface {
Send(response *orderer.SubmitResponse) error
Recv() (*orderer.SubmitRequest, error)
grpc.ServerStream
}
// Service defines the raft Service
type Service struct {
Dispatcher Dispatcher
Logger logging.Logger
}
// Step forwards a message to a raft FSM located in this server
func (s *Service) Step(ctx context.Context, request *orderer.StepRequest) (*orderer.StepResponse, error) {
addr := util.ExtractRemoteAddress(ctx)
s.Logger.Debugf("Connection from %s", addr)
defer s.Logger.Debugf("Closing connection from %s", addr)
response, err := s.Dispatcher.DispatchStep(ctx, request)
if err != nil {
s.Logger.Warningf("Handling of Step() from %s failed: %+v", addr, err)
}
return response, err
}
// Submit accepts transactions
func (s *Service) Submit(stream orderer.Cluster_SubmitServer) error {
addr := util.ExtractRemoteAddress(stream.Context())
s.Logger.Debugf("Connection from %s", addr)
defer s.Logger.Debugf("Closing connection from %s", addr)
for {
err := s.handleSubmit(stream, addr)
if err == io.EOF {
s.Logger.Debugf("%s disconnected", addr)
return nil
}
if err != nil {
return err
}
// Else, no error occurred, so we continue to the next iteration
}
}
func (s *Service) handleSubmit(stream SubmitStream, addr string) error {
request, err := stream.Recv()
if err == io.EOF {
return err
}
if err != nil {
s.Logger.Warningf("Stream read from %s failed: %v", addr, err)
return err
}
response, err := s.Dispatcher.DispatchSubmit(stream.Context(), request)
if err != nil {
s.Logger.Warningf("Handling of Propose() from %s failed: %+v", addr, err)
return err
}
err = stream.Send(response)
if err != nil {
s.Logger.Warningf("Send() failed: %v", err)
}
return err
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/mirrors/hyperledger-fabric.git
git@gitee.com:mirrors/hyperledger-fabric.git
mirrors
hyperledger-fabric
hyperledger-fabric
v1.3.0-rc1

Search

0d507c66 1850385 C8b1a773 1850385