github.com/m3db/m3@v1.5.0/src/dbnode/network/server/httpjson/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  	"net"
    25  	"net/http"
    26  
    27  	"github.com/m3db/m3/src/dbnode/client"
    28  	ns "github.com/m3db/m3/src/dbnode/network/server"
    29  	"github.com/m3db/m3/src/dbnode/network/server/httpjson"
    30  	ttcluster "github.com/m3db/m3/src/dbnode/network/server/tchannelthrift/cluster"
    31  	"github.com/m3db/m3/src/x/context"
    32  	xresource "github.com/m3db/m3/src/x/resource"
    33  )
    34  
    35  type server struct {
    36  	client  client.Client
    37  	address string
    38  	opts    httpjson.ServerOptions
    39  }
    40  
    41  // NewServer creates a cluster HTTP network service.
    42  func NewServer(
    43  	client client.Client,
    44  	address string,
    45  	contextPool context.Pool,
    46  	opts httpjson.ServerOptions,
    47  ) ns.NetworkService {
    48  	if opts == nil {
    49  		opts = httpjson.NewServerOptions()
    50  	}
    51  	opts = opts.
    52  		SetContextFn(httpjson.NewDefaultContextFn(contextPool)).
    53  		SetPostResponseFn(httpjson.DefaulPostResponseFn)
    54  	return &server{
    55  		client:  client,
    56  		address: address,
    57  		opts:    opts,
    58  	}
    59  }
    60  
    61  func (s *server) ListenAndServe() (ns.Close, error) {
    62  	service := ttcluster.NewService(s.client)
    63  
    64  	mux := http.NewServeMux()
    65  	if err := httpjson.RegisterHandlers(mux, service, s.opts); err != nil {
    66  		return nil, err
    67  	}
    68  
    69  	listener, err := net.Listen("tcp", s.address)
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  
    74  	server := http.Server{
    75  		Handler:      mux,
    76  		ReadTimeout:  s.opts.ReadTimeout(),
    77  		WriteTimeout: s.opts.WriteTimeout(),
    78  	}
    79  
    80  	go func() {
    81  		server.Serve(listener)
    82  	}()
    83  
    84  	return func() {
    85  		listener.Close()
    86  		xresource.TryClose(service) // nolint: errcheck
    87  	}, nil
    88  }