diff --git a/orion/config.go b/orion/config.go index 180243a6..ba8e6c33 100644 --- a/orion/config.go +++ b/orion/config.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "github.com/afex/hystrix-go/hystrix" "github.com/spf13/viper" @@ -55,6 +56,10 @@ type Config struct { DisableDefaultInterceptors bool // Receive message Size is used to update the default limit of message that can be received MaxRecvMsgSize int + // Read timeout http server, The time in seconds read request body timeout. + ReadTimeout int + // Write timeout http server, The time in seconds that start from read request complete to write the resp to client must be happened in this time. + WriteTimeout int } // HystrixConfig is configuration used by hystrix @@ -77,13 +82,13 @@ type HystrixConfig struct { DefaultErrorPercentThreshold int } -//ZipkinConfig is the configuration for the zipkin collector +// ZipkinConfig is the configuration for the zipkin collector type ZipkinConfig struct { //Addr is the address of the zipkin collector Addr string } -//NewRelicConfig is the configuration for newrelic +// NewRelicConfig is the configuration for newrelic type NewRelicConfig struct { APIKey string ServiceName string @@ -93,7 +98,7 @@ type NewRelicConfig struct { ExcludeAttributes []string } -//BuildDefaultConfig builds a default config object for Orion +// BuildDefaultConfig builds a default config object for Orion func BuildDefaultConfig(name string) Config { setup(name) readConfig(name) @@ -120,7 +125,7 @@ func BuildDefaultConfig(name string) Config { } } -//BuildDefaultHystrixConfig builds a default config for hystrix +// BuildDefaultHystrixConfig builds a default config for hystrix func BuildDefaultHystrixConfig() HystrixConfig { return HystrixConfig{ Port: viper.GetString("orion.HystrixPort"), @@ -134,14 +139,14 @@ func BuildDefaultHystrixConfig() HystrixConfig { } } -//BuildDefaultZipkinConfig builds a default config for zipkin +// BuildDefaultZipkinConfig builds a default config for zipkin func BuildDefaultZipkinConfig() ZipkinConfig { return ZipkinConfig{ Addr: viper.GetString("orion.ZipkinAddr"), } } -//BuildDefaultNewRelicConfig builds a default config for newrelic +// BuildDefaultNewRelicConfig builds a default config for newrelic func BuildDefaultNewRelicConfig() NewRelicConfig { return NewRelicConfig{ ServiceName: viper.GetString("orion.NewRelicServiceName"), diff --git a/orion/core.go b/orion/core.go index a28ce7b7..b89d2ac3 100644 --- a/orion/core.go +++ b/orion/core.go @@ -67,7 +67,7 @@ type middlewareInfo struct { middlewares []string } -//DefaultServerImpl provides a default implementation of orion.Server this can be embedded in custom orion.Server implementations +// DefaultServerImpl provides a default implementation of orion.Server this can be embedded in custom orion.Server implementations type DefaultServerImpl struct { config Config mu sync.Mutex @@ -87,7 +87,7 @@ type DefaultServerImpl struct { version uint64 } -//AddMiddleware adds middlewares for particular service/method +// AddMiddleware adds middlewares for particular service/method func (d *DefaultServerImpl) AddMiddleware(serviceName string, method string, middlewares ...string) { if d.middlewares == nil { d.middlewares = make(map[string]*middlewareInfo) @@ -114,7 +114,7 @@ func getSvcKey(serviceName, method string) string { return serviceName + "-" + method } -//AddEncoder is the implementation of handlers.Encodable +// AddEncoder is the implementation of handlers.Encodable func (d *DefaultServerImpl) AddEncoder(serviceName, method string, httpMethod []string, path string, encoder handlers.Encoder) { if d.encoders == nil { d.encoders = make(map[string]*encoderInfo) @@ -133,7 +133,7 @@ func (d *DefaultServerImpl) AddEncoder(serviceName, method string, httpMethod [] ei.encoder = encoder } -//AddDefaultEncoder is the implementation of handlers.Encodable +// AddDefaultEncoder is the implementation of handlers.Encodable func (d *DefaultServerImpl) AddDefaultEncoder(serviceName string, encoder Encoder) { if d.defEncoders == nil { d.defEncoders = make(map[string]handlers.Encoder) @@ -141,7 +141,7 @@ func (d *DefaultServerImpl) AddDefaultEncoder(serviceName string, encoder Encode d.defEncoders[serviceName] = encoder } -//AddHTTPHandler is the implementation of handlers.HTTPInterceptor +// AddHTTPHandler is the implementation of handlers.HTTPInterceptor func (d *DefaultServerImpl) AddHTTPHandler(serviceName string, method string, path string, handler handlers.HTTPHandler) { if d.encoders == nil { d.encoders = make(map[string]*encoderInfo) @@ -159,7 +159,7 @@ func (d *DefaultServerImpl) AddHTTPHandler(serviceName string, method string, pa ei.handler = handler } -//AddDecoder is the implementation of handlers.Decodable +// AddDecoder is the implementation of handlers.Decodable func (d *DefaultServerImpl) AddDecoder(serviceName, method string, decoder handlers.Decoder) { if d.decoders == nil { d.decoders = make(map[string]*decoderInfo) @@ -171,7 +171,7 @@ func (d *DefaultServerImpl) AddDecoder(serviceName, method string, decoder handl } } -//AddDefaultDecoder is the implementation of handlers.Decodable +// AddDefaultDecoder is the implementation of handlers.Decodable func (d *DefaultServerImpl) AddDefaultDecoder(serviceName string, decoder Decoder) { if d.defDecoders == nil { d.defDecoders = make(map[string]handlers.Decoder) @@ -179,7 +179,7 @@ func (d *DefaultServerImpl) AddDefaultDecoder(serviceName string, decoder Decode d.defDecoders[serviceName] = decoder } -//AddOption adds a option for the particular service/method +// AddOption adds a option for the particular service/method func (d *DefaultServerImpl) AddOption(serviceName, method, option string) { if d.options == nil { d.options = make(map[string]*optionInfo) @@ -191,8 +191,8 @@ func (d *DefaultServerImpl) AddOption(serviceName, method, option string) { } } -//GetOrionConfig returns current orion config -//NOTE: this config can not be modifies +// GetOrionConfig returns current orion config +// NOTE: this config can not be modifies func (d *DefaultServerImpl) GetOrionConfig() Config { return d.config } @@ -246,6 +246,8 @@ func (d *DefaultServerImpl) buildHandlers() []*handlerInfo { EnableProtoURL: d.config.EnableProtoURL, DefaultJSONPB: d.config.DefaultJSONPB, NRHttpTxNameType: d.config.NewRelicConfig.HttpTxNameType, + ReadTimeout: d.config.ReadTimeout, + WriteTimeout: d.config.WriteTimeout, } handler := http.NewHTTPHandler(config) hlrs = append(hlrs, &handlerInfo{ @@ -339,7 +341,7 @@ func (d *DefaultServerImpl) signalWatcher() { } } -//Start starts the orion server +// Start starts the orion server func (d *DefaultServerImpl) Start() { fmt.Println(BANNER) if d.config.HTTPOnly && d.config.GRPCOnly { @@ -424,8 +426,8 @@ func (d *DefaultServerImpl) Wait() error { return nil } -//RegisterService registers a service from a generated proto file -//Note: this is only called from code generated by orion plugin +// RegisterService registers a service from a generated proto file +// Note: this is only called from code generated by orion plugin func (d *DefaultServerImpl) RegisterService(sd *grpc.ServiceDesc, sf interface{}) error { d.init(false) // make sure its called before lock f, err := ToServiceFactoryV2(sf) @@ -469,7 +471,7 @@ func (d *DefaultServerImpl) registerService(sd *grpc.ServiceDesc, sf ServiceFact } -//AddInitializers adds the initializers to orion server +// AddInitializers adds the initializers to orion server func (d *DefaultServerImpl) AddInitializers(ins ...Initializer) { if d.initializers == nil { d.initializers = make([]Initializer, 0) @@ -480,12 +482,12 @@ func (d *DefaultServerImpl) AddInitializers(ins ...Initializer) { } -//GetConfig returns current config as parsed from the file/defaults +// GetConfig returns current config as parsed from the file/defaults func (d *DefaultServerImpl) GetConfig() map[string]interface{} { return viper.AllSettings() } -//Stop stops the server +// Stop stops the server func (d *DefaultServerImpl) Stop(timeout time.Duration) error { var wg sync.WaitGroup for _, h := range d.handlers { @@ -500,7 +502,7 @@ func (d *DefaultServerImpl) Stop(timeout time.Duration) error { return nil } -//GetDefaultServer returns a default server object that can be directly used to start orion server +// GetDefaultServer returns a default server object that can be directly used to start orion server func GetDefaultServer(name string, opts ...DefaultServerOption) Server { server := &DefaultServerImpl{ config: BuildDefaultConfig(name), @@ -511,7 +513,7 @@ func GetDefaultServer(name string, opts ...DefaultServerOption) Server { return server } -//GetDefaultServerWithConfig returns a default server object that uses provided configuration +// GetDefaultServerWithConfig returns a default server object that uses provided configuration func GetDefaultServerWithConfig(config Config) Server { return &DefaultServerImpl{ config: config, diff --git a/orion/handlers/http/handler.go b/orion/handlers/http/handler.go index 0044d00f..d193d0c0 100644 --- a/orion/handlers/http/handler.go +++ b/orion/handlers/http/handler.go @@ -3,6 +3,7 @@ package http import ( "context" "fmt" + "math" "net" "net/http" "strings" @@ -14,7 +15,7 @@ import ( "google.golang.org/grpc" ) -//NewHTTPHandler creates a new HTTP handler +// NewHTTPHandler creates a new HTTP handler func NewHTTPHandler(config Config) handlers.Handler { return &httpHandler{ config: config, @@ -189,9 +190,12 @@ func (h *httpHandler) Run(httpListener net.Listener) error { } } r.NotFoundHandler = ¬FoundHandler{} + + readTimeout := math.Min(300, math.Max(5, float64(h.config.ReadTimeout))) + writeTimeout := math.Min(300, math.Max(10, float64(h.config.WriteTimeout))) h.svr = &http.Server{ - ReadTimeout: 5 * time.Second, - WriteTimeout: 10 * time.Second, + ReadTimeout: time.Duration(readTimeout) * time.Second, + WriteTimeout: time.Duration(writeTimeout) * time.Second, Handler: r, } return h.svr.Serve(httpListener) diff --git a/orion/handlers/http/types.go b/orion/handlers/http/types.go index 9b738470..32ca6fba 100644 --- a/orion/handlers/http/types.go +++ b/orion/handlers/http/types.go @@ -40,17 +40,19 @@ const ( ) const ( - NRTxNameTypeMethod = "method" + NRTxNameTypeMethod = "method" NRTxNameTypeFullMethod = "fullmethod" - NRTxNameTypeRoute = "route" + NRTxNameTypeRoute = "route" ) -//Config is the configuration for HTTP Handler +// Config is the configuration for HTTP Handler type Config struct { handlers.CommonConfig - EnableProtoURL bool - DefaultJSONPB bool + EnableProtoURL bool + DefaultJSONPB bool NRHttpTxNameType string + ReadTimeout int + WriteTimeout int } type serviceInfo struct {