Skip to content

Commit

Permalink
NewSimpleServer now accepts net.Listener
Browse files Browse the repository at this point in the history
Summary: NewSimpleServer now accepts net.Listener

Reviewed By: echistyakov

Differential Revision: D58582042

fbshipit-source-id: fcc6daf4b03752ddcfc55562df53e230d5850a83
  • Loading branch information
awalterschulze authored and facebook-github-bot committed Jun 17, 2024
1 parent c614d5c commit f77b502
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions thrift/lib/go/thrift/simple_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,22 @@ var ErrServerClosed = errors.New("thrift: Server closed")
// the request, processes it, and writes the response serially
type SimpleServer struct {
processorContext ProcessorContext
serverTransport ServerTransport
listener net.Listener
newProtocol func(net.Conn) (Protocol, error)
quit chan struct{}
log *log.Logger
*ServerOptions
}

// NewSimpleServer creates a new server that only supports Header Transport.
func NewSimpleServer(processor ProcessorContext, serverTransport ServerTransport, transportType TransportID, options ...func(*ServerOptions)) *SimpleServer {
func NewSimpleServer(processor ProcessorContext, listener net.Listener, transportType TransportID, options ...func(*ServerOptions)) *SimpleServer {
if transportType != TransportIDHeader {
panic(fmt.Sprintf("SimpleServer only supports Header Transport and not %d", transportType))
}
return &SimpleServer{
quit: make(chan struct{}, 1),
processorContext: processor,
serverTransport: serverTransport,
listener: listener,
newProtocol: NewHeaderProtocol,
log: log.New(os.Stderr, "", log.LstdFlags),
ServerOptions: simpleServerOptions(options...),
Expand All @@ -72,16 +72,13 @@ func simpleServerOptions(options ...func(*ServerOptions)) *ServerOptions {

// Listen returns the server transport listener
func (p *SimpleServer) Listen() (net.Addr, error) {
if err := p.serverTransport.Listen(); err != nil {
return nil, err
}
return p.serverTransport.Addr(), nil
return p.listener.Addr(), nil
}

// acceptLoopContext takes a context that will be decorated with ConnInfo and passed down to new clients.
func (p *SimpleServer) acceptLoopContext(ctx context.Context) error {
for {
client, err := p.serverTransport.Accept()
client, err := p.listener.Accept()
if err != nil {
select {
case <-p.quit:
Expand Down Expand Up @@ -137,7 +134,7 @@ func (p *SimpleServer) ServeContext(ctx context.Context) error {
// Stop stops the server
func (p *SimpleServer) Stop() error {
p.quit <- struct{}{}
p.serverTransport.Close()
p.listener.Close()
return nil
}

Expand Down

0 comments on commit f77b502

Please sign in to comment.