File: options.go

package info (click to toggle)
golang-github-vulcand-oxy 2.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 728 kB
  • sloc: makefile: 14
file content (113 lines) | stat: -rw-r--r-- 2,675 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package buffer

import (
	"fmt"

	"github.com/vulcand/oxy/v2/utils"
)

// Option represents an option you can pass to New.
type Option func(b *Buffer) error

// Logger defines the logger used by Buffer.
func Logger(l utils.Logger) Option {
	return func(b *Buffer) error {
		b.log = l
		return nil
	}
}

// Verbose additional debug information.
func Verbose(verbose bool) Option {
	return func(b *Buffer) error {
		b.verbose = verbose
		return nil
	}
}

// Cond Conditional setter.
// ex: Cond(a > 4, MemRequestBodyBytes(a))
func Cond(condition bool, setter Option) Option {
	if !condition {
		// NoOp setter
		return func(*Buffer) error {
			return nil
		}
	}
	return setter
}

// Retry provides a predicate that allows buffer middleware to replay the request
// if it matches certain condition, e.g. returns special error code. Available functions are:
//
// Attempts() - limits the amount of retry attempts
// ResponseCode() - returns http response code
// IsNetworkError() - tests if response code is related to networking error
//
// Example of the predicate:
//
// `Attempts() <= 2 && ResponseCode() == 502`.
func Retry(predicate string) Option {
	return func(b *Buffer) error {
		p, err := parseExpression(predicate)
		if err != nil {
			return err
		}
		b.retryPredicate = p
		return nil
	}
}

// ErrorHandler sets error handler of the server.
func ErrorHandler(h utils.ErrorHandler) Option {
	return func(b *Buffer) error {
		b.errHandler = h
		return nil
	}
}

// MaxRequestBodyBytes sets the maximum request body size in bytes.
func MaxRequestBodyBytes(m int64) Option {
	return func(b *Buffer) error {
		if m < 0 {
			return fmt.Errorf("max bytes should be >= 0 got %d", m)
		}
		b.maxRequestBodyBytes = m
		return nil
	}
}

// MemRequestBodyBytes bytes sets the maximum request body to be stored in memory
// buffer middleware will serialize the excess to disk.
func MemRequestBodyBytes(m int64) Option {
	return func(b *Buffer) error {
		if m < 0 {
			return fmt.Errorf("mem bytes should be >= 0 got %d", m)
		}
		b.memRequestBodyBytes = m
		return nil
	}
}

// MaxResponseBodyBytes sets the maximum response body size in bytes.
func MaxResponseBodyBytes(m int64) Option {
	return func(b *Buffer) error {
		if m < 0 {
			return fmt.Errorf("max bytes should be >= 0 got %d", m)
		}
		b.maxResponseBodyBytes = m
		return nil
	}
}

// MemResponseBodyBytes sets the maximum response body to be stored in memory
// buffer middleware will serialize the excess to disk.
func MemResponseBodyBytes(m int64) Option {
	return func(b *Buffer) error {
		if m < 0 {
			return fmt.Errorf("mem bytes should be >= 0 got %d", m)
		}
		b.memResponseBodyBytes = m
		return nil
	}
}