Skip to content

Commit

Permalink
upgrade to latest dependencies (#24)
Browse files Browse the repository at this point in the history
Signed-off-by: Knative Automation <[email protected]>
  • Loading branch information
knative-automation authored Nov 6, 2020
1 parent e4ca3b2 commit 8fcba13
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 41 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
k8s.io/api v0.18.8
k8s.io/apimachinery v0.18.8
k8s.io/client-go v11.0.1-0.20190805182717-6502b5e7b1b5+incompatible
knative.dev/eventing v0.18.1-0.20201103183104-b1706b6c2ddf
knative.dev/eventing v0.18.1-0.20201105172507-a6fc5408cb44
knative.dev/hack v0.0.0-20201103151104-3d5abc3a0075
knative.dev/pkg v0.0.0-20201103163404-5514ab0c1fdf
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1160,8 +1160,8 @@ k8s.io/kube-openapi v0.0.0-20200410145947-bcb3869e6f29/go.mod h1:F+5wygcW0wmRTnM
k8s.io/utils v0.0.0-20200324210504-a9aa75ae1b89/go.mod h1:sZAwmy6armz5eXlNoLmJcl4F1QuKu7sr+mFQ0byX7Ew=
k8s.io/utils v0.0.0-20200603063816-c1c6865ac451 h1:v8ud2Up6QK1lNOKFgiIVrZdMg7MpmSnvtrOieolJKoE=
k8s.io/utils v0.0.0-20200603063816-c1c6865ac451/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
knative.dev/eventing v0.18.1-0.20201103183104-b1706b6c2ddf h1:OHHyNK24URG8feO/TFedPzNt/8mHDJUI2p1G3hRW/DE=
knative.dev/eventing v0.18.1-0.20201103183104-b1706b6c2ddf/go.mod h1:jwhDgDvoscWE4jWF8cXh7yHfxJcK0mTawVKVfrSjnvg=
knative.dev/eventing v0.18.1-0.20201105172507-a6fc5408cb44 h1:vgz108o3Br0WfDysXMSD10lcVOl1ZIjkDQa8uFyaeq0=
knative.dev/eventing v0.18.1-0.20201105172507-a6fc5408cb44/go.mod h1:jwhDgDvoscWE4jWF8cXh7yHfxJcK0mTawVKVfrSjnvg=
knative.dev/hack v0.0.0-20201103151104-3d5abc3a0075 h1:YAgWplKIy4O5e3F5vUUECmXAAyZ0M5ymo6fCt1jeZhs=
knative.dev/hack v0.0.0-20201103151104-3d5abc3a0075/go.mod h1:PHt8x8yX5Z9pPquBEfIj0X66f8iWkWfR0S/sarACJrI=
knative.dev/pkg v0.0.0-20201103163404-5514ab0c1fdf h1:QwULgRwcv6R3Ya1GZlf/E1atcaGUNw4DKjxSQUfcR6U=
Expand Down
2 changes: 1 addition & 1 deletion vendor/knative.dev/eventing/pkg/adapter/v2/main_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func MainMessageAdapterWithContext(ctx context.Context, component string, ector
logger.Error("Error setting up trace publishing", zap.Error(err))
}

httpBindingsSender, err := kncloudevents.NewHTTPMessageSender(nil, env.GetSink())
httpBindingsSender, err := kncloudevents.NewHTTPMessageSenderWithTarget(env.GetSink())
if err != nil {
logger.Fatal("error building cloud event client", zap.Error(err))
}
Expand Down
106 changes: 106 additions & 0 deletions vendor/knative.dev/eventing/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
}
43 changes: 7 additions & 36 deletions vendor/knative.dev/eventing/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,19 @@ 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,
},
}
// 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 NewHTTPMessageSenderWithTarget(target)
}

return &HTTPMessageSender{Client: client, Target: target}, nil
func NewHTTPMessageSenderWithTarget(target string) (*HTTPMessageSender, error) {
return &HTTPMessageSender{Client: getClient(), Target: target}, nil
}

func (s *HTTPMessageSender) NewCloudEventRequest(ctx context.Context) (*nethttp.Request, error) {
Expand Down
2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@ k8s.io/utils/buffer
k8s.io/utils/integer
k8s.io/utils/pointer
k8s.io/utils/trace
# knative.dev/eventing v0.18.1-0.20201103183104-b1706b6c2ddf
# knative.dev/eventing v0.18.1-0.20201105172507-a6fc5408cb44
## explicit
knative.dev/eventing/pkg/adapter/v2
knative.dev/eventing/pkg/adapter/v2/test
Expand Down

0 comments on commit 8fcba13

Please sign in to comment.