diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..b73280a --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,12 @@ +# These are supported funding model platforms + +github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] +patreon: # Replace with a single Patreon username +open_collective: go-co-op +ko_fi: # Replace with a single Ko-fi username +tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel +community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry +liberapay: # Replace with a single Liberapay username +issuehunt: # Replace with a single IssueHunt username +otechie: # Replace with a single Otechie username +custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..ca534ca --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,18 @@ +# To get started with Dependabot version updates, you'll need to specify which +# package ecosystems to update and where the package manifests are located. +# Please see the documentation for all configuration options: +# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates + +version: 2 +updates: + # Maintain dependencies for GitHub Actions + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "weekly" + + # Maintain Go dependencies + - package-ecosystem: "gomod" + directory: "/" + schedule: + interval: "weekly" diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml new file mode 100644 index 0000000..78e7ba0 --- /dev/null +++ b/.github/workflows/codeql-analysis.yml @@ -0,0 +1,67 @@ +# For most projects, this workflow file will not need changing; you simply need +# to commit it to your repository. +# +# You may wish to alter this file to override the set of languages analyzed, +# or to provide custom queries or build logic. +# +# ******** NOTE ******** +# We have attempted to detect the languages in your repository. Please check +# the `language` matrix defined below to confirm you have the correct set of +# supported CodeQL languages. +# +name: "CodeQL" + +on: + push: + branches: [ main ] + pull_request: + # The branches below must be a subset of the branches above + branches: [ main ] + schedule: + - cron: '34 7 * * 1' + +jobs: + analyze: + name: Analyze + runs-on: ubuntu-latest + + strategy: + fail-fast: false + matrix: + language: [ 'go' ] + # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ] + # Learn more: + # https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + # Initializes the CodeQL tools for scanning. + - name: Initialize CodeQL + uses: github/codeql-action/init@v2 + with: + languages: ${{ matrix.language }} + # If you wish to specify custom queries, you can do so here or in a config file. + # By default, queries listed here will override any specified in a config file. + # Prefix the list here with "+" to use these queries and those in the config file. + # queries: ./path/to/local/query, your-org/your-repo/queries@main + + # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). + # If this step fails, then you should remove it and run the build manually (see below) + - name: Autobuild + uses: github/codeql-action/autobuild@v2 + + # ℹī¸ Command-line programs to run using the OS shell. + # 📚 https://git.io/JvXDl + + # ✏ī¸ If the Autobuild fails above, remove it and uncomment the following three lines + # and modify them (or add more) to build your code if your project + # uses a compiled language + + #- run: | + # make bootstrap + # make release + + - name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@v2 diff --git a/.github/workflows/go_test.yml b/.github/workflows/go_test.yml new file mode 100644 index 0000000..2cd8195 --- /dev/null +++ b/.github/workflows/go_test.yml @@ -0,0 +1,33 @@ +on: [push] +name: golangci-lint +jobs: + golangci: + strategy: + matrix: + go-version: + - "1.20" + name: lint and test + runs-on: ubuntu-latest + services: + etcd: + image: bitnami/etcd:3.5.5 + env: + ALLOW_NONE_AUTHENTICATION: yes + ETCD_ADVERTISE_CLIENT_URLS: http://127.0.0.1:2379 + ETCDCTL_API: 3 + ports: + - 2379:2379 + - 2380:2380 + steps: + - name: Checkout code + uses: actions/checkout@v3 + - name: golangci-lint + uses: golangci/golangci-lint-action@v3.7.0 + with: + version: v1.51.2 + - name: Install Go + uses: actions/setup-go@v4 + with: + go-version: ${{ matrix.go-version }} + - name: test + run: make test diff --git a/Makefile b/Makefile index 81a8a69..d6f18df 100644 --- a/Makefile +++ b/Makefile @@ -9,4 +9,4 @@ lint: @golangci-lint run test: - @go test -race -v $(GO_FLAGS) -count=1 $(GO_PKGS) + @go test -v $(GO_FLAGS) -count=1 $(GO_PKGS) diff --git a/elector.go b/elector.go index 3ff4b7c..79ee7e0 100644 --- a/elector.go +++ b/elector.go @@ -5,6 +5,7 @@ import ( "crypto/rand" "errors" "fmt" + mrand "math/rand" "os" "sync" "time" @@ -185,7 +186,7 @@ func (e *Elector) Start(electionPath string) error { continue } - if !e.isLeader { + if e.IsLeader(e.ctx) != nil { // is non-leader e.setLeader(val) e.logger("switch to leader, the current instance is leader") } @@ -202,6 +203,9 @@ func (e *Elector) Start(electionPath string) error { func getID() string { hostname, _ := os.Hostname() bs := make([]byte, 10) - rand.Read(bs) + _, err := rand.Read(bs) + if err != nil { + return fmt.Sprintf("%s-%d", hostname, mrand.Int63()) + } return fmt.Sprintf("%s-%x", hostname, bs) } diff --git a/elector_test.go b/elector_test.go index 9cf0666..f2c6b49 100644 --- a/elector_test.go +++ b/elector_test.go @@ -20,9 +20,16 @@ var ( func TestGocronWithElector(t *testing.T) { el, err := NewElector(context.Background(), testConfig, WithTTL(1)) assert.Equal(t, nil, err) - go el.Start(testElectionPath + "gocron_one") + go func() { + err := el.Start(testElectionPath + "gocron_one") + if err != nil { + t.Error(err) + } + }() - defer el.Stop() + defer func() { + _ = el.Stop() + }() done := make(chan struct{}, 1) counter := 0 @@ -62,7 +69,9 @@ func TestGocronWithMultipleElectors(t *testing.T) { el, err := NewElector(context.Background(), testConfig, WithTTL(1)) assert.Equal(t, nil, err) - go el.Start(testElectionPath + "gocron_multi") + go func() { + _ = el.Start(testElectionPath + "gocron_multi") + }() s := gocron.NewScheduler(time.UTC) s.WithDistributedElector(el) @@ -92,7 +101,7 @@ func TestGocronWithMultipleElectors(t *testing.T) { } for i := 0; i < workers; i++ { - elections[i].Stop() + _ = elections[i].Stop() schedulers[i].Stop() } } @@ -108,9 +117,9 @@ func TestElectorSingleAcquire(t *testing.T) { }() time.Sleep(2 * time.Second) - assert.Equal(t, nil, el.IsLeader(nil)) + assert.Equal(t, nil, el.IsLeader(context.Background())) assert.Equal(t, el.GetLeaderID(), el.GetID()) - el.Stop() + _ = el.Stop() select { case <-sig: @@ -119,7 +128,7 @@ func TestElectorSingleAcquire(t *testing.T) { } // after elector.stop, current instance is not leader - assert.Equal(t, ErrNonLeader, el.IsLeader(nil)) + assert.Equal(t, ErrNonLeader, el.IsLeader(context.Background())) } func TestElectorMultipleAcquire(t *testing.T) { @@ -142,7 +151,7 @@ func TestElectorMultipleAcquire(t *testing.T) { var leaderCounter int for _, el := range elections { - if el.IsLeader(nil) == nil { + if el.IsLeader(context.Background()) == nil { leaderCounter++ } } @@ -152,7 +161,7 @@ func TestElectorMultipleAcquire(t *testing.T) { // stop all electors for _, el := range elections { - el.Stop() + _ = el.Stop() } } @@ -192,7 +201,7 @@ func TestElectorAcquireRace(t *testing.T) { for idx, el := range elections { last := len(elections) - 1 - el.Stop() + _ = el.Stop() time.Sleep(3 * time.Second) if idx == last { @@ -206,7 +215,7 @@ func TestElectorAcquireRace(t *testing.T) { func TestElectorStop(t *testing.T) { el, err := NewElector(context.Background(), testConfig) assert.Equal(t, nil, err) - el.Stop() + _ = el.Stop() err = el.Start(testElectionPath) assert.Equal(t, err, ErrClosed) } diff --git a/example/main.go b/example/main.go index 1a7c4b0..a71e37b 100644 --- a/example/main.go +++ b/example/main.go @@ -37,8 +37,8 @@ func main() { s := gocron.NewScheduler(time.UTC) s.WithDistributedElector(el) - s.Every("1s").Do(func() { - if el.IsLeader(context.TODO()) == nil { + _, _ = s.Every("1s").Do(func() { + if el.IsLeader(context.Background()) == nil { fmt.Println("the current instance is leader") } else { fmt.Println("the current leader is", el.GetLeaderID())