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
24Suppored Go releases
25====================
26
27Following the
28[official Go release policy](https://golang.org/doc/devel/release#policy),
29we support the latest two Go releases at the time of the Thrift release.
30
31For example, at the time of Thrift v0.14.0 release,
32the latest two Go releases are go1.15 and go1.14,
33and those are the two Go releases supported by Thrift v0.14.*
34(including v0.14.1 and v0.14.2 patch releases).
35
36Because of Go's backward compatibility guarantee,
37older Thrift libraries usually works with newer Go releases
38(e.g. Thrift v0.14.* works with go1.16, although it's not officially supported),
39but newer Thrift releases might use new APIs introduced in Go releases and no
40longer work with older Go releases.
41For example, Thrift v0.14.0 used APIs introduced in go1.13,
42and as a result no longer works on go1.12.
43
44
45Using Thrift with Go
46====================
47
48Thrift supports the currently officially supported Go releases (the latest 2).
49
50After initializing the go modules file in your project, use the following
51command to add the most recent version of the package:
52
53 $ go get github.com/apache/thrift
54
55
56A note about optional fields
57============================
58
59The thrift-to-Go compiler tries to represent thrift IDL structs as Go structs.
60We must be able to distinguish between optional fields that are set to their
61default value and optional values which are actually unset, so the generated
62code represents optional fields via pointers.
63
64This is generally intuitive and works well much of the time, but Go does not
65have a syntax for creating a pointer to a constant in a single expression. That
66is, given a struct like
67
68 struct SomeIDLType {
69 OptionalField *int32
70 }
71
72, the following will not compile:
73
74 x := &SomeIDLType{
75 OptionalField: &(3),
76 }
77
78(Nor is there any other syntax that's built in to the language)
79
80As such, we provide some helpers that do just this under lib/go/thrift/. E.g.,
81
82 x := &SomeIDLType{
83 OptionalField: thrift.Int32Ptr(3),
84 }
85
86And so on. The code generator also creates analogous helpers for user-defined
87typedefs and enums.
88
89Adding custom tags to generated Thrift structs
90==============================================
91
92You can add tags to the auto-generated thrift structs using the following format:
93
94 struct foo {
95 1: required string Bar (go.tag = "some_tag:\"some_tag_value\"")
96 }
97
98which will generate:
99
100 type Foo struct {
101 Bar string `thrift:"bar,1,required" some_tag:"some_tag_value"`
102 }
103
104A note about server handler implementations
105===========================================
106
107The context object passed into the server handler function will be canceled when
108the client closes the connection (this is a best effort check, not a guarantee
109-- there's no guarantee that the context object is always canceled when client
110closes the connection, but when it's canceled you can always assume the client
111closed the connection). When implementing Go Thrift server, you can take
112advantage of that to abandon requests that's no longer needed:
113
114 func MyEndpoint(ctx context.Context, req *thriftRequestType) (*thriftResponseType, error) {
115 ...
116 if ctx.Err() == context.Canceled {
117 return nil, thrift.ErrAbandonRequest
118 }
119 ...
120 }
121
122This feature would add roughly 1 millisecond of latency overhead to the server
123handlers (along with roughly 2 goroutines per request).
124If that is unacceptable, it can be disabled by having this line early in your
125main function:
126
127 thrift.ServerConnectivityCheckInterval = 0
128
129Please be advised that due to a
130[Go runtime bug](https://github.com/golang/go/issues/27707), currently
131if this interval is set to a value too low (for example, 1ms), it might cause
132excessive cpu overhead.
133
134This feature is also only enabled on non-oneway endpoints.
135
136A note about server stop implementations
137========================================
138
139[TSimpleServer.Stop](https://pkg.go.dev/github.com/apache/thrift/lib/go/thrift#TSimpleServer.Stop) will wait for all client connections to be closed after
140the last received request to be handled, as the time spent by Stop
141 may sometimes be too long:
142* When socket timeout is not set, server might be hanged before all active
143 clients to finish handling the last received request.
144* When the socket timeout is too long (e.g one hour), server will
145 hang for that duration before all active clients to finish handling the
146 last received request.
147
148To prevent Stop from hanging for too long, you can set
149thrift.ServerStopTimeout in your main or init function:
150
151 thrift.ServerStopTimeout = <max_duration_to_stop>
152
153If it's set to <=0, the feature will be disabled (by default), and server
154will wait for all the client connections to be closed gracefully with
155zero err time. Otherwise, the stop will wait for all the client
156connections to be closed gracefully util thrift.ServerStopTimeout is
157reached, and client connections that are not closed after thrift.ServerStopTimeout
158will be closed abruptly which may cause some client errors.