Skip to content

Commit

Permalink
feat: set read and write timeout (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
Duslia authored Dec 6, 2023
1 parent ec86e8c commit cb7e10e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,12 @@ func (hc *HostClient) onConnectionDropped(c *clientConn) {

func (hc *HostClient) createConn() (*clientConn, *list.Element, error) {
conn, err := hc.dialHostHard()
if hc.ReadTimeout != 0 {
conn.SetReadTimeout(hc.ReadTimeout)
}
if hc.WriteTimeout != 0 {
conn.SetWriteTimeout(hc.WriteTimeout)
}
if err != nil {
return nil, nil, err
}
Expand Down
20 changes: 20 additions & 0 deletions config/client_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ type ClientConfig struct {

// If true, h2 client won't add default user-agent
NoDefaultUserAgent bool

// readtimeout sets timeout when read package
ReadTimeout time.Duration

// writetimeout sets timeout when write package
WriteTimeout time.Duration
}

func (o *ClientConfig) Apply(opts []ClientOption) {
Expand Down Expand Up @@ -226,6 +232,20 @@ func WithClientDisableKeepAlive(disable bool) ClientOption {
}}
}

// WithClientReadTimeout is used to set client readtimeout.
func WithClientReadTimeout(t time.Duration) ClientOption {
return ClientOption{F: func(o *ClientConfig) {
o.ReadTimeout = t
}}
}

// WithClientWriteTimeout is used to set client writeTimeout.
func WithClientWriteTimeout(t time.Duration) ClientOption {
return ClientOption{F: func(o *ClientConfig) {
o.WriteTimeout = t
}}
}

func NewClientConfig(opts ...ClientOption) *ClientConfig {
cfg := &ClientConfig{
PingTimeout: consts.DefaultPingTimeout,
Expand Down

0 comments on commit cb7e10e

Please sign in to comment.