| Jens Geyer | 0853ab6 | 2013-12-17 21:38:44 +0100 | [diff] [blame] | 1 | Thrift Go Software Library |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 2 | |
| 3 | License |
| 4 | ======= |
| 5 | |
| 6 | Licensed to the Apache Software Foundation (ASF) under one |
| 7 | or more contributor license agreements. See the NOTICE file |
| 8 | distributed with this work for additional information |
| 9 | regarding copyright ownership. The ASF licenses this file |
| 10 | to you under the Apache License, Version 2.0 (the |
| 11 | "License"); you may not use this file except in compliance |
| 12 | with the License. You may obtain a copy of the License at |
| 13 | |
| 14 | http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | |
| 16 | Unless required by applicable law or agreed to in writing, |
| 17 | software distributed under the License is distributed on an |
| 18 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 19 | KIND, either express or implied. See the License for the |
| 20 | specific language governing permissions and limitations |
| 21 | under the License. |
| 22 | |
| Jens Geyer | 0853ab6 | 2013-12-17 21:38:44 +0100 | [diff] [blame] | 23 | |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 24 | Using Thrift with Go |
| 25 | ==================== |
| 26 | |
| John Boiles | 5785279 | 2018-01-05 14:37:05 -0800 | [diff] [blame] | 27 | Thrift supports Go 1.7+ |
| 28 | |
| Jens Geyer | b7cb945 | 2014-05-17 00:38:06 +0200 | [diff] [blame] | 29 | In following Go conventions, we recommend you use the 'go' tool to install |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 30 | Thrift for go. |
| 31 | |
| D. Can Celasun | 9ee2951 | 2018-10-17 08:44:48 +0200 | [diff] [blame] | 32 | $ go get github.com/apache/thrift/lib/go/thrift/... |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 33 | |
| Jens Geyer | b7cb945 | 2014-05-17 00:38:06 +0200 | [diff] [blame] | 34 | Will retrieve and install the most recent version of the package. |
| Jens Geyer | 0853ab6 | 2013-12-17 21:38:44 +0100 | [diff] [blame] | 35 | |
| 36 | |
| 37 | A note about optional fields |
| 38 | ============================ |
| 39 | |
| 40 | The thrift-to-Go compiler tries to represent thrift IDL structs as Go structs. |
| 41 | We must be able to distinguish between optional fields that are set to their |
| 42 | default value and optional values which are actually unset, so the generated |
| 43 | code represents optional fields via pointers. |
| 44 | |
| 45 | This is generally intuitive and works well much of the time, but Go does not |
| 46 | have a syntax for creating a pointer to a constant in a single expression. That |
| 47 | is, 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 | |
| 61 | As 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 | |
| 67 | And so on. The code generator also creates analogous helpers for user-defined |
| 68 | typedefs and enums. |
| Richard Artoul | c3a3f65 | 2016-07-22 14:26:53 -0700 | [diff] [blame] | 69 | |
| 70 | Adding custom tags to generated Thrift structs |
| 71 | ============================================== |
| 72 | |
| 73 | You 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 | |
| 79 | which will generate: |
| 80 | |
| 81 | type Foo struct { |
| 82 | Bar string `thrift:"bar,1,required" some_tag:"some_tag_value"` |
| 83 | } |
| Yuxuan 'fishy' Wang | 4db7a0a | 2020-06-27 10:13:34 -0700 | [diff] [blame] | 84 | |
| 85 | A note about server handler implementations |
| 86 | =========================================== |
| 87 | |
| 88 | The context object passed into the server handler function will be canceled when |
| 89 | the 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 |
| 91 | closes the connection, but when it's canceled you can always assume the client |
| 92 | closed the connection). When implementing Go Thrift server, you can take |
| 93 | advantage 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 | |
| 103 | This feature would add roughly 1 millisecond of latency overhead to the server |
| 104 | handlers (along with roughly 2 goroutines per request). |
| 105 | If that is unacceptable, it can be disabled by having this line early in your |
| 106 | main function: |
| 107 | |
| 108 | thrift.ServerConnectivityCheckInterval = 0 |
| 109 | |
| Yuxuan 'fishy' Wang | daf6209 | 2020-10-07 16:28:38 -0700 | [diff] [blame] | 110 | Please be advised that due to a |
| 111 | [Go runtime bug](https://github.com/golang/go/issues/27707), currently |
| 112 | if this interval is set to a value too low (for example, 1ms), it might cause |
| 113 | excessive cpu overhead. |
| 114 | |
| Yuxuan 'fishy' Wang | 4db7a0a | 2020-06-27 10:13:34 -0700 | [diff] [blame] | 115 | This feature is also only enabled on non-oneway endpoints. |