-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use internal utils methods and deprecated old utils package
- Loading branch information
1 parent
a56d9ac
commit 3589f7b
Showing
14 changed files
with
79 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package utils | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"time" | ||
) | ||
|
||
// Runtime is for setting timeout, proxy and host | ||
type Runtime struct { | ||
ReadTimeout int | ||
ConnectTimeout int | ||
Proxy string | ||
Host string | ||
STSEndpoint string | ||
} | ||
|
||
// NewRuntime returns a Runtime | ||
func NewRuntime(readTimeout, connectTimeout int, proxy string, host string) *Runtime { | ||
return &Runtime{ | ||
ReadTimeout: readTimeout, | ||
ConnectTimeout: connectTimeout, | ||
Proxy: proxy, | ||
Host: host, | ||
} | ||
} | ||
|
||
// Timeout is for connect Timeout | ||
func Timeout(connectTimeout time.Duration) func(cxt context.Context, net, addr string) (c net.Conn, err error) { | ||
return func(ctx context.Context, network, address string) (net.Conn, error) { | ||
return (&net.Dialer{ | ||
Timeout: connectTimeout, | ||
DualStack: true, | ||
}).DialContext(ctx, network, address) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package utils | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_NewRuntime(t *testing.T) { | ||
runitme := NewRuntime(10, 10, "proxy", "host") | ||
assert.Equal(t, 10, runitme.ReadTimeout) | ||
assert.Equal(t, 10, runitme.ConnectTimeout) | ||
assert.Equal(t, "proxy", runitme.Proxy) | ||
assert.Equal(t, "host", runitme.Host) | ||
|
||
dialContext := Timeout(5 * time.Second) | ||
ctx, cancelFunc := context.WithTimeout(context.Background(), 1*time.Second) | ||
assert.NotNil(t, cancelFunc) | ||
c, err := dialContext(ctx, "127.0.0.1", "127.0.0.2") | ||
assert.Nil(t, c) | ||
assert.Equal(t, "dial 127.0.0.1: unknown network 127.0.0.1", err.Error()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters