blob: 96e1af2813b327cf15fdf203afff051ab17ccb7a [file] [log] [blame]
<!DOCTYPE html>
<html xmlns="" xml:lang="en" lang="en">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<meta content="en-us" http-equiv="Content-Language" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="/static/images/favicon.ico" rel="shortcut icon" />
<link href="/static/css/style.css" rel="stylesheet" type="text/css" />
<link href="/static/css/codehilite.css" rel="stylesheet" type="text/css" />
<link href="/static/css/bootstrap.css" media="screen, projection" rel="stylesheet" type="text/css" />
<link href="/static/css/thrift.css" media="screen, projection" rel="stylesheet" type="text/css" />
<script src="/static/js/jquery.min.js"></script>
<script src="/static/js/bootstrap-dropdown.js"></script>
<script src="/static/js/bootstrap-tab.js"></script>
<script src="/static/js/thrift.js"></script>
<title>Apache Thrift - Go library</title>
<div class="navbar">
<div class="navbar-inner">
<div class="container">
<a class="brand" href="/">Apache Thrift &trade;</a>
<div class="nav-collapse">
<ul class="nav pull-right">
<li><a href="/download">Download</a></li>
<li><a href="/docs">Documentation</a></li>
<li><a href="/developers">Developers</a></li>
<li><a href="/lib">Libraries</a></li>
<li><a href="/tutorial">Tutorial</a></li>
<li><a href="/test">Test Suite</a></li>
<li><a href="/about">About</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Apache <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="" target="_blank">Apache Home</a></li>
<li><a href="" target="_blank">Apache License v2.0</a></li>
<li><a href="" target="_blank">Donate</a></li>
<li><a href="" target="_blank">Thanks</a></li>
<li><a href="" target="_blank">Security</a></li>
<div class="container">
<h1 id="suppored-go-releases">Suppored Go releases</h1>
<p>Following the
<a href="">official Go release policy</a>,
we support the latest two Go releases at the time of the Thrift release.</p>
<p>For example, at the time of Thrift v0.14.0 release,
the latest two Go releases are go1.15 and go1.14,
and those are the two Go releases supported by Thrift v0.14.*
(including v0.14.1 and v0.14.2 patch releases).</p>
<p>Because of Go&#8217;s backward compatibility guarantee,
older Thrift libraries usually works with newer Go releases
(e.g. Thrift v0.14.* works with go1.16, although it&#8217;s not officially supported),
but newer Thrift releases might use new APIs introduced in Go releases and no
longer work with older Go releases.
For example, Thrift v0.14.0 used APIs introduced in go1.13,
and as a result no longer works on go1.12.</p>
<h1 id="using-thrift-with-go">Using Thrift with Go</h1>
<p>Thrift supports the currently officially supported Go releases (the latest 2).</p>
<p>After initializing the go modules file in your project, use the following
command to add the most recent version of the package:</p>
<pre><code>$ go get
<h1 id="a-note-about-optional-fields">A note about optional fields</h1>
<p>The thrift-to-Go compiler tries to represent thrift IDL structs as Go structs.
We must be able to distinguish between optional fields that are set to their
default value and optional values which are actually unset, so the generated
code represents optional fields via pointers.</p>
<p>This is generally intuitive and works well much of the time, but Go does not
have a syntax for creating a pointer to a constant in a single expression. That
is, given a struct like</p>
<pre><code>struct SomeIDLType {
OptionalField *int32
<p>, the following will not compile:</p>
<pre><code>x := &amp;SomeIDLType{
OptionalField: &amp;(3),
<p>(Nor is there any other syntax that&#8217;s built in to the language)</p>
<p>As such, we provide some helpers that do just this under lib/go/thrift/. E.g.,</p>
<pre><code>x := &amp;SomeIDLType{
OptionalField: thrift.Int32Ptr(3),
<p>And so on. The code generator also creates analogous helpers for user-defined
typedefs and enums.</p>
<h1 id="adding-custom-tags-to-generated-thrift-structs">Adding custom tags to generated Thrift structs</h1>
<p>You can add tags to the auto-generated thrift structs using the following format:</p>
<pre><code>struct foo {
1: required string Bar (go.tag = "some_tag:\"some_tag_value\"")
<p>which will generate:</p>
<pre><code>type Foo struct {
Bar string `thrift:"bar,1,required" some_tag:"some_tag_value"`
<h1 id="a-note-about-server-handler-implementations">A note about server handler implementations</h1>
<p>The context object passed into the server handler function will be canceled when
the client closes the connection (this is a best effort check, not a guarantee
&#8211; there&#8217;s no guarantee that the context object is always canceled when client
closes the connection, but when it&#8217;s canceled you can always assume the client
closed the connection). When implementing Go Thrift server, you can take
advantage of that to abandon requests that&#8217;s no longer needed:</p>
<pre><code>func MyEndpoint(ctx context.Context, req *thriftRequestType) (*thriftResponseType, error) {
if ctx.Err() == context.Canceled {
return nil, thrift.ErrAbandonRequest
<p>This feature would add roughly 1 millisecond of latency overhead to the server
handlers (along with roughly 2 goroutines per request).
If that is unacceptable, it can be disabled by having this line early in your
main function:</p>
<pre><code>thrift.ServerConnectivityCheckInterval = 0
<p>Please be advised that due to a
<a href="">Go runtime bug</a>, currently
if this interval is set to a value too low (for example, 1ms), it might cause
excessive cpu overhead.</p>
<p>This feature is also only enabled on non-oneway endpoints.</p>
<h1 id="a-note-about-server-stop-implementations">A note about server stop implementations</h1>
<p><a href="">TSimpleServer.Stop</a> will wait for all client connections to be closed after
the last received request to be handled, as the time spent by Stop
may sometimes be too long:
* When socket timeout is not set, server might be hanged before all active
clients to finish handling the last received request.
* When the socket timeout is too long (e.g one hour), server will
hang for that duration before all active clients to finish handling the
last received request.</p>
<p>To prevent Stop from hanging for too long, you can set
thrift.ServerStopTimeout in your main or init function:</p>
<pre><code>thrift.ServerStopTimeout = &lt;max_duration_to_stop&gt;
<p>If it&#8217;s set to &lt;=0, the feature will be disabled (by default), and server
will wait for all the client connections to be closed gracefully with
zero err time. Otherwise, the stop will wait for all the client
connections to be closed gracefully util thrift.ServerStopTimeout is
reached, and client connections that are not closed after thrift.ServerStopTimeout
will be closed abruptly which may cause some client errors.</p>
<p class="snippet_footer">This page was generated by Apache Thrift's <strong>source tree docs</strong>:
<a href=";a=blob;hb=HEAD;f=lib/go/">lib/go/</a>
<div class="container">
<footer class="footer">
<div class="row">
<div class="span3">
<ul class="unstyled">
<li><a href="/download">Download</a></li>
<li><a href="/developers">Developers</a></li>
<li><a href="/tutorial">Tutorials</a></li>
<ul class="unstyled">
<li><a href="/sitemap">Sitemap</a></li>
<div class="span3">
<h3>Get Involved</h3>
<ul class="unstyled">
<li><a href="/mailing">Mailing Lists</a></li>
<li><a href="">Issue Tracking</a></li>
<li><a href="/docs/HowToContribute">How To Contribute</a></li>
<div class="span6">
<a href=""><img src="/static/images/feather.svg" onerror="this.src='/static/images/feather.png';this.onerror=null;" /></a>
Copyright &copy; 2023 <a href="">Apache Software Foundation</a>.
Licensed under the <a href="">Apache License v2.0</a>.
Apache, Apache Thrift, and the Apache feather logo are trademarks of The Apache Software Foundation.