Skip to content

Commit c463038

Browse files
committed
grpc-go initial commit
0 parents  commit c463038

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+8099
-0
lines changed

LICENSE

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Copyright 2014, Google Inc.
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are
6+
met:
7+
8+
* Redistributions of source code must retain the above copyright
9+
notice, this list of conditions and the following disclaimer.
10+
* Redistributions in binary form must reproduce the above
11+
copyright notice, this list of conditions and the following disclaimer
12+
in the documentation and/or other materials provided with the
13+
distribution.
14+
* Neither the name of Google Inc. nor the names of its
15+
contributors may be used to endorse or promote products derived from
16+
this software without specific prior written permission.
17+
18+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
gRPC-Go: a Go implementation of gRPC, Google's RPC library
2+
3+
To install this package, you need to install Go 1.4 and setup your Go workspace on your computer. The simplest way to install the library is to run:
4+
5+
go get github.com/google/grpc-go/rpc

rpc/call.go

+165
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/*
2+
*
3+
* Copyright 2014, Google Inc.
4+
* All rights reserved.
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions are
8+
* met:
9+
*
10+
* * Redistributions of source code must retain the above copyright
11+
* notice, this list of conditions and the following disclaimer.
12+
* * Redistributions in binary form must reproduce the above
13+
* copyright notice, this list of conditions and the following disclaimer
14+
* in the documentation and/or other materials provided with the
15+
* distribution.
16+
* * Neither the name of Google Inc. nor the names of its
17+
* contributors may be used to endorse or promote products derived from
18+
* this software without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
*
32+
*/
33+
34+
package rpc
35+
36+
import (
37+
"io"
38+
39+
"github.com/golang/protobuf/proto"
40+
"github.com/grpc/grpc-go/rpc/codes"
41+
"github.com/grpc/grpc-go/rpc/metadata"
42+
"github.com/grpc/grpc-go/rpc/transport"
43+
"golang.org/x/net/context"
44+
)
45+
46+
// recv receives and parses an RPC response.
47+
// On error, it returns the error and indicates whether the call should be retried.
48+
//
49+
// TODO(zhaoq): Check whether the received message sequence is valid.
50+
func recv(stream *transport.Stream, reply proto.Message) error {
51+
p := &parser{s: stream}
52+
for {
53+
if err := recvProto(p, reply); err != nil {
54+
if err == io.EOF {
55+
return nil
56+
}
57+
return err
58+
}
59+
}
60+
}
61+
62+
// sendRPC writes out various information of an RPC such as Context and Message.
63+
func sendRPC(ctx context.Context, callHdr *transport.CallHdr, t transport.ClientTransport, args proto.Message, opts *transport.Options) (_ *transport.Stream, err error) {
64+
stream, err := t.NewStream(ctx, callHdr)
65+
if err != nil {
66+
return nil, err
67+
}
68+
defer func() {
69+
if err != nil {
70+
if _, ok := err.(transport.ConnectionError); !ok {
71+
t.CloseStream(stream, err)
72+
}
73+
}
74+
}()
75+
// TODO(zhaoq): Support compression.
76+
outBuf, err := encode(args, compressionNone)
77+
if err != nil {
78+
return nil, transport.StreamErrorf(codes.Internal, "%v", err)
79+
}
80+
err = t.Write(stream, outBuf, opts)
81+
if err != nil {
82+
return nil, err
83+
}
84+
// Sent successfully.
85+
return stream, nil
86+
}
87+
88+
// callInfo contains all related configuration and information about an RPC.
89+
type callInfo struct {
90+
failFast bool
91+
headerMD metadata.MD
92+
trailerMD metadata.MD
93+
}
94+
95+
// Invoke is called by the generated code. It sends the RPC request on the
96+
// wire and returns after response is received.
97+
func Invoke(ctx context.Context, method string, args, reply proto.Message, cc *ClientConn, opts ...CallOption) error {
98+
var c callInfo
99+
for _, o := range opts {
100+
if err := o.before(&c); err != nil {
101+
return toRPCErr(err)
102+
}
103+
}
104+
defer func() {
105+
for _, o := range opts {
106+
o.after(&c)
107+
}
108+
}()
109+
callHdr := &transport.CallHdr{
110+
Host: cc.target,
111+
Method: method,
112+
}
113+
topts := &transport.Options{
114+
Last: true,
115+
Delay: false,
116+
}
117+
ts := 0
118+
var lastErr error // record the error that happened
119+
for {
120+
var (
121+
err error
122+
t transport.ClientTransport
123+
stream *transport.Stream
124+
)
125+
// TODO(zhaoq): Need a formal spec of retry strategy for non-failfast rpcs.
126+
if lastErr != nil && c.failFast {
127+
return lastErr
128+
}
129+
t, ts, err = cc.wait(ctx, ts)
130+
if err != nil {
131+
if lastErr != nil {
132+
// This was a retry; return the error from the last attempt.
133+
return lastErr
134+
}
135+
return err
136+
}
137+
stream, err = sendRPC(ctx, callHdr, t, args, topts)
138+
if err != nil {
139+
if _, ok := err.(transport.ConnectionError); ok {
140+
lastErr = err
141+
continue
142+
}
143+
if lastErr != nil {
144+
return toRPCErr(lastErr)
145+
}
146+
return toRPCErr(err)
147+
}
148+
// Try to acquire header metadata from the server if there is any.
149+
c.headerMD, err = stream.Header()
150+
if err != nil {
151+
return toRPCErr(err)
152+
}
153+
// Receive the response
154+
lastErr = recv(stream, reply)
155+
if _, ok := lastErr.(transport.ConnectionError); ok {
156+
continue
157+
}
158+
c.trailerMD = stream.Trailer()
159+
t.CloseStream(stream, lastErr)
160+
if lastErr != nil {
161+
return toRPCErr(lastErr)
162+
}
163+
return Errorf(stream.StatusCode(), stream.StatusDesc())
164+
}
165+
}

0 commit comments

Comments
 (0)