Skip to content

Commit

Permalink
Backport knative#4465
Browse files Browse the repository at this point in the history
Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>
  • Loading branch information
slinkydeveloper committed Nov 5, 2020
1 parent 6c1d2a5 commit e739e10
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 47 deletions.
1 change: 1 addition & 0 deletions cmd/mtbroker/ingress/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ func main() {
MaxIdleConns: defaultMaxIdleConnections,
MaxIdleConnsPerHost: defaultMaxIdleConnectionsPerHost,
}
kncloudevents.ConfigureConnectionArgs(&connectionArgs)
sender, err := kncloudevents.NewHttpMessageSender(&connectionArgs, "")
if err != nil {
logger.Fatal("Unable to create message sender", zap.Error(err))
Expand Down
106 changes: 106 additions & 0 deletions pkg/kncloudevents/http_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
Copyright 2020 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package kncloudevents

import (
nethttp "net/http"
"sync"
"time"

"go.opencensus.io/plugin/ochttp"
"knative.dev/pkg/tracing/propagation/tracecontextb3"
)

const (
defaultRetryWaitMin = 1 * time.Second
defaultRetryWaitMax = 30 * time.Second
)

type holder struct {
clientMutex sync.Mutex
connectionArgs *ConnectionArgs
client **nethttp.Client
}

var clientHolder = holder{}

// The used HTTP client is a singleton, so the same http client is reused across all the application.
// If connection args is modified, client is cleaned and a new one is created.
func getClient() *nethttp.Client {
clientHolder.clientMutex.Lock()
defer clientHolder.clientMutex.Unlock()

if clientHolder.client == nil {
// Add connection options to the default transport.
var base = nethttp.DefaultTransport.(*nethttp.Transport).Clone()
clientHolder.connectionArgs.configureTransport(base)
c := &nethttp.Client{
// Add output tracing.
Transport: &ochttp.Transport{
Base: base,
Propagation: tracecontextb3.TraceContextEgress,
},
}
clientHolder.client = &c
}

return *clientHolder.client
}

// ConfigureConnectionArgs configures the new connection args.
// The existing client won't be affected, but a new one will be created.
// Use sparingly, because it might lead to creating a lot of clients, none of them sharing their connection pool!
func ConfigureConnectionArgs(ca *ConnectionArgs) {
clientHolder.clientMutex.Lock()
defer clientHolder.clientMutex.Unlock()

// Check if same config
if clientHolder.connectionArgs != nil &&
ca != nil &&
ca.MaxIdleConns == clientHolder.connectionArgs.MaxIdleConns &&
ca.MaxIdleConnsPerHost == clientHolder.connectionArgs.MaxIdleConnsPerHost {
return
}

if clientHolder.client != nil {
// Let's try to clean up a bit the existing client
// Note: this won't remove it nor close it
(*clientHolder.client).CloseIdleConnections()

// Setting client to nil
clientHolder.client = nil
}

clientHolder.connectionArgs = ca
}

// ConnectionArgs allow to configure connection parameters to the underlying
// HTTP Client transport.
type ConnectionArgs struct {
// MaxIdleConns refers to the max idle connections, as in net/http/transport.
MaxIdleConns int
// MaxIdleConnsPerHost refers to the max idle connections per host, as in net/http/transport.
MaxIdleConnsPerHost int
}

func (ca *ConnectionArgs) configureTransport(transport *nethttp.Transport) {
if ca == nil {
return
}
transport.MaxIdleConns = ca.MaxIdleConns
transport.MaxIdleConnsPerHost = ca.MaxIdleConnsPerHost
}
72 changes: 72 additions & 0 deletions pkg/kncloudevents/http_client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
Copyright 2020 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package kncloudevents

import (
nethttp "net/http"
"testing"

"github.com/stretchr/testify/require"
"go.opencensus.io/plugin/ochttp"
)

func TestConfigureConnectionArgs(t *testing.T) {
// Set connection args
ConfigureConnectionArgs(&ConnectionArgs{
MaxIdleConnsPerHost: 1000,
MaxIdleConns: 1000,
})
client1 := getClient()

require.Same(t, getClient(), client1)
require.Equal(t, 1000, castToTransport(client1).MaxIdleConns)
require.Equal(t, 1000, castToTransport(client1).MaxIdleConnsPerHost)

// Set other connection args
ConfigureConnectionArgs(&ConnectionArgs{
MaxIdleConnsPerHost: 2000,
MaxIdleConns: 2000,
})
client2 := getClient()

require.Same(t, getClient(), client2)
require.Equal(t, 2000, castToTransport(client2).MaxIdleConns)
require.Equal(t, 2000, castToTransport(client2).MaxIdleConnsPerHost)

// Try to set the same value and client should not be cleaned up
ConfigureConnectionArgs(&ConnectionArgs{
MaxIdleConnsPerHost: 2000,
MaxIdleConns: 2000,
})
require.Same(t, getClient(), client2)

// Set back to nil
ConfigureConnectionArgs(nil)
client3 := getClient()

require.Same(t, getClient(), client3)
require.Equal(t, nethttp.DefaultTransport.(*nethttp.Transport).MaxIdleConns, castToTransport(client3).MaxIdleConns)
require.Equal(t, nethttp.DefaultTransport.(*nethttp.Transport).MaxIdleConnsPerHost, castToTransport(client3).MaxIdleConnsPerHost)

require.NotSame(t, client1, client2)
require.NotSame(t, client1, client3)
require.NotSame(t, client2, client3)
}

func castToTransport(client *nethttp.Client) *nethttp.Transport {
return client.Transport.(*ochttp.Transport).Base.(*nethttp.Transport)
}
41 changes: 4 additions & 37 deletions pkg/kncloudevents/message_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,10 @@ import (

"github.com/hashicorp/go-retryablehttp"
"github.com/rickb777/date/period"
"go.opencensus.io/plugin/ochttp"
"knative.dev/pkg/tracing/propagation/tracecontextb3"

duckv1 "knative.dev/eventing/pkg/apis/duck/v1"
)

const (
defaultRetryWaitMin = 1 * time.Second
defaultRetryWaitMax = 30 * time.Second
)

var noRetries = RetryConfig{
RetryMax: 0,
CheckRetry: func(ctx context.Context, resp *nethttp.Response, err error) (bool, error) {
Expand All @@ -46,41 +39,15 @@ var noRetries = RetryConfig{
},
}

// ConnectionArgs allow to configure connection parameters to the underlying
// HTTP Client transport.
type ConnectionArgs struct {
// MaxIdleConns refers to the max idle connections, as in net/http/transport.
MaxIdleConns int
// MaxIdleConnsPerHost refers to the max idle connections per host, as in net/http/transport.
MaxIdleConnsPerHost int
}

func (ca *ConnectionArgs) ConfigureTransport(transport *nethttp.Transport) {
if ca == nil {
return
}
transport.MaxIdleConns = ca.MaxIdleConns
transport.MaxIdleConnsPerHost = ca.MaxIdleConnsPerHost
}

type HttpMessageSender struct {
Client *nethttp.Client
Target string
}

func NewHttpMessageSender(connectionArgs *ConnectionArgs, target string) (*HttpMessageSender, error) {
// Add connection options to the default transport.
var base = nethttp.DefaultTransport.(*nethttp.Transport).Clone()
connectionArgs.ConfigureTransport(base)
// Add output tracing.
client := &nethttp.Client{
Transport: &ochttp.Transport{
Base: base,
Propagation: tracecontextb3.TraceContextEgress,
},
}

return &HttpMessageSender{Client: client, Target: target}, nil
// Deprecated: Don't use this anymore, now it has the same effect of NewHTTPMessageSenderWithTarget
// If you need to modify the connection args, use ConfigureConnectionArgs sparingly.
func NewHttpMessageSender(ca *ConnectionArgs, target string) (*HttpMessageSender, error) {
return &HttpMessageSender{Client: getClient(), Target: target}, nil
}

func (s *HttpMessageSender) NewCloudEventRequest(ctx context.Context) (*nethttp.Request, error) {
Expand Down
8 changes: 3 additions & 5 deletions pkg/mtbroker/filter/filter_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,11 @@ type FilterResult string
// NewHandler creates a new Handler and its associated MessageReceiver. The caller is responsible for
// Start()ing the returned Handler.
func NewHandler(logger *zap.Logger, triggerLister eventinglisters.TriggerLister, reporter StatsReporter, port int) (*Handler, error) {

connectionArgs := kncloudevents.ConnectionArgs{
kncloudevents.ConfigureConnectionArgs(&kncloudevents.ConnectionArgs{
MaxIdleConns: defaultMaxIdleConnections,
MaxIdleConnsPerHost: defaultMaxIdleConnectionsPerHost,
}

sender, err := kncloudevents.NewHttpMessageSender(&connectionArgs, "")
})
sender, err := kncloudevents.NewHttpMessageSender(nil, "")
if err != nil {
return nil, fmt.Errorf("failed to create message sender: %w", err)
}
Expand Down
12 changes: 7 additions & 5 deletions pkg/reconciler/inmemorychannel/dispatcher/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,22 @@ import (
"context"
"time"

"knative.dev/pkg/logging"

inmemorychannelreconciler "knative.dev/eventing/pkg/client/injection/reconciler/messaging/v1/inmemorychannel"

"go.uber.org/zap"
"k8s.io/client-go/tools/cache"
"knative.dev/pkg/configmap"
"knative.dev/pkg/controller"
"knative.dev/pkg/injection"
"knative.dev/pkg/logging"
pkgreconciler "knative.dev/pkg/reconciler"
tracingconfig "knative.dev/pkg/tracing/config"

"knative.dev/eventing/pkg/apis/eventing"
"knative.dev/eventing/pkg/channel"
"knative.dev/eventing/pkg/channel/swappable"
inmemorychannelinformer "knative.dev/eventing/pkg/client/injection/informers/messaging/v1/inmemorychannel"
inmemorychannelreconciler "knative.dev/eventing/pkg/client/injection/reconciler/messaging/v1/inmemorychannel"
"knative.dev/eventing/pkg/inmemorychannel"
"knative.dev/eventing/pkg/kncloudevents"
"knative.dev/eventing/pkg/tracing"
)

Expand Down Expand Up @@ -88,7 +87,10 @@ func NewController(

// Nothing to filer, enqueue all imcs if configmap updates.
noopFilter := func(interface{}) bool { return true }
resyncIMCs := configmap.TypeFilter(channel.EventDispatcherConfig{})(func(string, interface{}) {
resyncIMCs := configmap.TypeFilter(channel.EventDispatcherConfig{})(func(key string, val interface{}) {
conf := val.(channel.EventDispatcherConfig)
kncloudevents.ConfigureConnectionArgs(&conf.ConnectionArgs)

impl.FilteredGlobalResync(noopFilter, informer)
})
// Watch for configmap changes and trigger imc reconciliation by enqueuing imcs.
Expand Down

0 comments on commit e739e10

Please sign in to comment.