Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POC: purego plugins (not for merge) #6520

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ build:

.PHONY: build-linux
build-linux:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GOBUILD) -tags "$(TAGS)" -o $(BINARY_LINUX) -v .
CGO_ENABLED=1 GOOS=linux GOARCH=amd64 $(GOBUILD) -ldflags '-extldflags "-static"' .

.PHONY: install
install:
Expand Down
18 changes: 0 additions & 18 deletions ci/goreleaser/goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,6 @@ builds:
goarch:
- arm64
binary: tyk
- id: std-s390x
flags:
- -tags=ignore
- -trimpath
- -tags=goplugin
ldflags:
- -X github.com/TykTechnologies/tyk/internal/build.Version={{.Version}}
- -X github.com/TykTechnologies/tyk/internal/build.Commit={{.FullCommit}}
- -X github.com/TykTechnologies/tyk/internal/build.BuildDate={{.Date}}
- -X github.com/TykTechnologies/tyk/internal/build.BuiltBy=goreleaser
env:
- CC=s390x-linux-gnu-gcc
goos:
- linux
goarch:
- s390x
binary: tyk
nfpms:
- id: std
vendor: "Tyk Technologies Ltd"
Expand All @@ -84,7 +67,6 @@ nfpms:
builds:
- std
- std-arm64
- std-s390x
formats:
- deb
- rpm
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ require (
github.com/TykTechnologies/kin-openapi v0.90.0
github.com/TykTechnologies/opentelemetry v0.0.21
github.com/alecthomas/kingpin/v2 v2.4.0
github.com/ebitengine/purego v0.7.1
github.com/go-redis/redismock/v9 v9.2.0
github.com/goccy/go-json v0.10.3
github.com/google/go-cmp v0.6.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ github.com/eapache/go-xerial-snappy v0.0.0-20230731223053-c322873962e3 h1:Oy0F4A
github.com/eapache/go-xerial-snappy v0.0.0-20230731223053-c322873962e3/go.mod h1:YvSRo5mw33fLEx1+DlK6L2VV43tJt5Eyel9n9XBcR+0=
github.com/eapache/queue v1.1.0 h1:YOEu7KNc61ntiQlcEeUIoDTJ2o8mQznoNvUhiigpIqc=
github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I=
github.com/ebitengine/purego v0.7.1 h1:6/55d26lG3o9VCZX8lping+bZcmShseiqlh2bnUDiPA=
github.com/ebitengine/purego v0.7.1/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ=
github.com/eclipse/paho.mqtt.golang v1.2.0 h1:1F8mhG9+aO5/xpdtFkW4SxOJB67ukuDC3t2y2qayIX0=
github.com/eclipse/paho.mqtt.golang v1.2.0/go.mod h1:H9keYFcgq3Qr5OUJm/JZI/i6U7joQ8SYLhZwfeOo6Ts=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
Expand Down
33 changes: 0 additions & 33 deletions goplugin/analyticsplugin.go

This file was deleted.

53 changes: 35 additions & 18 deletions goplugin/goplugin.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
//go:build goplugin
// +build goplugin

package goplugin

import (
"errors"
"net/http"
"plugin"

"github.com/TykTechnologies/tyk-pump/analytics"

"github.com/TykTechnologies/tyk/internal/errors"
"github.com/TykTechnologies/tyk/internal/plugin"
)

const symbolPrefix = "github.com/TykTechnologies/tyk/test/goplugins."

// GetSymbol only tests plugin loading. Used in tyk plugin cli.
// Don't encourage internal use.
func GetSymbol(modulePath string, symbol string) (interface{}, error) {
// try to load plugin
loadedPlugin, err := plugin.Open(modulePath)
Expand All @@ -26,31 +30,44 @@ func GetSymbol(modulePath string, symbol string) (interface{}, error) {
}

func GetHandler(modulePath string, symbol string) (http.HandlerFunc, error) {
funcSymbol, err := GetSymbol(modulePath, symbol)
loadedPlugin, err := plugin.Open(modulePath)
if err != nil {
return nil, err
}

// try to cast symbol to real func
pluginHandler, ok := funcSymbol.(func(http.ResponseWriter, *http.Request))
if !ok {
return nil, errors.New("could not cast function symbol to http.HandlerFunc")
}
var handler func(http.ResponseWriter, *http.Request)

return pluginHandler, nil
if err := loadedPlugin.As(&handler, symbolPrefix+symbol); err != nil {
return nil, errors.Wrap(err, "could not cast function symbol to http.HandlerFunc function")
}
return handler, nil
}

func GetResponseHandler(modulePath string, symbol string) (func(rw http.ResponseWriter, res *http.Response, req *http.Request), error) {
funcSymbol, err := GetSymbol(modulePath, symbol)
loadedPlugin, err := plugin.Open(modulePath)
if err != nil {
return nil, err
}

// try to cast symbol to real func
respPluginHandler, ok := funcSymbol.(func(rw http.ResponseWriter, res *http.Response, req *http.Request))
if !ok {
return nil, errors.New("could not cast function symbol to TykResponseHandler")
var handler func(rw http.ResponseWriter, res *http.Response, req *http.Request)

if err := loadedPlugin.As(&handler, symbolPrefix+symbol); err != nil {
return nil, errors.Wrap(err, "could not cast function symbol to TykResponseHandler function")
}
return handler, nil
}

func GetAnalyticsHandler(name string, symbol string) (func(record *analytics.AnalyticsRecord), error) {
// try to load plugin
loadedPlugin, err := plugin.Open(name)
if err != nil {
return nil, err
}

return respPluginHandler, nil
var handler func(record *analytics.AnalyticsRecord)

if err := loadedPlugin.As(&handler, symbolPrefix+symbol); err != nil {
return nil, errors.Wrap(err, "could not cast function symbol to AnalyticsPlugin function")
}
return handler, nil
}
3 changes: 0 additions & 3 deletions goplugin/mw_go_plugin_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//go:build goplugin
// +build goplugin

package goplugin_test

import (
Expand Down
15 changes: 0 additions & 15 deletions goplugin/no_analyticsplugin.go

This file was deleted.

25 changes: 0 additions & 25 deletions goplugin/no_goplugin.go

This file was deleted.

8 changes: 8 additions & 0 deletions internal/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package errors

import (
"errors"
"fmt"
"strings"
)

Expand All @@ -24,3 +25,10 @@ func Formatter(errs []error) string {

return result.String()
}

func Wrap(err error, message string) error {
if err == nil {
return err
}
return fmt.Errorf("%s: %w", message, err)
}
Loading
Loading