• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..02-Mar-2021-

test/H02-Mar-2021-4,6123,611

thrift/H02-Mar-2021-16,73512,433

Makefile.amH A D02-Mar-20211.4 KiB4620

README.mdH A D02-Mar-20213.7 KiB11682

coding_standards.mdH A D02-Mar-202167 21

README.md

1Thrift Go Software Library
2
3License
4=======
5
6Licensed to the Apache Software Foundation (ASF) under one
7or more contributor license agreements. See the NOTICE file
8distributed with this work for additional information
9regarding copyright ownership. The ASF licenses this file
10to you under the Apache License, Version 2.0 (the
11"License"); you may not use this file except in compliance
12with the License. You may obtain a copy of the License at
13
14  http://www.apache.org/licenses/LICENSE-2.0
15
16Unless required by applicable law or agreed to in writing,
17software distributed under the License is distributed on an
18"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19KIND, either express or implied. See the License for the
20specific language governing permissions and limitations
21under the License.
22
23
24Using Thrift with Go
25====================
26
27Thrift supports Go 1.7+
28
29In following Go conventions, we recommend you use the 'go' tool to install
30Thrift for go.
31
32    $ go get github.com/apache/thrift/lib/go/thrift/...
33
34Will retrieve and install the most recent version of the package.
35
36
37A note about optional fields
38============================
39
40The thrift-to-Go compiler tries to represent thrift IDL structs as Go structs.
41We must be able to distinguish between optional fields that are set to their
42default value and optional values which are actually unset, so the generated
43code represents optional fields via pointers.
44
45This is generally intuitive and works well much of the time, but Go does not
46have a syntax for creating a pointer to a constant in a single expression. That
47is, given a struct like
48
49    struct SomeIDLType {
50    	OptionalField *int32
51    }
52
53, the following will not compile:
54
55    x := &SomeIDLType{
56    	OptionalField: &(3),
57    }
58
59(Nor is there any other syntax that's built in to the language)
60
61As such, we provide some helpers that do just this under lib/go/thrift/. E.g.,
62
63    x := &SomeIDLType{
64    	OptionalField: thrift.Int32Ptr(3),
65    }
66
67And so on. The code generator also creates analogous helpers for user-defined
68typedefs and enums.
69
70Adding custom tags to generated Thrift structs
71==============================================
72
73You can add tags to the auto-generated thrift structs using the following format:
74
75    struct foo {
76      1: required string Bar (go.tag = "some_tag:\"some_tag_value\"")
77    }
78
79which will generate:
80
81    type Foo struct {
82      Bar string `thrift:"bar,1,required" some_tag:"some_tag_value"`
83    }
84
85A note about server handler implementations
86===========================================
87
88The context object passed into the server handler function will be canceled when
89the client closes the connection (this is a best effort check, not a guarantee
90-- there's no guarantee that the context object is always canceled when client
91closes the connection, but when it's canceled you can always assume the client
92closed the connection). When implementing Go Thrift server, you can take
93advantage of that to abandon requests that's no longer needed:
94
95    func MyEndpoint(ctx context.Context, req *thriftRequestType) (*thriftResponseType, error) {
96        ...
97        if ctx.Err() == context.Canceled {
98            return nil, thrift.ErrAbandonRequest
99        }
100        ...
101    }
102
103This feature would add roughly 1 millisecond of latency overhead to the server
104handlers (along with roughly 2 goroutines per request).
105If that is unacceptable, it can be disabled by having this line early in your
106main function:
107
108    thrift.ServerConnectivityCheckInterval = 0
109
110Please be advised that due to a
111[Go runtime bug](https://github.com/golang/go/issues/27707), currently
112if this interval is set to a value too low (for example, 1ms), it might cause
113excessive cpu overhead.
114
115This feature is also only enabled on non-oneway endpoints.
116