-
-
Notifications
You must be signed in to change notification settings - Fork 316
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
elector & locker were failing to send out when not leader (#688)
* elector & locker were failing to send out when not leader * update test to confirm non-active elector/locker are checked * clean up data race * try to make test more reliable
- Loading branch information
1 parent
c2f9575
commit ebec5e9
Showing
5 changed files
with
142 additions
and
26 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
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,73 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/go-co-op/gocron/v2" | ||
) | ||
|
||
var _ gocron.Elector = (*myElector)(nil) | ||
|
||
type myElector struct { | ||
num int | ||
leader bool | ||
} | ||
|
||
func (m myElector) IsLeader(_ context.Context) error { | ||
if m.leader { | ||
log.Printf("node %d is leader", m.num) | ||
return nil | ||
} | ||
log.Printf("node %d is not leader", m.num) | ||
return fmt.Errorf("not leader") | ||
} | ||
|
||
func main() { | ||
log.SetFlags(log.LstdFlags | log.Lmicroseconds) | ||
|
||
for i := 0; i < 3; i++ { | ||
go func(i int) { | ||
elector := &myElector{ | ||
num: i, | ||
} | ||
if i == 0 { | ||
elector.leader = true | ||
} | ||
|
||
scheduler, err := gocron.NewScheduler( | ||
gocron.WithDistributedElector(elector), | ||
) | ||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
|
||
_, err = scheduler.NewJob( | ||
gocron.DurationJob(time.Second), | ||
gocron.NewTask(func() { | ||
log.Println("run job") | ||
}), | ||
) | ||
|
||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
scheduler.Start() | ||
|
||
if i == 0 { | ||
time.Sleep(5 * time.Second) | ||
elector.leader = false | ||
} | ||
if i == 1 { | ||
time.Sleep(5 * time.Second) | ||
elector.leader = true | ||
} | ||
}(i) | ||
} | ||
|
||
select {} // wait forever | ||
} |
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