Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Commit

Permalink
Backports and skip tracing (#959)
Browse files Browse the repository at this point in the history
* Update pingsource-mt-adapter.yaml

* Like on 0.18.3, we skip the tracing tests

Signed-off-by: Matthias Wessendorf <mwessend@redhat.com>

* [release-0.18] Retry on network failures (knative#4454) (knative#4457)

* Retry on network failures (knative#4454)

Signed-off-by: Pierangelo Di Pilato <pierangelodipilato@gmail.com>

* nethttp -> http

Signed-off-by: Pierangelo Di Pilato <pierangelodipilato@gmail.com>

* Backport knative#4465 (knative#4468)

Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>

* [0.18] Backport knative#4466 (knative#4471)

* Remove double invocations to responseWriter.WriteHeader in filter handler (knative#4466)

* Fix knative#4464

Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>

* Docs

Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>

* Moar tests

Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>

* Linting

Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>

* Nit with metrics

Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>

(cherry picked from commit a6fc540)
Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>

* Nit

Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>

* fixed wrong marshall in apiserversouece which will fix the missing ceOverrides extension  (knative#4477) (knative#4480)

* fixed wrong marshall

* fixed UT

* [0.18] Readyness probe in broker ingress (knative#4483)

* Fix knative#4473

Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>

* Massage the filter yaml

Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>

Co-authored-by: Matthias Wessendorf <mwessend@redhat.com>
Co-authored-by: Pierangelo Di Pilato <pierangelodipilato@gmail.com>
Co-authored-by: Francesco Guardiani <francescoguard@gmail.com>
Co-authored-by: capri-xiyue <52932582+capri-xiyue@users.noreply.github.com>
  • Loading branch information
5 people committed Nov 7, 2020
1 parent 0511208 commit e5c8a88
Show file tree
Hide file tree
Showing 16 changed files with 407 additions and 107 deletions.
1 change: 1 addition & 0 deletions cmd/mtbroker/ingress/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,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
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,18 @@ spec:
- name: filter
terminationMessagePolicy: FallbackToLogsOnError
image: ko://knative.dev/eventing/cmd/mtbroker/filter
livenessProbe:
readinessProbe: &probe
failureThreshold: 3
httpGet:
path: /healthz
port: 8080
scheme: HTTP
initialDelaySeconds: 5
periodSeconds: 2
successThreshold: 1
timeoutSeconds: 1
livenessProbe:
<<: *probe
initialDelaySeconds: 5
resources:
requests:
cpu: 100m
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,18 @@ spec:
- name: ingress
terminationMessagePolicy: FallbackToLogsOnError
image: ko://knative.dev/eventing/cmd/mtbroker/ingress
livenessProbe:
readinessProbe: &probe
failureThreshold: 3
httpGet:
path: /healthz
port: 8080
scheme: HTTP
initialDelaySeconds: 5
periodSeconds: 2
successThreshold: 1
timeoutSeconds: 1
livenessProbe:
<<: *probe
initialDelaySeconds: 5
resources:
requests:
cpu: 100m
Expand Down
1 change: 1 addition & 0 deletions config/core/deployments/pingsource-mt-adapter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ spec:
image: ko://knative.dev/eventing/cmd/mtping
env:
- name: SYSTEM_NAMESPACE
value: ''
valueFrom:
fieldRef:
apiVersion: v1
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)
}
45 changes: 6 additions & 39 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 Expand Up @@ -184,6 +151,6 @@ func RetryConfigFromDeliverySpec(spec duckv1.DeliverySpec) (RetryConfig, error)
return retryConfig, nil
}

func checkRetry(_ context.Context, resp *nethttp.Response, _ error) (bool, error) {
return resp != nil && resp.StatusCode >= 300, nil
func checkRetry(_ context.Context, resp *nethttp.Response, err error) (bool, error) {
return !(resp != nil && resp.StatusCode < 300), err
}
Loading

0 comments on commit e5c8a88

Please sign in to comment.