Skip to content

Commit

Permalink
src: revert golobal dialer (#716)
Browse files Browse the repository at this point in the history
Signed-off-by: Matej Vasek <mvasek@redhat.com>
  • Loading branch information
matejvasek authored Dec 10, 2021
1 parent a13f897 commit 6c67be0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
21 changes: 11 additions & 10 deletions cloudevents/emitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cloudevents
import (
"context"
"fmt"
nethttp "net/http"

cloudevents "github.com/cloudevents/sdk-go/v2"
"github.com/cloudevents/sdk-go/v2/client"
Expand All @@ -24,6 +25,7 @@ type Emitter struct {
Id string
Data string
ContentType string
Transport nethttp.RoundTripper
}

func NewEmitter() *Emitter {
Expand All @@ -33,14 +35,21 @@ func NewEmitter() *Emitter {
Id: uuid.NewString(),
Data: "",
ContentType: event.TextPlain,
Transport: nethttp.DefaultTransport,
}
}

func (e *Emitter) Emit(ctx context.Context, endpoint string) (err error) {
c, err := newClient(endpoint)
p, err := http.New(http.WithTarget(endpoint), http.WithRoundTripper(e.Transport))
if err != nil {
return
return err
}

c, err := client.New(p)
if err != nil {
return err
}

evt := event.Event{
Context: event.EventContextV1{
Type: e.Type,
Expand All @@ -60,11 +69,3 @@ func (e *Emitter) Emit(ctx context.Context, endpoint string) (err error) {
}
return nil
}

func newClient(target string) (c client.Client, err error) {
p, err := http.New(http.WithTarget(target))
if err != nil {
return
}
return client.New(p)
}
9 changes: 9 additions & 0 deletions cmd/emit.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"context"
"errors"
"io/ioutil"
"net/http"

"github.com/google/uuid"
"github.com/ory/viper"
"github.com/spf13/cobra"
fn "knative.dev/kn-plugin-func"
"knative.dev/kn-plugin-func/cloudevents"
fnhttp "knative.dev/kn-plugin-func/http"
"knative.dev/kn-plugin-func/knative"
)

Expand All @@ -25,6 +27,9 @@ func newEmitClient(cfg emitConfig) (*fn.Client, error) {
e.Type = cfg.Type
e.ContentType = cfg.ContentType
e.Data = cfg.Data
if e.Transport != nil {
e.Transport = cfg.Transport
}
if cfg.File != "" {
// See config.Validate for --Data and --file exclusivity enforcement
b, err := ioutil.ReadFile(cfg.File)
Expand Down Expand Up @@ -106,6 +111,9 @@ func runEmit(cmd *cobra.Command, _ []string, clientFn emitClientFn) (err error)
}

// Instantiate a client based on the final value of config
transport := fnhttp.NewRoundTripper()
defer transport.Close()
config.Transport = transport
client, err := clientFn(config)
if err != nil {
return err
Expand Down Expand Up @@ -175,6 +183,7 @@ type emitConfig struct {
ContentType string
Sink string
Verbose bool
Transport http.RoundTripper
}

func newEmitConfig() emitConfig {
Expand Down
8 changes: 0 additions & 8 deletions cmd/func/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ package main

import (
"context"
"net/http"
"os"
"os/signal"
"syscall"

funcHttp "knative.dev/kn-plugin-func/http"

"knative.dev/kn-plugin-func/cmd"
)

Expand All @@ -17,11 +14,6 @@ import (
var date, vers, hash string

func main() {

rt := funcHttp.NewRoundTripper()
http.DefaultTransport = rt
defer rt.Close()

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

Expand Down

0 comments on commit 6c67be0

Please sign in to comment.