github.com/m3db/m3@v1.5.0/src/dbnode/network/server/httpjson/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 httpjson 22 23 import ( 24 "time" 25 26 apachethrift "github.com/apache/thrift/lib/go/thrift" 27 "github.com/uber/tchannel-go/thrift" 28 "golang.org/x/net/context" 29 ) 30 31 const ( 32 defaultReadTimeout = 10 * time.Second 33 defaultWriteTimeout = 10 * time.Second 34 defaultRequestTimeout = 60 * time.Second 35 ) 36 37 // ContextFn is a function that sets the context for all service 38 // methods derived from the incoming request context 39 type ContextFn func(ctx context.Context, method string, headers map[string]string) thrift.Context 40 41 // PostResponseFn is a function that is called at the end of a request 42 type PostResponseFn func(ctx context.Context, method string, response apachethrift.TStruct) 43 44 // ServerOptions is a set of server options 45 type ServerOptions interface { 46 // SetReadTimeout sets the read timeout and returns a new ServerOptions 47 SetReadTimeout(value time.Duration) ServerOptions 48 49 // ReadTimeout returns the read timeout 50 ReadTimeout() time.Duration 51 52 // SetWriteTimeout sets the write timeout and returns a new ServerOptions 53 SetWriteTimeout(value time.Duration) ServerOptions 54 55 // WriteTimeout returns the write timeout 56 WriteTimeout() time.Duration 57 58 // SetRequestTimeout sets the request timeout and returns a new ServerOptions 59 SetRequestTimeout(value time.Duration) ServerOptions 60 61 // RequestTimeout returns the request timeout 62 RequestTimeout() time.Duration 63 64 // SetContextFn sets the context fn and returns a new ServerOptions 65 SetContextFn(value ContextFn) ServerOptions 66 67 // ContextFn returns the context fn 68 ContextFn() ContextFn 69 70 // SetPostResponseFn sets the post response fn and returns a new ServerOptions 71 SetPostResponseFn(value PostResponseFn) ServerOptions 72 73 // PostResponseFn returns the post response fn 74 PostResponseFn() PostResponseFn 75 } 76 77 type serverOptions struct { 78 readTimeout time.Duration 79 writeTimeout time.Duration 80 requestTimeout time.Duration 81 contextFn ContextFn 82 postResponseFn PostResponseFn 83 } 84 85 // NewServerOptions creates a new set of server options with defaults 86 func NewServerOptions() ServerOptions { 87 return &serverOptions{ 88 readTimeout: defaultReadTimeout, 89 writeTimeout: defaultWriteTimeout, 90 requestTimeout: defaultRequestTimeout, 91 } 92 } 93 94 func (o *serverOptions) SetReadTimeout(value time.Duration) ServerOptions { 95 opts := *o 96 opts.readTimeout = value 97 return &opts 98 } 99 100 func (o *serverOptions) ReadTimeout() time.Duration { 101 return o.readTimeout 102 } 103 104 func (o *serverOptions) SetWriteTimeout(value time.Duration) ServerOptions { 105 opts := *o 106 opts.writeTimeout = value 107 return &opts 108 } 109 110 func (o *serverOptions) WriteTimeout() time.Duration { 111 return o.writeTimeout 112 } 113 114 func (o *serverOptions) SetRequestTimeout(value time.Duration) ServerOptions { 115 opts := *o 116 opts.requestTimeout = value 117 return &opts 118 } 119 120 func (o *serverOptions) RequestTimeout() time.Duration { 121 return o.requestTimeout 122 } 123 124 func (o *serverOptions) SetContextFn(value ContextFn) ServerOptions { 125 opts := *o 126 opts.contextFn = value 127 return &opts 128 } 129 130 func (o *serverOptions) ContextFn() ContextFn { 131 return o.contextFn 132 } 133 134 func (o *serverOptions) SetPostResponseFn(value PostResponseFn) ServerOptions { 135 opts := *o 136 opts.postResponseFn = value 137 return &opts 138 } 139 140 func (o *serverOptions) PostResponseFn() PostResponseFn { 141 return o.postResponseFn 142 }