|
| 1 | +/* |
| 2 | +Copyright 2020 The Knative Authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package kncloudevents |
| 18 | + |
| 19 | +import ( |
| 20 | + nethttp "net/http" |
| 21 | + "sync" |
| 22 | + "time" |
| 23 | + |
| 24 | + "go.opencensus.io/plugin/ochttp" |
| 25 | + "knative.dev/pkg/tracing/propagation/tracecontextb3" |
| 26 | +) |
| 27 | + |
| 28 | +const ( |
| 29 | + defaultRetryWaitMin = 1 * time.Second |
| 30 | + defaultRetryWaitMax = 30 * time.Second |
| 31 | +) |
| 32 | + |
| 33 | +type holder struct { |
| 34 | + clientMutex sync.Mutex |
| 35 | + connectionArgs *ConnectionArgs |
| 36 | + client **nethttp.Client |
| 37 | +} |
| 38 | + |
| 39 | +var clientHolder = holder{} |
| 40 | + |
| 41 | +// The used HTTP client is a singleton, so the same http client is reused across all the application. |
| 42 | +// If connection args is modified, client is cleaned and a new one is created. |
| 43 | +func getClient() *nethttp.Client { |
| 44 | + clientHolder.clientMutex.Lock() |
| 45 | + defer clientHolder.clientMutex.Unlock() |
| 46 | + |
| 47 | + if clientHolder.client == nil { |
| 48 | + // Add connection options to the default transport. |
| 49 | + var base = nethttp.DefaultTransport.(*nethttp.Transport).Clone() |
| 50 | + clientHolder.connectionArgs.configureTransport(base) |
| 51 | + c := &nethttp.Client{ |
| 52 | + // Add output tracing. |
| 53 | + Transport: &ochttp.Transport{ |
| 54 | + Base: base, |
| 55 | + Propagation: tracecontextb3.TraceContextEgress, |
| 56 | + }, |
| 57 | + } |
| 58 | + clientHolder.client = &c |
| 59 | + } |
| 60 | + |
| 61 | + return *clientHolder.client |
| 62 | +} |
| 63 | + |
| 64 | +// ConfigureConnectionArgs configures the new connection args. |
| 65 | +// The existing client won't be affected, but a new one will be created. |
| 66 | +// Use sparingly, because it might lead to creating a lot of clients, none of them sharing their connection pool! |
| 67 | +func ConfigureConnectionArgs(ca *ConnectionArgs) { |
| 68 | + clientHolder.clientMutex.Lock() |
| 69 | + defer clientHolder.clientMutex.Unlock() |
| 70 | + |
| 71 | + // Check if same config |
| 72 | + if clientHolder.connectionArgs != nil && |
| 73 | + ca != nil && |
| 74 | + ca.MaxIdleConns == clientHolder.connectionArgs.MaxIdleConns && |
| 75 | + ca.MaxIdleConnsPerHost == clientHolder.connectionArgs.MaxIdleConnsPerHost { |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + if clientHolder.client != nil { |
| 80 | + // Let's try to clean up a bit the existing client |
| 81 | + // Note: this won't remove it nor close it |
| 82 | + (*clientHolder.client).CloseIdleConnections() |
| 83 | + |
| 84 | + // Setting client to nil |
| 85 | + clientHolder.client = nil |
| 86 | + } |
| 87 | + |
| 88 | + clientHolder.connectionArgs = ca |
| 89 | +} |
| 90 | + |
| 91 | +// ConnectionArgs allow to configure connection parameters to the underlying |
| 92 | +// HTTP Client transport. |
| 93 | +type ConnectionArgs struct { |
| 94 | + // MaxIdleConns refers to the max idle connections, as in net/http/transport. |
| 95 | + MaxIdleConns int |
| 96 | + // MaxIdleConnsPerHost refers to the max idle connections per host, as in net/http/transport. |
| 97 | + MaxIdleConnsPerHost int |
| 98 | +} |
| 99 | + |
| 100 | +func (ca *ConnectionArgs) configureTransport(transport *nethttp.Transport) { |
| 101 | + if ca == nil { |
| 102 | + return |
| 103 | + } |
| 104 | + transport.MaxIdleConns = ca.MaxIdleConns |
| 105 | + transport.MaxIdleConnsPerHost = ca.MaxIdleConnsPerHost |
| 106 | +} |
0 commit comments