Skip to content

Commit

Permalink
max_error_reason_size limit error reason
Browse files Browse the repository at this point in the history
  • Loading branch information
k.torgaev committed Dec 25, 2023
1 parent 9d9dd19 commit 72afd67
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
9 changes: 9 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ var (

defaultMaxPayloadSize = ByteSize(1 << 50)

defaultMaxErrorReasonSize = ByteSize(1 << 50)

defaultRetryNumber = 0
)

Expand All @@ -67,6 +69,9 @@ type Config struct {

NetworkGroups []NetworkGroups `yaml:"network_groups,omitempty"`

// Maximum size of error payload
MaxErrorReasonSize ByteSize `yaml:"max_error_reason_size,omitempty"`

Caches []Cache `yaml:"caches,omitempty"`

ParamGroups []ParamGroup `yaml:"param_groups,omitempty"`
Expand Down Expand Up @@ -188,6 +193,10 @@ func (cfg *Config) setDefaults() error {
c.setDefaults()
}

if cfg.MaxErrorReasonSize <= 0 {
cfg.MaxErrorReasonSize = defaultMaxErrorReasonSize
}

cfg.setServerMaxResponseTime(maxResponseTime)

return nil
Expand Down
17 changes: 13 additions & 4 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type reverseProxy struct {
hasWildcarded bool
maxIdleConns int
maxIdleConnsPerHost int
maxErrorReasonSize int64
}

func newReverseProxy(cfgCp *config.ConnectionPool) *reverseProxy {
Expand Down Expand Up @@ -451,12 +452,18 @@ func (rp *reverseProxy) serveFromCache(s *scope, srw *statResponseWriter, req *h
tmpFileRespWriter.WriteHeader(srw.statusCode)
}

errString, err := toString(reader)
if err != nil {
log.Errorf("%s failed to get error reason: %s", s, err.Error())
var errReason string
if contentLength > rp.maxErrorReasonSize {
log.Infof("%s: Error reason length (%d) is greater than max error reason size (%d)", s, contentLength, rp.maxErrorReasonSize)
} else {
errString, err := toString(reader)
if err != nil {
log.Errorf("%s failed to get error reason: %s", s, err.Error())
}

errReason = fmt.Sprintf("%s %s", failedTransactionPrefix, errString)
}

errReason := fmt.Sprintf("%s %s", failedTransactionPrefix, errString)
rp.completeTransaction(s, statusCode, userCache, key, q, errReason)

// we need to reset the offset since the reader of tmpFileRespWriter was already
Expand Down Expand Up @@ -622,6 +629,8 @@ func (rp *reverseProxy) applyConfig(cfg *config.Config) error {
return err
}

rp.maxErrorReasonSize = int64(cfg.MaxErrorReasonSize)

caches := make(map[string]*cache.AsyncCache, len(cfg.Caches))
defer func() {
// caches is swapped with old caches from rp.caches
Expand Down

0 comments on commit 72afd67

Please sign in to comment.