blob: 0aa4f1bc6f2cb78ecb94d137f110f56b63fc3f76 [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
Yuxuan 'fishy' Wanga2652362021-08-04 09:07:53 -070024Suppored 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
Jens Geyer0e87c462013-06-18 22:25:07 +020045Using Thrift with Go
46====================
47
Yuxuan 'fishy' Wang68c02722021-07-26 08:59:01 -070048Thrift supports the currently officially supported Go releases (the latest 2).
John Boiles57852792018-01-05 14:37:05 -080049
Yuxuan 'fishy' Wang68c02722021-07-26 08:59:01 -070050After initializing the go modules file in your project, use the following
51command to add the most recent version of the package:
Jens Geyer0e87c462013-06-18 22:25:07 +020052
Yuxuan 'fishy' Wang68c02722021-07-26 08:59:01 -070053 $ go get github.com/apache/thrift
Jens Geyer0853ab62013-12-17 21:38:44 +010054
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.
Richard Artoulc3a3f652016-07-22 14:26:53 -070088
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 }
Yuxuan 'fishy' Wang4db7a0a2020-06-27 10:13:34 -0700103
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
Yuxuan 'fishy' Wang0e872c82023-08-09 15:06:37 -0700111closed the connection). The cause of the cancellation (via `context.Cause(ctx)`)
112would also be set to `thrift.ErrAbandonRequest`.
113
114When implementing Go Thrift server, you can take advantage of that to abandon
115requests that's no longer needed by returning `thrift.ErrAbandonRequest`:
Yuxuan 'fishy' Wang4db7a0a2020-06-27 10:13:34 -0700116
117 func MyEndpoint(ctx context.Context, req *thriftRequestType) (*thriftResponseType, error) {
118 ...
119 if ctx.Err() == context.Canceled {
120 return nil, thrift.ErrAbandonRequest
Yuxuan 'fishy' Wang0e872c82023-08-09 15:06:37 -0700121 // Or just return ctx.Err(), compiler generated processor code will
122 // handle it for you automatically:
123 // return nil, ctx.Err()
Yuxuan 'fishy' Wang4db7a0a2020-06-27 10:13:34 -0700124 }
125 ...
126 }
127
128This feature would add roughly 1 millisecond of latency overhead to the server
129handlers (along with roughly 2 goroutines per request).
130If that is unacceptable, it can be disabled by having this line early in your
131main function:
132
133 thrift.ServerConnectivityCheckInterval = 0
134
Yuxuan 'fishy' Wangdaf62092020-10-07 16:28:38 -0700135Please be advised that due to a
136[Go runtime bug](https://github.com/golang/go/issues/27707), currently
137if this interval is set to a value too low (for example, 1ms), it might cause
138excessive cpu overhead.
139
Yuxuan 'fishy' Wang4db7a0a2020-06-27 10:13:34 -0700140This feature is also only enabled on non-oneway endpoints.
郑桐2fa907e2022-01-04 18:20:24 +0800141
142A note about server stop implementations
143========================================
144
145[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
146the last received request to be handled, as the time spent by Stop
147 may sometimes be too long:
148* When socket timeout is not set, server might be hanged before all active
149 clients to finish handling the last received request.
150* When the socket timeout is too long (e.g one hour), server will
151 hang for that duration before all active clients to finish handling the
152 last received request.
153
154To prevent Stop from hanging for too long, you can set
155thrift.ServerStopTimeout in your main or init function:
156
157 thrift.ServerStopTimeout = <max_duration_to_stop>
158
159If it's set to <=0, the feature will be disabled (by default), and server
160will wait for all the client connections to be closed gracefully with
161zero err time. Otherwise, the stop will wait for all the client
162connections to be closed gracefully util thrift.ServerStopTimeout is
163reached, and client connections that are not closed after thrift.ServerStopTimeout
Yuxuan 'fishy' Wang0e872c82023-08-09 15:06:37 -0700164will be closed abruptly which may cause some client errors.