{"id":19659734,"url":"https://github.com/lrita/go-sql-coordinate","last_synced_at":"2026-05-02T05:37:43.188Z","repository":{"id":78945314,"uuid":"128174536","full_name":"lrita/go-sql-coordinate","owner":"lrita","description":null,"archived":false,"fork":false,"pushed_at":"2018-04-11T15:40:25.000Z","size":5,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-10T01:27:14.398Z","etag":null,"topics":["coordinate","election","go","mysql"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lrita.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-04-05T07:46:11.000Z","updated_at":"2018-04-11T15:40:26.000Z","dependencies_parsed_at":"2023-03-12T06:15:54.572Z","dependency_job_id":null,"html_url":"https://github.com/lrita/go-sql-coordinate","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lrita%2Fgo-sql-coordinate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lrita%2Fgo-sql-coordinate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lrita%2Fgo-sql-coordinate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lrita%2Fgo-sql-coordinate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lrita","download_url":"https://codeload.github.com/lrita/go-sql-coordinate/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240968778,"owners_count":19886372,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["coordinate","election","go","mysql"],"created_at":"2024-11-11T15:44:14.979Z","updated_at":"2026-05-02T05:37:38.168Z","avatar_url":"https://github.com/lrita.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# go-sql-coordinate\nIn some cases, we have some instances in a cluster and we want run a special function\nin only one instance. We want not hardcode the instance, because it maybe crash. So we\nwant those instances in the cluster can auto elect a leader. Usually, we can use etcd/consul\nto do this. But some time we only use mysql in the code. We want not maintain a etcd/consul\ncluster only for a special function. We can use mysql do this.\n\n# example\n```go\npackage main\n\nimport (\n    \"database/sql\"\n    \"log\"\n\n    \"github.com/go-sql-driver/mysql\"\n    \"github.com/lrita/go-sql-coordinate/keepalived\"\n)\n\nfunc main() {\n    // Recommend set a timeout with mysql, which will make it sensitive when network broken down.\n    db, err := sql.Open(\"mysql\", \"root@tcp(127.0.0.1:3306)/test?charset=utf8\u0026parseTime=True\u0026loc=Local\u0026timeout=1s\u0026writeTimeout=3s\u0026readTimeout=3s\")\n    if err != nil {\n        log.Fatal(err)\n    }\n\n    tablename := \"test_election\"\n    session := \"127.0.0.1:80\"\n    if err := keepalived.CreateTable(tablename, db); err != nil {\n        log.Fatal(err)\n    }\n\n    keepa, err := keepalived.NewKeepalived(keepalived.Config{\n        Table:         tablename,\n        Election:      \"cluster_leader\",\n        Session:       session,\n        EpochChanSize: 10,\n        DB:            db,\n    })\n    if err != nil {\n        log.Fatal(err)\n    }\n\n    epoch := keepa.Election()\n    go func() {\n        for e := range epoch {\n            if e.Session == session {\n                log.Printf(\"I am become the leader of cluster_leader\")\n            } else {\n                log.Printf(\"leader change to %s\", e.Session)\n            }\n        }\n    }()\n\n    // do something...\n    keepa.Exit()\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flrita%2Fgo-sql-coordinate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flrita%2Fgo-sql-coordinate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flrita%2Fgo-sql-coordinate/lists"}