github.com/m3db/m3@v1.5.0/src/dbnode/network/server/tchannelthrift/cluster/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 cluster 22 23 import ( 24 "github.com/m3db/m3/src/dbnode/client" 25 "github.com/m3db/m3/src/dbnode/generated/thrift/rpc" 26 ns "github.com/m3db/m3/src/dbnode/network/server" 27 "github.com/m3db/m3/src/dbnode/network/server/tchannelthrift" 28 "github.com/m3db/m3/src/x/context" 29 xresource "github.com/m3db/m3/src/x/resource" 30 31 "github.com/uber/tchannel-go" 32 ) 33 34 const ( 35 // ChannelName is the TChannel channel name the cluster service is exposed on 36 ChannelName = "Cluster" 37 ) 38 39 type server struct { 40 client client.Client 41 address string 42 contextPool context.Pool 43 opts *tchannel.ChannelOptions 44 } 45 46 // NewServer creates a new cluster TChannel Thrift network service 47 func NewServer( 48 client client.Client, 49 address string, 50 contextPool context.Pool, 51 opts *tchannel.ChannelOptions, 52 ) ns.NetworkService { 53 return &server{ 54 address: address, 55 client: client, 56 contextPool: contextPool, 57 opts: opts, 58 } 59 } 60 61 func (s *server) ListenAndServe() (ns.Close, error) { 62 // NB(r): Keep ref to a local channel options since it's actually modified 63 // by TChannel itself to set defaults. 64 var opts *tchannel.ChannelOptions 65 if s.opts != nil { 66 immutableOpts := *s.opts 67 opts = &immutableOpts 68 } 69 channel, err := tchannel.NewChannel(ChannelName, opts) 70 if err != nil { 71 return nil, err 72 } 73 74 service := NewService(s.client) 75 tchannelthrift.RegisterServer(channel, rpc.NewTChanClusterServer(service), s.contextPool) 76 77 channel.ListenAndServe(s.address) 78 79 return func() { 80 channel.Close() 81 xresource.TryClose(service) // nolint: errcheck 82 }, nil 83 }