Skip to content

Commit

Permalink
fix(plugin): tyk
Browse files Browse the repository at this point in the history
  • Loading branch information
darkweak committed Sep 4, 2023
1 parent 5092d91 commit 0922ada
Show file tree
Hide file tree
Showing 15 changed files with 235 additions and 49 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/non-regression.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Install Go
uses: actions/setup-go@v3
with:
Expand All @@ -28,7 +28,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Install Go
uses: actions/setup-go@v3
with:
Expand All @@ -41,7 +41,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Install Go
uses: actions/setup-go@v3
with:
Expand All @@ -59,7 +59,7 @@ jobs:
steps:
-
name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4
-
name: Build the stack
run: docker network create your_network || true
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/plugin_template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
go-version: ${{ inputs.GO_VERSION }}
-
name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4
-
name: golangci-lint
uses: golangci/golangci-lint-action@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/plugins-master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
go-version: ${{ env.GO_VERSION }}
-
name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4
-
name: Install xcaddy
run: go install github.com/caddyserver/xcaddy/cmd/xcaddy@latest
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/plugins.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
go-version: '1.21'
-
name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4
-
name: Install xcaddy
run: go install github.com/caddyserver/xcaddy/cmd/xcaddy@latest
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
-
name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4
-
name: Set up QEMU
uses: docker/setup-qemu-action@v2
Expand Down Expand Up @@ -48,7 +48,7 @@ jobs:
run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
-
name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4
-
name: Set up QEMU
uses: docker/setup-qemu-action@v2
Expand Down Expand Up @@ -85,7 +85,7 @@ jobs:
go-version: ${{ env.GO_VERSION }}
-
name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: 0
-
Expand Down Expand Up @@ -113,7 +113,7 @@ jobs:
go-version: ${{ env.GO_VERSION }}
-
name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4
-
name: Generate Tyk amd64 artifacts
run: cd plugins/tyk && make vendor && docker compose -f docker-compose.yml.artifacts up
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/workflow_plugins_generator.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
go-version: '$go_version'
-
name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4
-
name: Install xcaddy
run: go install github.com/caddyserver/xcaddy/cmd/xcaddy@latest
Expand Down
2 changes: 1 addition & 1 deletion plugins/roadrunner/examples/Dockerfile.test
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ FROM ghcr.io/roadrunner-server/velox:latest as velox
ARG CURRENT_SHA
ARG GH_TOKEN
ENV CGO_ENABLED=0
ENV VERSION=v2023.2.2
ENV VERSION=v2023.3.0-beta.2
ENV TIME="$(date +%H:%M)"

RUN apk add git
Expand Down
8 changes: 4 additions & 4 deletions plugins/roadrunner/examples/configuration.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ build_args = [
]

[roadrunner]
ref = "v2023.2.2"
ref = "v2023.3.0-beta.2"

[github]
[github.token]
Expand All @@ -15,9 +15,9 @@ token = "GH_TOKEN"
[github.plugins]
logger = { ref = "v4.2.1", owner = "roadrunner-server", repository = "logger" }
cache = { ref = "CURRENT_SHA", owner = "darkweak", repository = "souin", folder = "/plugins/roadrunner", replace = "/opt/plugins/roadrunner" }
server = { ref = "v4.1.4", owner = "roadrunner-server", repository = "server" }
gzip = { ref = "v4.0.7", owner = "roadrunner-server", repository = "gzip" }
http = { ref = "v4.1.7", owner = "roadrunner-server", repository = "http" }
server = { ref = "v4.3.1", owner = "roadrunner-server", repository = "server" }
gzip = { ref = "v4.1.2", owner = "roadrunner-server", repository = "gzip" }
http = { ref = "v4.3.1", owner = "roadrunner-server", repository = "http" }

