145 lines
3.2 KiB
Go
145 lines
3.2 KiB
Go
|
package http
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"crypto/tls"
|
||
|
"net"
|
||
|
"net/http"
|
||
|
|
||
|
"github.com/unistack-org/micro/v3/client"
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
// DefaultPoolMaxStreams maximum streams on a connectioin
|
||
|
// (20)
|
||
|
DefaultPoolMaxStreams = 20
|
||
|
|
||
|
// DefaultPoolMaxIdle maximum idle conns of a pool
|
||
|
// (50)
|
||
|
DefaultPoolMaxIdle = 50
|
||
|
|
||
|
// DefaultMaxRecvMsgSize maximum message that client can receive
|
||
|
// (4 MB).
|
||
|
DefaultMaxRecvMsgSize = 1024 * 1024 * 4
|
||
|
|
||
|
// DefaultMaxSendMsgSize maximum message that client can send
|
||
|
// (4 MB).
|
||
|
DefaultMaxSendMsgSize = 1024 * 1024 * 4
|
||
|
)
|
||
|
|
||
|
type poolMaxStreams struct{}
|
||
|
type poolMaxIdle struct{}
|
||
|
type codecsKey struct{}
|
||
|
type tlsAuth struct{}
|
||
|
type maxRecvMsgSizeKey struct{}
|
||
|
type maxSendMsgSizeKey struct{}
|
||
|
|
||
|
// maximum streams on a connectioin
|
||
|
func PoolMaxStreams(n int) client.Option {
|
||
|
return func(o *client.Options) {
|
||
|
if o.Context == nil {
|
||
|
o.Context = context.Background()
|
||
|
}
|
||
|
o.Context = context.WithValue(o.Context, poolMaxStreams{}, n)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// maximum idle conns of a pool
|
||
|
func PoolMaxIdle(d int) client.Option {
|
||
|
return func(o *client.Options) {
|
||
|
if o.Context == nil {
|
||
|
o.Context = context.Background()
|
||
|
}
|
||
|
o.Context = context.WithValue(o.Context, poolMaxIdle{}, d)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// AuthTLS should be used to setup a secure authentication using TLS
|
||
|
func AuthTLS(t *tls.Config) client.Option {
|
||
|
return func(o *client.Options) {
|
||
|
if o.Context == nil {
|
||
|
o.Context = context.Background()
|
||
|
}
|
||
|
o.Context = context.WithValue(o.Context, tlsAuth{}, t)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//
|
||
|
// MaxRecvMsgSize set the maximum size of message that client can receive.
|
||
|
//
|
||
|
func MaxRecvMsgSize(s int) client.Option {
|
||
|
return func(o *client.Options) {
|
||
|
if o.Context == nil {
|
||
|
o.Context = context.Background()
|
||
|
}
|
||
|
o.Context = context.WithValue(o.Context, maxRecvMsgSizeKey{}, s)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//
|
||
|
// MaxSendMsgSize set the maximum size of message that client can send.
|
||
|
//
|
||
|
func MaxSendMsgSize(s int) client.Option {
|
||
|
return func(o *client.Options) {
|
||
|
if o.Context == nil {
|
||
|
o.Context = context.Background()
|
||
|
}
|
||
|
o.Context = context.WithValue(o.Context, maxSendMsgSizeKey{}, s)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
type httpClientKey struct{}
|
||
|
|
||
|
func HTTPClient(c *http.Client) client.Option {
|
||
|
return func(o *client.Options) {
|
||
|
if o.Context == nil {
|
||
|
o.Context = context.Background()
|
||
|
}
|
||
|
o.Context = context.WithValue(o.Context, httpClientKey{}, c)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
type httpDialerKey struct{}
|
||
|
|
||
|
func HTTPDialer(d *net.Dialer) client.Option {
|
||
|
return func(o *client.Options) {
|
||
|
if o.Context == nil {
|
||
|
o.Context = context.Background()
|
||
|
}
|
||
|
o.Context = context.WithValue(o.Context, httpDialerKey{}, d)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
type methodKey struct{}
|
||
|
|
||
|
func Method(m string) client.CallOption {
|
||
|
return func(o *client.CallOptions) {
|
||
|
if o.Context == nil {
|
||
|
o.Context = context.Background()
|
||
|
}
|
||
|
o.Context = context.WithValue(o.Context, methodKey{}, m)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
type pathKey struct{}
|
||
|
|
||
|
func Path(p string) client.CallOption {
|
||
|
return func(o *client.CallOptions) {
|
||
|
if o.Context == nil {
|
||
|
o.Context = context.Background()
|
||
|
}
|
||
|
o.Context = context.WithValue(o.Context, pathKey{}, p)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
type errorMapKey struct{}
|
||
|
|
||
|
func ErrorMap(m map[string]interface{}) client.CallOption {
|
||
|
return func(o *client.CallOptions) {
|
||
|
if o.Context == nil {
|
||
|
o.Context = context.Background()
|
||
|
}
|
||
|
o.Context = context.WithValue(o.Context, errorMapKey{}, m)
|
||
|
}
|
||
|
}
|