github.com/m3db/m3@v1.5.0/src/aggregator/server/http/options.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 "net/http" 25 "time" 26 ) 27 28 const ( 29 defaultReadTimeout = 10 * time.Second 30 defaultWriteTimeout = 10 * time.Second 31 ) 32 33 // Options is a set of server options. 34 type Options interface { 35 // SetReadTimeout sets the read timeout. 36 SetReadTimeout(value time.Duration) Options 37 38 // ReadTimeout returns the read timeout. 39 ReadTimeout() time.Duration 40 41 // SetWriteTimeout sets the write timeout. 42 SetWriteTimeout(value time.Duration) Options 43 44 // WriteTimeout returns the write timeout. 45 WriteTimeout() time.Duration 46 47 // Mux returns the http mux for the server. 48 Mux() *http.ServeMux 49 50 // SetMux sets the http mux for the server. 51 SetMux(value *http.ServeMux) Options 52 } 53 54 type options struct { 55 readTimeout time.Duration 56 writeTimeout time.Duration 57 mux *http.ServeMux 58 } 59 60 // NewOptions creates a new set of server options. 61 func NewOptions() Options { 62 return &options{ 63 readTimeout: defaultReadTimeout, 64 writeTimeout: defaultWriteTimeout, 65 mux: http.NewServeMux(), 66 } 67 } 68 69 func (o *options) SetReadTimeout(value time.Duration) Options { 70 opts := *o 71 opts.readTimeout = value 72 return &opts 73 } 74 75 func (o *options) ReadTimeout() time.Duration { 76 return o.readTimeout 77 } 78 79 func (o *options) SetWriteTimeout(value time.Duration) Options { 80 opts := *o 81 opts.writeTimeout = value 82 return &opts 83 } 84 85 func (o *options) WriteTimeout() time.Duration { 86 return o.writeTimeout 87 } 88 89 func (o *options) Mux() *http.ServeMux { 90 return o.mux 91 } 92 93 func (o *options) SetMux(value *http.ServeMux) Options { 94 opts := *o 95 opts.mux = value 96 return &opts 97 }