[log]
level = "debug"
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 38 additions & 5 deletions plugins/tyk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,32 @@ func SouinResponseHandler(rw http.ResponseWriter, rs *http.Response, rq *http.Re
if err == nil {
variedHeaders := rfc.HeaderAllCommaSepValues(res.Header)
cachedKey += rfc.GetVariedCacheKey(rq, variedHeaders)
if s.SouinBaseHandler.Storer.Set(cachedKey, response, currentMatchedURL, ma) == nil {
var wg sync.WaitGroup
mu := sync.Mutex{}
fails := []string{}
for _, storer := range s.SouinBaseHandler.Storers {
wg.Add(1)
go func(currentStorer storage.Storer) {
defer wg.Done()
if currentStorer.Set(cachedKey, response, currentMatchedURL, ma) == nil {
} else {
mu.Lock()
fails = append(fails, fmt.Sprintf("; detail=%s-INSERTION-ERROR", currentStorer.Name()))
mu.Unlock()
}
}(storer)
}

wg.Wait()
if len(fails) < len(s.SouinBaseHandler.Storers) {
go func(rs http.Response, key string) {
_ = s.SurrogateKeyStorer.Store(&rs, key)
}(res, cachedKey)
status += "; stored"
} else {
status += "; detail=STORAGE-INSERTION-ERROR"
}

if len(fails) > 0 {
status += strings.Join(fails, "")
}
}
} else {
Expand Down Expand Up @@ -157,7 +179,13 @@ func SouinRequestHandler(rw http.ResponseWriter, rq *http.Request) {
defer s.bufPool.Put(bufPool)
if !requestCc.NoCache {
validator := rfc.ParseRequest(rq)
response := s.SouinBaseHandler.Storer.Prefix(cachedKey, rq, validator)
var response *http.Response
for _, currentStorer := range s.SouinBaseHandler.Storers {
response = currentStorer.Prefix(cachedKey, rq, validator)
if response != nil {
break
}
}

if response != nil && rfc.ValidateCacheControl(response, requestCc) {
rfc.SetCacheStatusHeader(response)
Expand All @@ -170,7 +198,12 @@ func SouinRequestHandler(rw http.ResponseWriter, rq *http.Request) {
return
}
} else if response == nil && (requestCc.MaxStaleSet || requestCc.MaxStale > -1) {
response := s.SouinBaseHandler.Storer.Prefix(storage.StalePrefix+cachedKey, rq, validator)
for _, currentStorer := range s.SouinBaseHandler.Storers {
response = currentStorer.Prefix(storage.StalePrefix+cachedKey, rq, validator)
if response != nil {
break
}
}
if nil != response && rfc.ValidateCacheControl(response, requestCc) {
addTime, _ := time.ParseDuration(response.Header.Get(rfc.StoredTTLHeader))
rfc.SetCacheStatusHeader(response)
Expand Down
12 changes: 5 additions & 7 deletions plugins/tyk/override/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type MapHandler struct {
// GenerateHandlerMap generate the MapHandler
func GenerateHandlerMap(
configuration configurationtypes.AbstractConfigurationInterface,
storer storage.Storer,
storers []storage.Storer,
surrogateStorage providers.SurrogateInterface,
) *MapHandler {
hm := make(map[string]http.HandlerFunc)
Expand All @@ -28,12 +28,10 @@ func GenerateHandlerMap(
basePathAPIS = "/souin-api"
}

for _, endpoint := range Initialize(configuration, storer, surrogateStorage) {
for _, endpoint := range Initialize(configuration, storers, surrogateStorage) {
if endpoint.IsEnabled() {
shouldEnable = true
if e, ok := endpoint.(*SouinAPI); ok {
hm[basePathAPIS+endpoint.GetBasePath()] = e.HandleRequest
}
hm[basePathAPIS+endpoint.GetBasePath()] = endpoint.HandleRequest
}
}

Expand All @@ -45,6 +43,6 @@ func GenerateHandlerMap(
}

// Initialize contains all apis that should be enabled
func Initialize(c configurationtypes.AbstractConfigurationInterface, storer storage.Storer, surrogateStorage providers.SurrogateInterface) []EndpointInterface {
return []EndpointInterface{initializeSouin(c, storer, surrogateStorage)}
func Initialize(c configurationtypes.AbstractConfigurationInterface, storers []storage.Storer, surrogateStorage providers.SurrogateInterface) []EndpointInterface {
return []EndpointInterface{initializeSouin(c, storers, surrogateStorage)}
}
Loading

0 comments on commit 0922ada

Please sign in to comment.