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 ( |
| 23 | "log" |
Jens Geyer | 91cfb99 | 2014-05-17 01:07:28 +0200 | [diff] [blame] | 24 | "runtime/debug" |
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 |
| 45 | } |
| 46 | |
| 47 | func NewTSimpleServer2(processor TProcessor, serverTransport TServerTransport) *TSimpleServer { |
| 48 | return NewTSimpleServerFactory2(NewTProcessorFactory(processor), serverTransport) |
| 49 | } |
| 50 | |
| 51 | func NewTSimpleServer4(processor TProcessor, serverTransport TServerTransport, transportFactory TTransportFactory, protocolFactory TProtocolFactory) *TSimpleServer { |
| 52 | return NewTSimpleServerFactory4(NewTProcessorFactory(processor), |
| 53 | serverTransport, |
| 54 | transportFactory, |
| 55 | protocolFactory, |
| 56 | ) |
| 57 | } |
| 58 | |
| 59 | func NewTSimpleServer6(processor TProcessor, serverTransport TServerTransport, inputTransportFactory TTransportFactory, outputTransportFactory TTransportFactory, inputProtocolFactory TProtocolFactory, outputProtocolFactory TProtocolFactory) *TSimpleServer { |
| 60 | return NewTSimpleServerFactory6(NewTProcessorFactory(processor), |
| 61 | serverTransport, |
| 62 | inputTransportFactory, |
| 63 | outputTransportFactory, |
| 64 | inputProtocolFactory, |
| 65 | outputProtocolFactory, |
| 66 | ) |
| 67 | } |
| 68 | |
| 69 | func NewTSimpleServerFactory2(processorFactory TProcessorFactory, serverTransport TServerTransport) *TSimpleServer { |
| 70 | return NewTSimpleServerFactory6(processorFactory, |
| 71 | serverTransport, |
| 72 | NewTTransportFactory(), |
| 73 | NewTTransportFactory(), |
| 74 | NewTBinaryProtocolFactoryDefault(), |
| 75 | NewTBinaryProtocolFactoryDefault(), |
| 76 | ) |
| 77 | } |
| 78 | |
| 79 | func NewTSimpleServerFactory4(processorFactory TProcessorFactory, serverTransport TServerTransport, transportFactory TTransportFactory, protocolFactory TProtocolFactory) *TSimpleServer { |
| 80 | return NewTSimpleServerFactory6(processorFactory, |
| 81 | serverTransport, |
| 82 | transportFactory, |
| 83 | transportFactory, |
| 84 | protocolFactory, |
| 85 | protocolFactory, |
| 86 | ) |
| 87 | } |
| 88 | |
| 89 | 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] | 90 | return &TSimpleServer{ |
| 91 | processorFactory: processorFactory, |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 92 | serverTransport: serverTransport, |
| 93 | inputTransportFactory: inputTransportFactory, |
| 94 | outputTransportFactory: outputTransportFactory, |
| 95 | inputProtocolFactory: inputProtocolFactory, |
| 96 | outputProtocolFactory: outputProtocolFactory, |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | func (p *TSimpleServer) ProcessorFactory() TProcessorFactory { |
| 101 | return p.processorFactory |
| 102 | } |
| 103 | |
| 104 | func (p *TSimpleServer) ServerTransport() TServerTransport { |
| 105 | return p.serverTransport |
| 106 | } |
| 107 | |
| 108 | func (p *TSimpleServer) InputTransportFactory() TTransportFactory { |
| 109 | return p.inputTransportFactory |
| 110 | } |
| 111 | |
| 112 | func (p *TSimpleServer) OutputTransportFactory() TTransportFactory { |
| 113 | return p.outputTransportFactory |
| 114 | } |
| 115 | |
| 116 | func (p *TSimpleServer) InputProtocolFactory() TProtocolFactory { |
| 117 | return p.inputProtocolFactory |
| 118 | } |
| 119 | |
| 120 | func (p *TSimpleServer) OutputProtocolFactory() TProtocolFactory { |
| 121 | return p.outputProtocolFactory |
| 122 | } |
| 123 | |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 124 | func (p *TSimpleServer) Listen() error { |
| 125 | return p.serverTransport.Listen() |
| 126 | } |
Jens Geyer | c975bbc | 2014-03-06 21:11:46 +0100 | [diff] [blame] | 127 | |
Matthew Pound | 8a83b04 | 2018-03-29 14:03:50 -0700 | [diff] [blame] | 128 | func (p *TSimpleServer) innerAccept() (int32, error) { |
| 129 | client, err := p.serverTransport.Accept() |
| 130 | p.mu.Lock() |
| 131 | defer p.mu.Unlock() |
| 132 | closed := atomic.LoadInt32(&p.closed) |
| 133 | if closed != 0 { |
| 134 | return closed, nil |
| 135 | } |
| 136 | if err != nil { |
| 137 | return 0, err |
| 138 | } |
| 139 | if client != nil { |
| 140 | p.wg.Add(1) |
| 141 | go func() { |
| 142 | defer p.wg.Done() |
| 143 | if err := p.processRequests(client); err != nil { |
| 144 | log.Println("error processing request:", err) |
| 145 | } |
| 146 | }() |
| 147 | } |
| 148 | return 0, nil |
| 149 | } |
| 150 | |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 151 | func (p *TSimpleServer) AcceptLoop() error { |
Jens Geyer | c975bbc | 2014-03-06 21:11:46 +0100 | [diff] [blame] | 152 | for { |
Matthew Pound | 8a83b04 | 2018-03-29 14:03:50 -0700 | [diff] [blame] | 153 | closed, err := p.innerAccept() |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 154 | if err != nil { |
Jens Geyer | 57cd421 | 2014-12-08 21:25:00 +0100 | [diff] [blame] | 155 | return err |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 156 | } |
Matthew Pound | 8a83b04 | 2018-03-29 14:03:50 -0700 | [diff] [blame] | 157 | if closed != 0 { |
| 158 | return nil |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 159 | } |
| 160 | } |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | func (p *TSimpleServer) Serve() error { |
| 164 | err := p.Listen() |
| 165 | if err != nil { |
| 166 | return err |
| 167 | } |
| 168 | p.AcceptLoop() |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 169 | return nil |
| 170 | } |
| 171 | |
| 172 | func (p *TSimpleServer) Stop() error { |
Zachary Wasserman | c179435 | 2017-06-29 17:15:01 -0700 | [diff] [blame] | 173 | p.mu.Lock() |
| 174 | defer p.mu.Unlock() |
| 175 | if atomic.LoadInt32(&p.closed) != 0 { |
| 176 | return nil |
ZhiyuYin | 47f9b9d | 2016-06-16 17:28:42 +0800 | [diff] [blame] | 177 | } |
Zachary Wasserman | c179435 | 2017-06-29 17:15:01 -0700 | [diff] [blame] | 178 | atomic.StoreInt32(&p.closed, 1) |
| 179 | p.serverTransport.Interrupt() |
| 180 | p.wg.Wait() |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 181 | return nil |
| 182 | } |
| 183 | |
Jens Geyer | 91cfb99 | 2014-05-17 01:07:28 +0200 | [diff] [blame] | 184 | func (p *TSimpleServer) processRequests(client TTransport) error { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 185 | processor := p.processorFactory.GetProcessor(client) |
D. Can Celasun | 8da0e72 | 2017-06-02 14:33:32 +0200 | [diff] [blame] | 186 | inputTransport, err := p.inputTransportFactory.GetTransport(client) |
| 187 | if err != nil { |
| 188 | return err |
| 189 | } |
| 190 | outputTransport, err := p.outputTransportFactory.GetTransport(client) |
| 191 | if err != nil { |
| 192 | return err |
| 193 | } |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 194 | inputProtocol := p.inputProtocolFactory.GetProtocol(inputTransport) |
| 195 | outputProtocol := p.outputProtocolFactory.GetProtocol(outputTransport) |
Jens Geyer | 91cfb99 | 2014-05-17 01:07:28 +0200 | [diff] [blame] | 196 | defer func() { |
| 197 | if e := recover(); e != nil { |
| 198 | log.Printf("panic in processor: %s: %s", e, debug.Stack()) |
| 199 | } |
| 200 | }() |
libinbin | a576896 | 2017-05-18 14:18:28 +0800 | [diff] [blame] | 201 | |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 202 | if inputTransport != nil { |
| 203 | defer inputTransport.Close() |
| 204 | } |
| 205 | if outputTransport != nil { |
| 206 | defer outputTransport.Close() |
| 207 | } |
| 208 | for { |
Zachary Wasserman | c179435 | 2017-06-29 17:15:01 -0700 | [diff] [blame] | 209 | if atomic.LoadInt32(&p.closed) != 0 { |
libinbin | a576896 | 2017-05-18 14:18:28 +0800 | [diff] [blame] | 210 | return nil |
libinbin | a576896 | 2017-05-18 14:18:28 +0800 | [diff] [blame] | 211 | } |
| 212 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 213 | ok, err := processor.Process(defaultCtx, inputProtocol, outputProtocol) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 214 | if err, ok := err.(TTransportException); ok && err.TypeId() == END_OF_FILE { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 215 | return nil |
| 216 | } else if err != nil { |
| 217 | return err |
| 218 | } |
zhangxin | 54f49f8 | 2016-09-19 12:17:20 +0800 | [diff] [blame] | 219 | if err, ok := err.(TApplicationException); ok && err.TypeId() == UNKNOWN_METHOD { |
| 220 | continue |
| 221 | } |
libinbin | a576896 | 2017-05-18 14:18:28 +0800 | [diff] [blame] | 222 | if !ok { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 223 | break |
| 224 | } |
| 225 | } |
| 226 | return nil |
| 227 | } |