Skip to content

Commit

Permalink
Configure maximum number of concurrent requests via same AppServer
Browse files Browse the repository at this point in the history
  • Loading branch information
langbeinmovio committed Mar 4, 2021
1 parent b9b289d commit 71d99f1
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 9 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ RUN apt-get update && apt-get install -y --no-install-recommends default-mysql-c

ENV GO111MODULE=off

RUN git clone https://github.com/golang/sync $GOPATH/src/golang.org/x/sync

ENTRYPOINT [ "go", "test", "-v", "." ]
46 changes: 40 additions & 6 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
"strings"
)

type settings struct {
MaxAppServerConnections int64
}

type database struct {
AppServer string
DbServer string
Expand All @@ -18,9 +22,8 @@ type database struct {
SQLType string
}

func mustReadDatabasesConfigFile() map[string]database {
func getConfigPaths(fileName string) []string {
var paths []string
databases := map[string]database{}

usr, err := user.Current()
if err != nil {
Expand All @@ -33,29 +36,43 @@ func mustReadDatabasesConfigFile() map[string]database {
if xdgHome == "" {
xdgHome = fmt.Sprintf("%v/.config/", home)
}
xdgHome += "sql/.databases.json"
xdgHome += fmt.Sprintf("sql/%v", fileName)

paths = append(paths, xdgHome)

xdgConfigDirs := strings.Split(os.Getenv("XDG_CONFIG_DIRS"), ":")
xdgConfigDirs = append(xdgConfigDirs, "/etc/xdg")
for _, d := range xdgConfigDirs {
if d != "" {
paths = append(paths, fmt.Sprintf("%v/sql/.databases.json", d))
paths = append(paths, fmt.Sprintf("%v/sql/%v", d, fileName))
}
}

paths = append(paths, fmt.Sprintf("%v/.databases.json", home))
paths = append(paths, fmt.Sprintf("%v/%v", home, fileName))
return paths
}

func readFileContent(paths []string) ([]byte, error) {
var byts []byte
var err error
for _, p := range paths {
if byts, err = ioutil.ReadFile(p); err != nil {
continue
}
break
}
return byts, err
}

func mustReadDatabasesConfigFile() map[string]database {
databases := map[string]database{}

fileName := ".databases.json"
paths := getConfigPaths(fileName)
byts, err := readFileContent(paths)
if err != nil {
usage("Couldn't find .databases.json in the following paths [%v]. err=%v", paths, err)
usage("Couldn't find .%v in the following paths [%v]. err=%v", fileName, paths, err)

}

err = json.Unmarshal(byts, &databases)
Expand All @@ -69,3 +86,20 @@ func mustReadDatabasesConfigFile() map[string]database {

return databases
}

func readSettingsFile() *settings {
s := new(settings)
fileName := ".settings.json"
paths := getConfigPaths(fileName)
byts, err := readFileContent(paths)
if err == nil {
err = json.Unmarshal(byts, s)
if err != nil {
usage("Found but couldn't JSON unmarshal %v. Looked like this:\n\n%v\n\nerr=%v", fileName, string(byts), err)
}
}
if s.MaxAppServerConnections == 0 {
s.MaxAppServerConnections = 5
}
return s
}
22 changes: 20 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"log"
"os"
"sync"

"golang.org/x/sync/semaphore"
)

func main() {
Expand All @@ -28,6 +30,7 @@ func main() {
}

databases := mustReadDatabasesConfigFile()
settings := readSettingsFile()

if len(os.Args[1:]) == 0 {
usage("Target database unspecified; where should I run the query?")
Expand Down Expand Up @@ -56,10 +59,10 @@ func main() {
usage("No SQL to run. Exiting.")
}

os.Exit(_main(databases, databasesArgs, query, newThreadSafePrintliner(os.Stdout).println))
os.Exit(_main(settings, databases, databasesArgs, query, newThreadSafePrintliner(os.Stdout).println))
}

func _main(databases map[string]database, databasesArgs []string, query string, println func(string)) int {
func _main(settings *settings, databases map[string]database, databasesArgs []string, query string, println func(string)) int {
targetDatabases := []string{}
for _, k := range databasesArgs {
if _, ok := databases[k]; k != "all" && !ok {
Expand All @@ -81,12 +84,27 @@ func _main(databases map[string]database, databasesArgs []string, query string,
var wg sync.WaitGroup
wg.Add(len(targetDatabases))

appServerSemaphors := make(map[string]*semaphore.Weighted)
for _, k := range targetDatabases {
var appServer = databases[k].AppServer
if appServer != "" && appServerSemaphors[appServer] == nil {
appServerSemaphors[appServer] = semaphore.NewWeighted(settings.MaxAppServerConnections)
}
}

sqlRunner := mustNewSQLRunner(quitContext, println, query, len(targetDatabases) > 1)

returnCode := 0
for _, k := range targetDatabases {
go func(db database, k string) {
defer wg.Done()
if db.AppServer != "" {
fmt.Print("Aquiring lock from app server", db.AppServer, "\n")
var sem = appServerSemaphors[db.AppServer]
sem.Acquire(quitContext, 1)
fmt.Print("Aquired lock from app server", db.AppServer, "\n")
defer sem.Release(1)
}
if r := sqlRunner.runSQL(db, k); !r {
returnCode = 1
}
Expand Down
4 changes: 3 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ var mysqlTests = tests{{
},
}

var testSettings = settings{MaxAppServerConnections: 5}

func Test_MySQL(t *testing.T) {
awaitDB(mySQL, t)

Expand Down Expand Up @@ -196,7 +198,7 @@ func runTests(ts tests, testConfig testConfig, t *testing.T) {
for _, tc := range ts {
t.Run(tc.name, func(t *testing.T) {
var buf = bytes.Buffer{}
_main(testConfig, tc.targetDBs, tc.query, newThreadSafePrintliner(&buf).println)
_main(&testSettings, testConfig, tc.targetDBs, tc.query, newThreadSafePrintliner(&buf).println)
var actual = strings.Split(buf.String(), "\n")
sort.Strings(actual)
if !reflect.DeepEqual(tc.expected, actual) {
Expand Down

0 comments on commit 71d99f1

Please sign in to comment.