THRIFT-4203 thrift server stop gracefully
Client: Go
Patch: libinbin <libinbin@17paipai.cn>

This closes #1271
diff --git a/lib/go/thrift/simple_server.go b/lib/go/thrift/simple_server.go
index e207bd9..5c848f2 100644
--- a/lib/go/thrift/simple_server.go
+++ b/lib/go/thrift/simple_server.go
@@ -39,6 +39,7 @@
 	outputTransportFactory TTransportFactory
 	inputProtocolFactory   TProtocolFactory
 	outputProtocolFactory  TProtocolFactory
+	sync.WaitGroup
 }
 
 func NewTSimpleServer2(processor TProcessor, serverTransport TServerTransport) *TSimpleServer {
@@ -135,6 +136,7 @@
 			return err
 		}
 		if client != nil {
+			p.Add(1)
 			go func() {
 				if err := p.processRequests(client); err != nil {
 					log.Println("error processing request:", err)
@@ -157,14 +159,17 @@
 
 func (p *TSimpleServer) Stop() error {
 	q := func() {
-		p.quit <- struct{}{}
+		close(p.quit)
 		p.serverTransport.Interrupt()
+		p.Wait()
 	}
 	once.Do(q)
 	return nil
 }
 
 func (p *TSimpleServer) processRequests(client TTransport) error {
+	defer p.Done()
+
 	processor := p.processorFactory.GetProcessor(client)
 	inputTransport := p.inputTransportFactory.GetTransport(client)
 	outputTransport := p.outputTransportFactory.GetTransport(client)
@@ -175,6 +180,7 @@
 			log.Printf("panic in processor: %s: %s", e, debug.Stack())
 		}
 	}()
+
 	if inputTransport != nil {
 		defer inputTransport.Close()
 	}
@@ -182,6 +188,12 @@
 		defer outputTransport.Close()
 	}
 	for {
+		select {
+		case <-p.quit:
+			return nil
+		default:
+		}
+
 		ok, err := processor.Process(inputProtocol, outputProtocol)
 		if err, ok := err.(TTransportException); ok && err.TypeId() == END_OF_FILE {
 			return nil
@@ -191,7 +203,7 @@
 		if err, ok := err.(TApplicationException); ok && err.TypeId() == UNKNOWN_METHOD {
 			continue
 		}
- 		if !ok {
+		if !ok {
 			break
 		}
 	}