github.com/m3db/m3@v1.5.0/src/dbnode/network/server/tchannelthrift/node/server.go (about)

     1  // Copyright (c) 2016 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package node
    22  
    23  import (
    24  	ns "github.com/m3db/m3/src/dbnode/network/server"
    25  	"github.com/m3db/m3/src/dbnode/network/server/tchannelthrift"
    26  	"github.com/m3db/m3/src/dbnode/network/server/tchannelthrift/node/channel"
    27  	"github.com/m3db/m3/src/x/context"
    28  
    29  	"github.com/uber/tchannel-go"
    30  )
    31  
    32  type server struct {
    33  	service     Service
    34  	address     string
    35  	contextPool context.Pool
    36  	opts        Options
    37  }
    38  
    39  // NewServer creates a new node TChannel Thrift network service
    40  func NewServer(
    41  	service Service,
    42  	address string,
    43  	contextPool context.Pool,
    44  	opts Options,
    45  ) ns.NetworkService {
    46  	return &server{
    47  		service:     service,
    48  		address:     address,
    49  		contextPool: contextPool,
    50  		opts:        opts,
    51  	}
    52  }
    53  
    54  func (s *server) ListenAndServe() (ns.Close, error) {
    55  	// NB(r): Keep ref to a local channel options since it's actually modified
    56  	// by TChannel itself to set defaults.
    57  	var opts *tchannel.ChannelOptions
    58  	if chanOpts := s.opts.ChannelOptions(); chanOpts != nil {
    59  		immutableOpts := *chanOpts
    60  		opts = &immutableOpts
    61  	}
    62  	channel, err := s.opts.TChanChannelFn()(s.service, channel.ChannelName, opts)
    63  	if err != nil {
    64  		return nil, err
    65  	}
    66  
    67  	iOpts := s.opts.InstrumentOptions()
    68  	server := s.opts.TChanNodeServerFn()(s.service, iOpts)
    69  	tchannelthrift.RegisterServer(channel, server, s.contextPool)
    70  	channel.ListenAndServe(s.address)
    71  
    72  	return channel.Close, nil
    73  }