feat: make http transport client exchangeable

This commit is contained in:
franklin 2021-07-30 17:37:10 +02:00
parent ae29f1060c
commit 93fcca1a5f
3 changed files with 16 additions and 5 deletions

View File

@ -1,6 +1,7 @@
package client package client
import ( import (
"net/http"
"time" "time"
"github.com/foomo/contentserver/content" "github.com/foomo/contentserver/content"
@ -18,16 +19,26 @@ func NewClient(
server string, server string,
connectionPoolSize int, connectionPoolSize int,
waitTimeout time.Duration, waitTimeout time.Duration,
) (c *Client, err error) {
return NewClientWithTransport(NewSocketTransport(server, connectionPoolSize, waitTimeout))
}
func NewClientWithTransport(
transport transport,
) (c *Client, err error) { ) (c *Client, err error) {
c = &Client{ c = &Client{
t: newSocketTransport(server, connectionPoolSize, waitTimeout), t: transport,
} }
return return
} }
func NewHTTPClient(server string) (c *Client, err error) { func NewHTTPClient(server string) (c *Client, err error) {
return NewHTTPClientWithTransport(NewHTTPTransport(server, http.DefaultClient))
}
func NewHTTPClientWithTransport(transport transport) (c *Client, err error) {
c = &Client{ c = &Client{
t: newHTTPTransport(server), t: transport,
} }
return return
} }

View File

@ -14,10 +14,10 @@ type httpTransport struct {
endpoint string endpoint string
} }
func newHTTPTransport(server string) transport { func NewHTTPTransport(server string, client *http.Client) transport {
return &httpTransport{ return &httpTransport{
endpoint: server, endpoint: server,
client: http.DefaultClient, client: client,
} }
} }

View File

@ -24,7 +24,7 @@ type socketTransport struct {
connPool *connectionPool connPool *connectionPool
} }
func newSocketTransport(server string, connectionPoolSize int, waitTimeout time.Duration) transport { func NewSocketTransport(server string, connectionPoolSize int, waitTimeout time.Duration) transport {
return &socketTransport{ return &socketTransport{
connPool: newConnectionPool(server, connectionPoolSize, waitTimeout), connPool: newConnectionPool(server, connectionPoolSize, waitTimeout),
} }