Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit cb8ad66

Browse files
zukiyAndrey Kolomiets
authored andcommitted
api server: readTimeout and writeTimeout added to config (#54)
1 parent b6a5831 commit cb8ad66

3 files changed

Lines changed: 58 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## [3.2.0]- 2019-06-06
4+
### add:
5+
- api config with timeouts
6+
37
## [3.1.3]- 2019-03-05
48
### fix:
59
- added auto-create exchange

api/api.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ import (
1515
"github.com/gorilla/mux"
1616
)
1717

18+
const (
19+
cDefaultReadTimeout = 10 * time.Second
20+
cDefaultWriteTimeout = 10 * time.Second
21+
)
22+
1823
// ErrorWithCode contains a message and code, which will be used as a http response code.
1924
type ErrorWithCode struct {
2025
code int
@@ -60,9 +65,10 @@ type PathInfo struct {
6065

6166
// Config used to pass settings and handlers to the server.
6267
type Config struct {
63-
Addr string
64-
Prefix string
65-
Handlers []PathInfo
68+
Addr string
69+
Prefix string
70+
Handlers []PathInfo
71+
ReadTimeout, WriteTimeout time.Duration
6672
}
6773

6874
// Server is a REST api server.
@@ -82,11 +88,24 @@ func NewServer(cfg *Config) *Server {
8288
router.Handle(s.Path, srv).Methods(s.Method)
8389
}
8490
s.m.Lock()
91+
92+
// override timeouts
93+
readTimeout := cDefaultReadTimeout
94+
writeTimeout := cDefaultWriteTimeout
95+
96+
if cfg.ReadTimeout > 0 {
97+
readTimeout = cfg.ReadTimeout
98+
}
99+
100+
if cfg.WriteTimeout > 0 {
101+
writeTimeout = cfg.WriteTimeout
102+
}
103+
85104
s.server = &http.Server{
86105
Addr: s.cfg.Addr,
87106
Handler: r,
88-
ReadTimeout: 10 * time.Second,
89-
WriteTimeout: 10 * time.Second,
107+
ReadTimeout: readTimeout,
108+
WriteTimeout: writeTimeout,
90109
MaxHeaderBytes: 1 << 20,
91110
}
92111
s.m.Unlock()

api/api_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,33 @@ func TestNewErrorWithCode(t *testing.T) {
114114
assert.NoError(t, err)
115115
assert.Equal(t, []byte("null"), res)
116116
}
117+
118+
func TestTimeouts(t *testing.T) {
119+
a := assert.New(t)
120+
121+
testCases := []struct {
122+
config Config
123+
expectReadTimeout time.Duration
124+
expectWriteTimeout time.Duration
125+
}{
126+
{
127+
Config{},
128+
10 * time.Second,
129+
10 * time.Second,
130+
},
131+
{
132+
Config{
133+
ReadTimeout: 100500 * time.Second,
134+
WriteTimeout: 500100 * time.Second,
135+
},
136+
100500 * time.Second,
137+
500100 * time.Second,
138+
},
139+
}
140+
141+
for _, tc := range testCases {
142+
server := NewServer(&tc.config)
143+
a.Equal(tc.expectReadTimeout, server.server.ReadTimeout)
144+
a.Equal(tc.expectWriteTimeout, server.server.WriteTimeout)
145+
}
146+
}

0 commit comments

Comments
 (0)