Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
| 20 | package thrift |
| 21 | |
| 22 | import ( |
Yuxuan 'fishy' Wang | e4870a3 | 2019-10-24 13:23:30 -0700 | [diff] [blame] | 23 | "fmt" |
Yuxuan 'fishy' Wang | c03e2aa | 2019-10-23 13:43:09 -0700 | [diff] [blame] | 24 | "io" |
Paul | 2df9c20 | 2016-09-24 22:47:58 +0300 | [diff] [blame] | 25 | "sync" |
Zachary Wasserman | c179435 | 2017-06-29 17:15:01 -0700 | [diff] [blame] | 26 | "sync/atomic" |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 27 | ) |
| 28 | |
James E. King, III | 847fae9 | 2017-03-22 15:17:30 -0400 | [diff] [blame] | 29 | /* |
| 30 | * This is not a typical TSimpleServer as it is not blocked after accept a socket. |
| 31 | * It is more like a TThreadedServer that can handle different connections in different goroutines. |
| 32 | * This will work if golang user implements a conn-pool like thing in client side. |
| 33 | */ |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 34 | type TSimpleServer struct { |
Zachary Wasserman | c179435 | 2017-06-29 17:15:01 -0700 | [diff] [blame] | 35 | closed int32 |
| 36 | wg sync.WaitGroup |
| 37 | mu sync.Mutex |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 38 | |
| 39 | processorFactory TProcessorFactory |
| 40 | serverTransport TServerTransport |
| 41 | inputTransportFactory TTransportFactory |
| 42 | outputTransportFactory TTransportFactory |
| 43 | inputProtocolFactory TProtocolFactory |
| 44 | outputProtocolFactory TProtocolFactory |
Yuxuan 'fishy' Wang | 26ef904 | 2019-08-19 00:18:22 -0700 | [diff] [blame] | 45 | |
| 46 | // Headers to auto forward in THeaderProtocol |
| 47 | forwardHeaders []string |
Yuxuan 'fishy' Wang | e4870a3 | 2019-10-24 13:23:30 -0700 | [diff] [blame] | 48 | |
| 49 | logger Logger |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | func NewTSimpleServer2(processor TProcessor, serverTransport TServerTransport) *TSimpleServer { |
| 53 | return NewTSimpleServerFactory2(NewTProcessorFactory(processor), serverTransport) |
| 54 | } |
| 55 | |
| 56 | func NewTSimpleServer4(processor TProcessor, serverTransport TServerTransport, transportFactory TTransportFactory, protocolFactory TProtocolFactory) *TSimpleServer { |
| 57 | return NewTSimpleServerFactory4(NewTProcessorFactory(processor), |
| 58 | serverTransport, |
| 59 | transportFactory, |
| 60 | protocolFactory, |
| 61 | ) |
| 62 | } |
| 63 | |
| 64 | func NewTSimpleServer6(processor TProcessor, serverTransport TServerTransport, inputTransportFactory TTransportFactory, outputTransportFactory TTransportFactory, inputProtocolFactory TProtocolFactory, outputProtocolFactory TProtocolFactory) *TSimpleServer { |
| 65 | return NewTSimpleServerFactory6(NewTProcessorFactory(processor), |
| 66 | serverTransport, |
| 67 | inputTransportFactory, |
| 68 | outputTransportFactory, |
| 69 | inputProtocolFactory, |
| 70 | outputProtocolFactory, |
| 71 | ) |
| 72 | } |
| 73 | |
| 74 | func NewTSimpleServerFactory2(processorFactory TProcessorFactory, serverTransport TServerTransport) *TSimpleServer { |
| 75 | return NewTSimpleServerFactory6(processorFactory, |
| 76 | serverTransport, |
| 77 | NewTTransportFactory(), |
| 78 | NewTTransportFactory(), |
| 79 | NewTBinaryProtocolFactoryDefault(), |
| 80 | NewTBinaryProtocolFactoryDefault(), |
| 81 | ) |
| 82 | } |
| 83 | |
| 84 | func NewTSimpleServerFactory4(processorFactory TProcessorFactory, serverTransport TServerTransport, transportFactory TTransportFactory, protocolFactory TProtocolFactory) *TSimpleServer { |
| 85 | return NewTSimpleServerFactory6(processorFactory, |
| 86 | serverTransport, |
| 87 | transportFactory, |
| 88 | transportFactory, |
| 89 | protocolFactory, |
| 90 | protocolFactory, |
| 91 | ) |
| 92 | } |
| 93 | |
| 94 | func NewTSimpleServerFactory6(processorFactory TProcessorFactory, serverTransport TServerTransport, inputTransportFactory TTransportFactory, outputTransportFactory TTransportFactory, inputProtocolFactory TProtocolFactory, outputProtocolFactory TProtocolFactory) *TSimpleServer { |
Jens Geyer | c975bbc | 2014-03-06 21:11:46 +0100 | [diff] [blame] | 95 | return &TSimpleServer{ |
| 96 | processorFactory: processorFactory, |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 97 | serverTransport: serverTransport, |
| 98 | inputTransportFactory: inputTransportFactory, |
| 99 | outputTransportFactory: outputTransportFactory, |
| 100 | inputProtocolFactory: inputProtocolFactory, |
| 101 | outputProtocolFactory: outputProtocolFactory, |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | func (p *TSimpleServer) ProcessorFactory() TProcessorFactory { |
| 106 | return p.processorFactory |
| 107 | } |
| 108 | |
| 109 | func (p *TSimpleServer) ServerTransport() TServerTransport { |
| 110 | return p.serverTransport |
| 111 | } |
| 112 | |
| 113 | func (p *TSimpleServer) InputTransportFactory() TTransportFactory { |
| 114 | return p.inputTransportFactory |
| 115 | } |
| 116 | |
| 117 | func (p *TSimpleServer) OutputTransportFactory() TTransportFactory { |
| 118 | return p.outputTransportFactory |
| 119 | } |
| 120 | |
| 121 | func (p *TSimpleServer) InputProtocolFactory() TProtocolFactory { |
| 122 | return p.inputProtocolFactory |
| 123 | } |
| 124 | |
| 125 | func (p *TSimpleServer) OutputProtocolFactory() TProtocolFactory { |
| 126 | return p.outputProtocolFactory |
| 127 | } |
| 128 | |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 129 | func (p *TSimpleServer) Listen() error { |
| 130 | return p.serverTransport.Listen() |
| 131 | } |
Jens Geyer | c975bbc | 2014-03-06 21:11:46 +0100 | [diff] [blame] | 132 | |
Yuxuan 'fishy' Wang | 26ef904 | 2019-08-19 00:18:22 -0700 | [diff] [blame] | 133 | // SetForwardHeaders sets the list of header keys that will be auto forwarded |
| 134 | // while using THeaderProtocol. |
| 135 | // |
| 136 | // "forward" means that when the server is also a client to other upstream |
| 137 | // thrift servers, the context object user gets in the processor functions will |
| 138 | // have both read and write headers set, with write headers being forwarded. |
| 139 | // Users can always override the write headers by calling SetWriteHeaderList |
| 140 | // before calling thrift client functions. |
| 141 | func (p *TSimpleServer) SetForwardHeaders(headers []string) { |
| 142 | size := len(headers) |
| 143 | if size == 0 { |
| 144 | p.forwardHeaders = nil |
| 145 | return |
| 146 | } |
| 147 | |
| 148 | keys := make([]string, size) |
| 149 | copy(keys, headers) |
| 150 | p.forwardHeaders = keys |
| 151 | } |
| 152 | |
Yuxuan 'fishy' Wang | e4870a3 | 2019-10-24 13:23:30 -0700 | [diff] [blame] | 153 | // SetLogger sets the logger used by this TSimpleServer. |
| 154 | // |
| 155 | // If no logger was set before Serve is called, a default logger using standard |
| 156 | // log library will be used. |
| 157 | func (p *TSimpleServer) SetLogger(logger Logger) { |
| 158 | p.logger = logger |
| 159 | } |
| 160 | |
Matthew Pound | 8a83b04 | 2018-03-29 14:03:50 -0700 | [diff] [blame] | 161 | func (p *TSimpleServer) innerAccept() (int32, error) { |
| 162 | client, err := p.serverTransport.Accept() |
| 163 | p.mu.Lock() |
| 164 | defer p.mu.Unlock() |
| 165 | closed := atomic.LoadInt32(&p.closed) |
| 166 | if closed != 0 { |
| 167 | return closed, nil |
| 168 | } |
| 169 | if err != nil { |
| 170 | return 0, err |
| 171 | } |
| 172 | if client != nil { |
| 173 | p.wg.Add(1) |
| 174 | go func() { |
| 175 | defer p.wg.Done() |
| 176 | if err := p.processRequests(client); err != nil { |
Yuxuan 'fishy' Wang | e4870a3 | 2019-10-24 13:23:30 -0700 | [diff] [blame] | 177 | p.logger(fmt.Sprintf("error processing request: %v", err)) |
Matthew Pound | 8a83b04 | 2018-03-29 14:03:50 -0700 | [diff] [blame] | 178 | } |
| 179 | }() |
| 180 | } |
| 181 | return 0, nil |
| 182 | } |
| 183 | |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 184 | func (p *TSimpleServer) AcceptLoop() error { |
Jens Geyer | c975bbc | 2014-03-06 21:11:46 +0100 | [diff] [blame] | 185 | for { |
Matthew Pound | 8a83b04 | 2018-03-29 14:03:50 -0700 | [diff] [blame] | 186 | closed, err := p.innerAccept() |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 187 | if err != nil { |
Jens Geyer | 57cd421 | 2014-12-08 21:25:00 +0100 | [diff] [blame] | 188 | return err |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 189 | } |
Matthew Pound | 8a83b04 | 2018-03-29 14:03:50 -0700 | [diff] [blame] | 190 | if closed != 0 { |
| 191 | return nil |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 192 | } |
| 193 | } |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | func (p *TSimpleServer) Serve() error { |
Yuxuan 'fishy' Wang | e4870a3 | 2019-10-24 13:23:30 -0700 | [diff] [blame] | 197 | p.logger = fallbackLogger(p.logger) |
| 198 | |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 199 | err := p.Listen() |
| 200 | if err != nil { |
| 201 | return err |
| 202 | } |
| 203 | p.AcceptLoop() |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 204 | return nil |
| 205 | } |
| 206 | |
| 207 | func (p *TSimpleServer) Stop() error { |
Zachary Wasserman | c179435 | 2017-06-29 17:15:01 -0700 | [diff] [blame] | 208 | p.mu.Lock() |
| 209 | defer p.mu.Unlock() |
| 210 | if atomic.LoadInt32(&p.closed) != 0 { |
| 211 | return nil |
ZhiyuYin | 47f9b9d | 2016-06-16 17:28:42 +0800 | [diff] [blame] | 212 | } |
Zachary Wasserman | c179435 | 2017-06-29 17:15:01 -0700 | [diff] [blame] | 213 | atomic.StoreInt32(&p.closed, 1) |
| 214 | p.serverTransport.Interrupt() |
| 215 | p.wg.Wait() |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 216 | return nil |
| 217 | } |
| 218 | |
Jens Geyer | 91cfb99 | 2014-05-17 01:07:28 +0200 | [diff] [blame] | 219 | func (p *TSimpleServer) processRequests(client TTransport) error { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 220 | processor := p.processorFactory.GetProcessor(client) |
D. Can Celasun | 8da0e72 | 2017-06-02 14:33:32 +0200 | [diff] [blame] | 221 | inputTransport, err := p.inputTransportFactory.GetTransport(client) |
| 222 | if err != nil { |
| 223 | return err |
| 224 | } |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 225 | inputProtocol := p.inputProtocolFactory.GetProtocol(inputTransport) |
Yuxuan 'fishy' Wang | 4d46c11 | 2019-06-07 20:47:18 +0800 | [diff] [blame] | 226 | var outputTransport TTransport |
| 227 | var outputProtocol TProtocol |
| 228 | |
| 229 | // for THeaderProtocol, we must use the same protocol instance for |
| 230 | // input and output so that the response is in the same dialect that |
| 231 | // the server detected the request was in. |
Yuxuan 'fishy' Wang | b1002a7 | 2019-08-05 13:03:02 -0700 | [diff] [blame] | 232 | headerProtocol, ok := inputProtocol.(*THeaderProtocol) |
| 233 | if ok { |
Yuxuan 'fishy' Wang | 4d46c11 | 2019-06-07 20:47:18 +0800 | [diff] [blame] | 234 | outputProtocol = inputProtocol |
| 235 | } else { |
| 236 | oTrans, err := p.outputTransportFactory.GetTransport(client) |
| 237 | if err != nil { |
| 238 | return err |
| 239 | } |
| 240 | outputTransport = oTrans |
| 241 | outputProtocol = p.outputProtocolFactory.GetProtocol(outputTransport) |
| 242 | } |
| 243 | |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 244 | if inputTransport != nil { |
| 245 | defer inputTransport.Close() |
| 246 | } |
| 247 | if outputTransport != nil { |
| 248 | defer outputTransport.Close() |
| 249 | } |
| 250 | for { |
Zachary Wasserman | c179435 | 2017-06-29 17:15:01 -0700 | [diff] [blame] | 251 | if atomic.LoadInt32(&p.closed) != 0 { |
libinbin | a576896 | 2017-05-18 14:18:28 +0800 | [diff] [blame] | 252 | return nil |
libinbin | a576896 | 2017-05-18 14:18:28 +0800 | [diff] [blame] | 253 | } |
| 254 | |
Yuxuan 'fishy' Wang | b1002a7 | 2019-08-05 13:03:02 -0700 | [diff] [blame] | 255 | ctx := defaultCtx |
| 256 | if headerProtocol != nil { |
| 257 | // We need to call ReadFrame here, otherwise we won't |
| 258 | // get any headers on the AddReadTHeaderToContext call. |
| 259 | // |
| 260 | // ReadFrame is safe to be called multiple times so it |
| 261 | // won't break when it's called again later when we |
| 262 | // actually start to read the message. |
| 263 | if err := headerProtocol.ReadFrame(); err != nil { |
Yuxuan 'fishy' Wang | c03e2aa | 2019-10-23 13:43:09 -0700 | [diff] [blame] | 264 | if err == io.EOF { |
| 265 | return nil |
| 266 | } |
Yuxuan 'fishy' Wang | b1002a7 | 2019-08-05 13:03:02 -0700 | [diff] [blame] | 267 | return err |
| 268 | } |
Yuxuan 'fishy' Wang | c03e2aa | 2019-10-23 13:43:09 -0700 | [diff] [blame] | 269 | ctx = AddReadTHeaderToContext(ctx, headerProtocol.GetReadHeaders()) |
Yuxuan 'fishy' Wang | 26ef904 | 2019-08-19 00:18:22 -0700 | [diff] [blame] | 270 | ctx = SetWriteHeaderList(ctx, p.forwardHeaders) |
Yuxuan 'fishy' Wang | b1002a7 | 2019-08-05 13:03:02 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | ok, err := processor.Process(ctx, inputProtocol, outputProtocol) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 274 | if err, ok := err.(TTransportException); ok && err.TypeId() == END_OF_FILE { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 275 | return nil |
| 276 | } else if err != nil { |
| 277 | return err |
| 278 | } |
zhangxin | 54f49f8 | 2016-09-19 12:17:20 +0800 | [diff] [blame] | 279 | if err, ok := err.(TApplicationException); ok && err.TypeId() == UNKNOWN_METHOD { |
| 280 | continue |
| 281 | } |
libinbin | a576896 | 2017-05-18 14:18:28 +0800 | [diff] [blame] | 282 | if !ok { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 283 | break |
| 284 | } |
| 285 | } |
| 286 | return nil |
| 287 | } |