github.com/m3db/m3@v1.5.0/src/aggregator/server/http/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 http 22 23 import ( 24 "fmt" 25 "net" 26 "net/http" 27 "time" 28 29 "github.com/m3db/m3/src/aggregator/aggregator" 30 xdebug "github.com/m3db/m3/src/x/debug" 31 "github.com/m3db/m3/src/x/instrument" 32 xserver "github.com/m3db/m3/src/x/server" 33 ) 34 35 const ( 36 defaultCPUProfileDuration = 5 * time.Second 37 ) 38 39 // server is an http server. 40 type server struct { 41 opts Options 42 address string 43 listener net.Listener 44 aggregator aggregator.Aggregator 45 iOpts instrument.Options 46 } 47 48 // NewServer creates a new http server. 49 func NewServer( 50 address string, 51 aggregator aggregator.Aggregator, 52 opts Options, 53 iOpts instrument.Options, 54 ) xserver.Server { 55 return &server{ 56 opts: opts, 57 address: address, 58 aggregator: aggregator, 59 iOpts: iOpts, 60 } 61 } 62 63 func (s *server) ListenAndServe() error { 64 listener, err := net.Listen("tcp", s.address) 65 if err != nil { 66 return err 67 } 68 69 return s.Serve(listener) 70 } 71 72 func (s *server) Serve(l net.Listener) error { 73 registerHandlers(s.opts.Mux(), s.aggregator) 74 75 // create and register debug handler 76 debugWriter, err := xdebug.NewZipWriterWithDefaultSources( 77 defaultCPUProfileDuration, 78 s.iOpts, 79 ) 80 if err != nil { 81 return fmt.Errorf("unable to create debug writer: %v", err) 82 } 83 84 if err := debugWriter.RegisterHandler(xdebug.DebugURL, s.opts.Mux()); err != nil { 85 return fmt.Errorf("unable to register debug writer endpoint: %v", err) 86 } 87 88 server := http.Server{ 89 Handler: s.opts.Mux(), 90 ReadTimeout: s.opts.ReadTimeout(), 91 WriteTimeout: s.opts.WriteTimeout(), 92 } 93 94 s.listener = l 95 s.address = l.Addr().String() 96 97 go func() { 98 server.Serve(l) 99 }() 100 101 return nil 102 } 103 104 func (s *server) Close() { 105 if s.listener != nil { 106 s.listener.Close() 107 } 108 s.listener = nil 109 }