add reload and plugin support for hop
This commit is contained in:
@ -2,6 +2,7 @@ package pht
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
@ -38,6 +39,7 @@ type serverOptions struct {
|
||||
tlsConfig *tls.Config
|
||||
readBufferSize int
|
||||
readTimeout time.Duration
|
||||
mptcp bool
|
||||
logger logger.Logger
|
||||
}
|
||||
|
||||
@ -81,6 +83,12 @@ func ReadTimeoutServerOption(timeout time.Duration) ServerOption {
|
||||
}
|
||||
}
|
||||
|
||||
func MPTCPServerOption(mptcp bool) ServerOption {
|
||||
return func(opts *serverOptions) {
|
||||
opts.mptcp = mptcp
|
||||
}
|
||||
}
|
||||
|
||||
func LoggerServerOption(logger logger.Logger) ServerOption {
|
||||
return func(opts *serverOptions) {
|
||||
opts.logger = logger
|
||||
@ -187,7 +195,13 @@ func (s *Server) ListenAndServe() error {
|
||||
if xnet.IsIPv4(s.httpServer.Addr) {
|
||||
network = "tcp4"
|
||||
}
|
||||
ln, err := net.Listen(network, s.httpServer.Addr)
|
||||
|
||||
lc := net.ListenConfig{}
|
||||
if s.options.mptcp {
|
||||
lc.SetMultipathTCP(true)
|
||||
s.options.logger.Debugf("mptcp enabled: %v", lc.MultipathTCP())
|
||||
}
|
||||
ln, err := lc.Listen(context.Background(), network, s.httpServer.Addr)
|
||||
if err != nil {
|
||||
s.options.logger.Error(err)
|
||||
return err
|
||||
|
@ -1,91 +0,0 @@
|
||||
package plugin
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/backoff"
|
||||
"google.golang.org/grpc/credentials"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
)
|
||||
|
||||
type Options struct {
|
||||
Token string
|
||||
TLSConfig *tls.Config
|
||||
Header http.Header
|
||||
Timeout time.Duration
|
||||
}
|
||||
|
||||
type Option func(opts *Options)
|
||||
|
||||
func TokenOption(token string) Option {
|
||||
return func(opts *Options) {
|
||||
opts.Token = token
|
||||
}
|
||||
}
|
||||
|
||||
func TLSConfigOption(cfg *tls.Config) Option {
|
||||
return func(opts *Options) {
|
||||
opts.TLSConfig = cfg
|
||||
}
|
||||
}
|
||||
|
||||
func HeaderOption(header http.Header) Option {
|
||||
return func(opts *Options) {
|
||||
opts.Header = header
|
||||
}
|
||||
}
|
||||
|
||||
func TimeoutOption(timeout time.Duration) Option {
|
||||
return func(opts *Options) {
|
||||
opts.Timeout = timeout
|
||||
}
|
||||
}
|
||||
|
||||
func NewGRPCConn(addr string, opts *Options) (*grpc.ClientConn, error) {
|
||||
grpcOpts := []grpc.DialOption{
|
||||
// grpc.WithBlock(),
|
||||
grpc.WithConnectParams(grpc.ConnectParams{
|
||||
Backoff: backoff.DefaultConfig,
|
||||
}),
|
||||
grpc.FailOnNonTempDialError(true),
|
||||
}
|
||||
if opts.TLSConfig != nil {
|
||||
grpcOpts = append(grpcOpts,
|
||||
grpc.WithAuthority(opts.TLSConfig.ServerName),
|
||||
grpc.WithTransportCredentials(credentials.NewTLS(opts.TLSConfig)),
|
||||
)
|
||||
} else {
|
||||
grpcOpts = append(grpcOpts, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||
}
|
||||
if opts.Token != "" {
|
||||
grpcOpts = append(grpcOpts, grpc.WithPerRPCCredentials(&rpcCredentials{token: opts.Token}))
|
||||
}
|
||||
return grpc.Dial(addr, grpcOpts...)
|
||||
}
|
||||
|
||||
type rpcCredentials struct {
|
||||
token string
|
||||
}
|
||||
|
||||
func (c *rpcCredentials) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
|
||||
return map[string]string{
|
||||
"token": c.token,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *rpcCredentials) RequireTransportSecurity() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func NewHTTPClient(opts *Options) *http.Client {
|
||||
return &http.Client{
|
||||
Timeout: opts.Timeout,
|
||||
Transport: &http.Transport{
|
||||
TLSClientConfig: opts.TLSConfig,
|
||||
},
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user