{"id":39176283,"url":"https://github.com/randree/multibase","last_synced_at":"2026-01-17T22:28:43.760Z","repository":{"id":57623733,"uuid":"364006443","full_name":"randree/multibase","owner":"randree","description":"Multinode database connector for GROM","archived":false,"fork":false,"pushed_at":"2024-07-08T19:22:54.000Z","size":81,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-11-15T07:53:20.406Z","etag":null,"topics":["bitnami","go","golang","gorm","postgresql"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/randree.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-05-03T17:21:05.000Z","updated_at":"2024-07-08T19:19:46.000Z","dependencies_parsed_at":"2022-09-12T08:40:28.296Z","dependency_job_id":null,"html_url":"https://github.com/randree/multibase","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/randree/multibase","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/randree%2Fmultibase","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/randree%2Fmultibase/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/randree%2Fmultibase/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/randree%2Fmultibase/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/randree","download_url":"https://codeload.github.com/randree/multibase/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/randree%2Fmultibase/sbom","scorecard":{"id":761421,"data":{"date":"2025-08-11","repo":{"name":"github.com/randree/multibase","commit":"515c659972ec6fb4ed78dbb0f1fe86f69af7d489"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":1.7,"checks":[{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Code-Review","score":0,"reason":"Found 0/21 approved changesets -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Dangerous-Workflow","score":-1,"reason":"no workflows found","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Token-Permissions","score":-1,"reason":"No tokens found","details":null,"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"SAST","score":0,"reason":"no SAST tool detected","details":["Warn: no pull requests merged into dev branch"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}},{"name":"Pinned-Dependencies","score":-1,"reason":"no dependencies found","details":null,"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE:0","Info: FSF or OSI recognized license: MIT License: LICENSE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'main'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}},{"name":"Vulnerabilities","score":0,"reason":"10 existing vulnerabilities detected","details":["Warn: Project is vulnerable to: GO-2024-2606 / GHSA-7jwh-3vrq-q3m8 / GHSA-mrww-27vc-gghv","Warn: Project is vulnerable to: GO-2024-2605 / GHSA-m7wr-2xf7-cm9p","Warn: Project is vulnerable to: GO-2022-0968 / GHSA-gwc9-m7rh-j2ww","Warn: Project is vulnerable to: GO-2021-0356 / GHSA-8c26-wmh5-6g9v","Warn: Project is vulnerable to: GO-2024-2961","Warn: Project is vulnerable to: GO-2023-2402 / GHSA-45x7-px36-x8w8","Warn: Project is vulnerable to: GO-2024-3321 / GHSA-v778-237x-gjrc","Warn: Project is vulnerable to: GO-2025-3487 / GHSA-hcg3-q754-cr77","Warn: Project is vulnerable to: GO-2021-0113 / GHSA-ppp9-7jff-5vj2","Warn: Project is vulnerable to: GO-2022-1059 / GHSA-69ch-w2m2-3vjp"],"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}}]},"last_synced_at":"2025-08-22T23:35:46.915Z","repository_id":57623733,"created_at":"2025-08-22T23:35:46.915Z","updated_at":"2025-08-22T23:35:46.915Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28509147,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-17T11:50:55.898Z","status":"ssl_error","status_checked_at":"2026-01-17T11:50:55.569Z","response_time":85,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bitnami","go","golang","gorm","postgresql"],"created_at":"2026-01-17T22:28:43.137Z","updated_at":"2026-01-17T22:28:43.746Z","avatar_url":"https://github.com/randree.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Multinode database connector for GROM\n\n[![MIT licensed][1]][2] [![Go Report Card][3]][4]\n\n\n[1]: https://img.shields.io/badge/license-MIT-blue.svg\n[2]: LICENSE\n[3]: https://goreportcard.com/badge/github.com/randree/multibase/v2\n[4]: https://goreportcard.com/report/github.com/randree/multibase/v2\n\nSimple module to access multiple (Postgres or other) database nodes. Databases are divided into ONE write (or master) and MANY read (or slave) nodes. Replication is handled by the database and is not part of this module. You can choose for example [bitnami/bitnami-docker-postgresql](https://github.com/bitnami/bitnami-docker-postgresql) docker containers. They take care of replication.\n\nA loss of a connection to any (including the master) of the nodes does not end up in a panic. Instead that node is marked as offline. If all read nodes are offline the the load will be redirected to the master. If the master is down no query can be processed but still if the nodes reconnect everything gets back to normal.\n\nThe distribution to read nodes is done randomly. \n\nUnder the hood this package uses [GROM hooks](https://gorm.io/docs/hooks.html). Similar to [go-gorm/dbresolver](https://github.com/go-gorm/dbresolver).\n\n## Example\n\nLets start with one write and two read nodes.\n\n```golang\n\nimport (\n...\n\t\"github.com/randree/multibase/v2\"\n...\n)\n\nfunc main() {\n\n\tlogger := logger.New(\n\t\tlog.New(os.Stdout, \"\\r\\n\", log.LstdFlags), // io writer\n\t\tlogger.Config{\n\t\t\tSlowThreshold:             time.Second,   // Slow SQL threshold\n\t\t\tLogLevel:                  logger.Silent, // Log level\n\t\t\tIgnoreRecordNotFoundError: true,          // Ignore ErrRecordNotFound error for logger\n\t\t\tColorful:                  false,         // Disable color\n\t\t},\n\t)\n\n\t// WRITE NODE\n\tnodeWrite := \u0026multibase.NodeConf{\n\t\tHost:              \"mycomputer\",\n\t\tPort:              9000,\n\t\tUser:              \"database_user\",\n\t\tPassword:          \"database_password\",\n\t\tSslmode:           \"disable\",\n\t\tTimeZone:\t\t   \"Asia/Shanghai\",\n\t\tDb:                \"testdb\",\n\t\tDbMaxOpenConns:    20,\n\t\tDbMaxIdleConns:    8,\n\t\tDbConnMaxLifetime: 1 * time.Hour,\n\t\tDbLogger:          logger,\n\t}\n\t// READ NODE 1\n\tnodeRead1 := \u0026multibase.NodeConf{\n\t\tHost:              \"mycomputer\",\n\t\tPort:              9001,\n\t\tUser:              \"database_user\", // User must be the master.\n\t\tPassword:          \"database_password\",\n\t\tSslmode:           \"disable\",\n\t\tTimeZone:\t\t   \"Asia/Shanghai\",\n\t\tDb:                \"testdb\",\n\t\tDbMaxOpenConns:    20,\n\t\tDbMaxIdleConns:    8,\n\t\tDbConnMaxLifetime: 1 * time.Hour,\n\t\tDbLogger:          logger,\n\t}\n\n\t// READ NODE 2\n\tnodeRead2 := \u0026multibase.NodeConf{\n\t\tHost:              \"mycomputer\",\n\t\tPort:              9002,\n\t\tUser:              \"database_user\",\n\t\tPassword:          \"database_password\",\n\t\tSslmode:           \"disable\",\n\t\tTimeZone:\t\t   \"Asia/Shanghai\",\n\t\tDb:                \"testdb\",\n\t\tDbMaxOpenConns:    20,\n\t\tDbMaxIdleConns:    8,\n\t\tDbConnMaxLifetime: 1 * time.Hour,\n\t\tDbLogger:          logger,\n\t}\n\n\t// OpenNode uses gorm.Open with DisableAutomaticPing: true\n\t// You can replace it by any other GORM opener\n\t// The result should be a *gorm.DB instance\n\tdbWrite, _ := multibase.OpenNode(nodeWrite) // Feel free to check err\n\tdbRead1, _ := multibase.OpenNode(nodeRead1)\n\tdbRead2, _ := multibase.OpenNode(nodeRead2)\n\n\t// Initiate multibase\n\t// At this stage NO actual connection is made\n\tmb := multibase.New(dbWrite, dbRead1, dbRead2)\n\n\t// The most important node is the write node.\n\t// We use the following lines of code to ping the write node and connect to it.\n\t// Even if no connection can be established, a panic does not occur.\n\tfor {\n\t\terr := mb.ConnectWriteNode()\n\t\tif err != nil {\n\t\t\tfmt.Println(err)\n\t\t} else {\n\t\t\tbreak\n\t\t}\n\t\ttime.Sleep(time.Millisecond * 1000) // You can choose the interval\n\t}\n\n\t// After the write node is set up, it is time to connect the read nodes\n\terr := mb.ConnectReadNodes()\n\tif err != nil {\n\t\tfmt.Println(err)\n\t}\n\n\t// After this is done, GetDatabaseReplicaSet binds all nodes to a GORM database\n\t// All read queries are forwarded to the read nodes.\n\tdb := mb.GetDatabaseReplicaSet()\n\n\t// The StartReconnector is a go routine that checks the connection and \n\t// reconnects to the nodes if necessary.\n\tmb.StartReconnector(time.Second * 1)\n\n\t// Now we can use db as usual\n\t\ttype User struct {\n\t\tID   int `gorm:\"primarykey\"`\n\t\tName string\n\t}\n\tdb.AutoMigrate(\u0026User{})\n\n\tuser := \u0026User{}\n\tdb.FirstOrInit(user, User{Name: \"Jackx\"})\n\n\t...\n\n\t// To get some statistics use GetStatistics\n\tstatistics := mb.GetStatistics()\n\tfmt.Println(statistics)\n\n}\n\n```\nStatistics is a struct of following shape:\n```golang\ntype Statistic struct {\n\tonline               bool\n\tqueryCount           int64\n\terrorCount           int64\n\terrorConnectionCount int64\n}\n```\nThe output is similar to \n```\nmap[read0:{true 91214 3 0} read1:{true 98232 2 0} write:{true 234 0 0}]\n```\n\n\n## Try it out\n\nUse the `docker-compose.yml` to test the example.\n\n1. Open two consoles.  \n2. In the first one spin up nodes with\n```bash\n$ docker-compose up -d\n```\n3. Write a `main.go` like one the above, add\n```golang\n\tfor {\n\t\tfmt.Print(\"\\033[H\\033[2J\") // to clear the console after each cycle\n\t\tuser := []User{}\n\t\tdb.Find(\u0026user)\n\n\t\t// Print out statistics\n\t\tfmt.Println(mb.GetStatistics())\n\t\t// map[read0:{true 1125 0 0} read1:{true 1087 0 0} write:{true 0 0 0}]\n\n\t\ttime.Sleep(time.Millisecond * 100) // refresh each 100 ms\n\t}\n``` \n4. In the second console run go\n```bash\n$ go run main.go\n```\nthe result should be a similar to \n```\nmap[read0:{true 91214 3 0} read1:{true 98232 2 0} write:{true 234 0 0}]\n```\n5. While the go program is running stop a read node with\n```bash\n$ docker stop read2\n```\nand see that all queries are forwarded to `read0` (count starts at here at 0) and `read1` (read2) is marked as offline.\n\n6. Turn read2 on.\n```bash\n$ docker start read2\n```\nand the count is distributed between both read nodes.\n\n7. Play around. Stop the write node. Stop all nodes. Then start all with `docker start write read1 read2`.\n\nThe program should never stop or panic.\n\n## Replication\n\nNote that replication takes time in some cases. Try to avoid writing and reading the same data set in one routine.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frandree%2Fmultibase","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frandree%2Fmultibase","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frandree%2Fmultibase/lists"}