Skip to content

Commit

Permalink
update based on comments, update comment
Browse files Browse the repository at this point in the history
Signed-off-by: Noah Kreiger <noahkreiger@gmail.com>
  • Loading branch information
nkreiger committed Apr 22, 2024
1 parent b62ee24 commit 716e20a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
4 changes: 2 additions & 2 deletions v2/protocol/http/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func WithReadTimeout(timeout time.Duration) Option {
if p == nil {
return fmt.Errorf("http read timeout option can not set nil protocol")
}
p.ReadTimeout = timeout
p.readTimeout = timeout
return nil
}
}
Expand All @@ -100,7 +100,7 @@ func WithWriteTimeout(timeout time.Duration) Option {
if p == nil {
return fmt.Errorf("http write timeout option can not set nil protocol")
}
p.WriteTimeout = timeout
p.writeTimeout = timeout
return nil
}
}
Expand Down
20 changes: 17 additions & 3 deletions v2/protocol/http/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,14 @@ func TestWithReadTimeout(t *testing.T) {
t: &Protocol{},
timeout: time.Minute * 4,
want: &Protocol{
ReadTimeout: time.Minute * 4,
readTimeout: time.Minute * 4,
},
},
"negative timeout": {
t: &Protocol{},
timeout: -1,
want: &Protocol{
writeTimeout: -1,
},
},
"nil protocol": {
Expand Down Expand Up @@ -370,11 +377,18 @@ func TestWithWriteTimeout(t *testing.T) {
t: &Protocol{},
timeout: time.Minute * 4,
want: &Protocol{
WriteTimeout: time.Minute * 4,
writeTimeout: time.Minute * 4,
},
},
"negative timeout": {
t: &Protocol{},
timeout: -1,
want: &Protocol{
writeTimeout: -1,
},
},
"nil protocol": {
wantErr: `http write timeout option can not set nil protocol`,
wantErr: "http write timeout option can not set nil protocol",
},
}
for n, tc := range testCases {
Expand Down
18 changes: 10 additions & 8 deletions v2/protocol/http/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,19 @@ type Protocol struct {

// ReadTimeout defines the http.Service ReadTimeout
// It is the maximum duration for reading the entire
// request, including the body. A zero or negative value means
// request, including the body. A negative value means
// there will be no timeout.
ReadTimeout time.Duration
// If 0, DefaultReadTimeout is used.
readTimeout time.Duration

// WriteTimeout defines the http.Service WriteTimeout
// It is the maximum duration before timing out
// writes of the response. It is reset whenever a new
// request's header is read. Like ReadTimeout, it does not
// let Handlers make decisions on a per-request basis.
// A zero or negative value means there will be no timeout.
WriteTimeout time.Duration
// A negative value means there will be no timeout.
// If 0, DefaultWriteTimeout is used.
writeTimeout time.Duration

// Port is the port configured to bind the receiver to. Defaults to 8080.
// If you want to know the effective port you're listening to, use GetListeningPort()
Expand Down Expand Up @@ -130,12 +132,12 @@ func New(opts ...Option) (*Protocol, error) {
p.ShutdownTimeout = DefaultShutdownTimeout
}

if p.ReadTimeout == 0 {
p.ReadTimeout = DefaultTimeout
if p.readTimeout == 0 {
p.readTimeout = DefaultTimeout
}

if p.WriteTimeout == 0 {
p.WriteTimeout = DefaultTimeout
if p.writeTimeout <= 0 {
p.writeTimeout = DefaultTimeout
}

if p.isRetriableFunc == nil {
Expand Down
4 changes: 2 additions & 2 deletions v2/protocol/http/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ func TestNew(t *testing.T) {
want: &Protocol{
Client: http.DefaultClient,
ShutdownTimeout: dst,
ReadTimeout: ot,
WriteTimeout: ot,
readTimeout: ot,
writeTimeout: ot,
Port: -1,
},
},
Expand Down

0 comments on commit 716e20a

Please sign in to comment.