Skip to content

Commit

Permalink
allow setup readtime, writetimeout by config (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenth authored Jun 29, 2023
1 parent 52b5e11 commit fe74c94
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 32 deletions.
17 changes: 11 additions & 6 deletions orion/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"

"github.com/afex/hystrix-go/hystrix"
"github.com/spf13/viper"

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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"),
Expand All @@ -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"),
Expand Down
38 changes: 20 additions & 18 deletions orion/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -133,15 +133,15 @@ 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)
}
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)
Expand All @@ -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)
Expand All @@ -171,15 +171,15 @@ 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)
}
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)
Expand All @@ -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
}
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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),
Expand All @@ -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,
Expand Down
10 changes: 7 additions & 3 deletions orion/handlers/http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package http
import (
"context"
"fmt"
"math"
"net"
"net/http"
"strings"
Expand All @@ -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,
Expand Down Expand Up @@ -189,9 +190,12 @@ func (h *httpHandler) Run(httpListener net.Listener) error {
}
}
r.NotFoundHandler = &notFoundHandler{}

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)
Expand Down
12 changes: 7 additions & 5 deletions orion/handlers/http/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit fe74c94

Please sign in to comment.