blob: bca43706f216bc281c3061f12d720800c441ddc3 [file] [log] [blame] [view]
Jens Geyer0853ab62013-12-17 21:38:44 +01001Thrift Go Software Library
Jens Geyer0e87c462013-06-18 22:25:07 +02002
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
Jens Geyer0853ab62013-12-17 21:38:44 +010023
Jens Geyer0e87c462013-06-18 22:25:07 +020024Using Thrift with Go
25====================
26
Yuxuan 'fishy' Wang68c02722021-07-26 08:59:01 -070027Thrift supports the currently officially supported Go releases (the latest 2).
John Boiles57852792018-01-05 14:37:05 -080028
Yuxuan 'fishy' Wang68c02722021-07-26 08:59:01 -070029After initializing the go modules file in your project, use the following
30command to add the most recent version of the package:
Jens Geyer0e87c462013-06-18 22:25:07 +020031
Yuxuan 'fishy' Wang68c02722021-07-26 08:59:01 -070032 $ go get github.com/apache/thrift
Jens Geyer0853ab62013-12-17 21:38:44 +010033
34
35A note about optional fields
36============================
37
38The thrift-to-Go compiler tries to represent thrift IDL structs as Go structs.
39We must be able to distinguish between optional fields that are set to their
40default value and optional values which are actually unset, so the generated
41code represents optional fields via pointers.
42
43This is generally intuitive and works well much of the time, but Go does not
44have a syntax for creating a pointer to a constant in a single expression. That
45is, given a struct like
46
47 struct SomeIDLType {
48 OptionalField *int32
49 }
50
51, the following will not compile:
52
53 x := &SomeIDLType{
54 OptionalField: &(3),
55 }
56
57(Nor is there any other syntax that's built in to the language)
58
59As such, we provide some helpers that do just this under lib/go/thrift/. E.g.,
60
61 x := &SomeIDLType{
62 OptionalField: thrift.Int32Ptr(3),
63 }
64
65And so on. The code generator also creates analogous helpers for user-defined
66typedefs and enums.
Richard Artoulc3a3f652016-07-22 14:26:53 -070067
68Adding custom tags to generated Thrift structs
69==============================================
70
71You can add tags to the auto-generated thrift structs using the following format:
72
73 struct foo {
74 1: required string Bar (go.tag = "some_tag:\"some_tag_value\"")
75 }
76
77which will generate:
78
79 type Foo struct {
80 Bar string `thrift:"bar,1,required" some_tag:"some_tag_value"`
81 }
Yuxuan 'fishy' Wang4db7a0a2020-06-27 10:13:34 -070082
83A note about server handler implementations
84===========================================
85
86The context object passed into the server handler function will be canceled when
87the client closes the connection (this is a best effort check, not a guarantee
88-- there's no guarantee that the context object is always canceled when client
89closes the connection, but when it's canceled you can always assume the client
90closed the connection). When implementing Go Thrift server, you can take
91advantage of that to abandon requests that's no longer needed:
92
93 func MyEndpoint(ctx context.Context, req *thriftRequestType) (*thriftResponseType, error) {
94 ...
95 if ctx.Err() == context.Canceled {
96 return nil, thrift.ErrAbandonRequest
97 }
98 ...
99 }
100
101This feature would add roughly 1 millisecond of latency overhead to the server
102handlers (along with roughly 2 goroutines per request).
103If that is unacceptable, it can be disabled by having this line early in your
104main function:
105
106 thrift.ServerConnectivityCheckInterval = 0
107
Yuxuan 'fishy' Wangdaf62092020-10-07 16:28:38 -0700108Please be advised that due to a
109[Go runtime bug](https://github.com/golang/go/issues/27707), currently
110if this interval is set to a value too low (for example, 1ms), it might cause
111excessive cpu overhead.
112
Yuxuan 'fishy' Wang4db7a0a2020-06-27 10:13:34 -0700113This feature is also only enabled on non-oneway endpoints.