-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TT-10826] self trim oAuth sorted set (#5907)
<!-- Provide a general summary of your changes in the Title above --> Add a background job to self trim oAuth sorted set https://tyktech.atlassian.net/browse/TT-10826 <!-- Why is this change required? What problem does it solve? --> <!-- Please describe in detail how you tested your changes --> <!-- Include details of your testing environment, and the tests --> <!-- you ran to see how your change affects other areas of the code, etc. --> <!-- This information is helpful for reviewers and QA. --> <!-- What types of changes does your code introduce? Put an `x` in all the boxes that apply: --> - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Refactoring or add test (improvements in base code or adds test coverage to functionality) <!-- Go over all the following points, and put an `x` in all the boxes that apply --> <!-- If there are no documentation updates required, mark the item as checked. --> <!-- Raise up any additional concerns not covered by the checklist. --> - [ ] I ensured that the documentation is up to date - [ ] I explained why this PR updates go.mod in detail with reasoning why it's required - [ ] I would like a code coverage CI quality gate exception and have explained why --------- Co-authored-by: Tit Petric <tit.petric@monotek.net> Co-authored-by: Tit Petric <tit@tyk.io> (cherry picked from commit ee5dc29)
- Loading branch information
1 parent
43645f7
commit f2e8683
Showing
21 changed files
with
992 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/bin/bash | ||
set -e | ||
|
||
echo "Running benchmarks" | ||
|
||
|
||
go test -json -benchtime 30s -run='^$' -bench BenchmarkPurgeLapsedOAuthTokens github.com/TykTechnologies/tyk/gateway |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package config | ||
|
||
import "time" | ||
|
||
// Private contains configurations which are private, adding it to be part of config without exposing to customers. | ||
type Private struct { | ||
// OAuthTokensPurgeInterval specifies the interval at which lapsed tokens get purged. | ||
OAuthTokensPurgeInterval int `json:"-"` | ||
// OriginalPath is the path to the config file that is read. If | ||
// none was found, it's the path to the default config file that | ||
// was written. | ||
OriginalPath string `json:"-"` | ||
} | ||
|
||
// GetOAuthTokensPurgeInterval returns purge interval for lapsed OAuth tokens. | ||
func (p Private) GetOAuthTokensPurgeInterval() time.Duration { | ||
if p.OAuthTokensPurgeInterval != 0 { | ||
return time.Second * time.Duration(p.OAuthTokensPurgeInterval) | ||
} | ||
|
||
return time.Hour | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package config | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestPrivate_GetOAuthTokensPurgeInterval(t *testing.T) { | ||
t.Run("default value", func(t *testing.T) { | ||
p := Private{} | ||
assert.Equal(t, time.Hour, p.GetOAuthTokensPurgeInterval()) | ||
}) | ||
|
||
t.Run("custom value", func(t *testing.T) { | ||
p := Private{OAuthTokensPurgeInterval: 5} | ||
assert.Equal(t, time.Second*5, p.GetOAuthTokensPurgeInterval()) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package config | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
"runtime" | ||
"strconv" | ||
) | ||
|
||
// New produces a new config object by parsing | ||
// the default configuration for the values. | ||
func New() (*Config, error) { | ||
cfg := new(Config) | ||
|
||
cfgFile, err := findFile("tyk.conf") | ||
if err != nil { | ||
// Return cfg filled with environment | ||
// if we don't have a config file. | ||
if errors.Is(err, os.ErrNotExist) { | ||
err := FillEnv(cfg) | ||
return cfg, err | ||
} | ||
|
||
return nil, err | ||
} | ||
|
||
if err := Load([]string{cfgFile}, cfg); err != nil { | ||
return nil, err | ||
} | ||
|
||
return cfg, nil | ||
} | ||
|
||
func findFile(filename string) (string, error) { | ||
// Get folder in which current file lives | ||
_, testFile, _, _ := runtime.Caller(0) | ||
// Strip the filename and produce the dir | ||
currentDir := path.Dir(testFile) | ||
|
||
// Traverse the current directory and its parent directories | ||
for { | ||
// Check if the file exists in the current directory | ||
filePath := filepath.Join(currentDir, filename) | ||
_, err := os.Stat(filePath) | ||
if err == nil { | ||
// File found | ||
return filePath, nil | ||
} | ||
|
||
// Move to the parent directory | ||
parentDir := filepath.Dir(currentDir) | ||
if parentDir == currentDir { | ||
// No more parent directories remaining | ||
break | ||
} | ||
|
||
currentDir = parentDir | ||
} | ||
|
||
// File not found | ||
return "", os.ErrNotExist | ||
} | ||
|
||
// HostAddrs returns a sanitized list of hosts to connect to. | ||
func (config *StorageOptionsConf) HostAddrs() (addrs []string) { | ||
if len(config.Addrs) != 0 { | ||
addrs = config.Addrs | ||
} else { | ||
for h, p := range config.Hosts { | ||
addr := h + ":" + p | ||
addrs = append(addrs, addr) | ||
} | ||
} | ||
|
||
if len(addrs) == 0 && config.Port != 0 { | ||
addr := config.Host + ":" + strconv.Itoa(config.Port) | ||
addrs = append(addrs, addr) | ||
} | ||
|
||
return addrs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package config | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestHostAddrs(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
config StorageOptionsConf | ||
want []string | ||
}{ | ||
{ | ||
name: "empty", | ||
config: StorageOptionsConf{}, | ||
}, | ||
{ | ||
name: "addrs", | ||
config: StorageOptionsConf{ | ||
Addrs: []string{"host1:1234", "host2:5678"}, | ||
}, | ||
want: []string{"host1:1234", "host2:5678"}, | ||
}, | ||
{ | ||
name: "hosts map", | ||
config: StorageOptionsConf{ | ||
Hosts: map[string]string{ | ||
"host1": "1234", | ||
"host2": "5678", | ||
}, | ||
}, | ||
want: []string{"host1:1234", "host2:5678"}, | ||
}, | ||
{ | ||
name: "addrs and host maps", | ||
config: StorageOptionsConf{ | ||
Addrs: []string{"host1:1234", "host2:5678"}, | ||
Hosts: map[string]string{ | ||
"host3": "1234", | ||
"host4": "5678", | ||
}, | ||
}, | ||
want: []string{"host1:1234", "host2:5678"}, | ||
}, | ||
{ | ||
name: "host and port", | ||
config: StorageOptionsConf{ | ||
Host: "localhost", | ||
Port: 6379, | ||
}, | ||
want: []string{"localhost:6379"}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := tt.config.HostAddrs() | ||
assert.ElementsMatch(t, tt.want, got) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.