-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.go
77 lines (71 loc) · 2.35 KB
/
lib.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package wleader
import (
"context"
"errors"
"fmt"
"github.com/gammazero/nexus/client"
"github.com/gammazero/nexus/wamp"
)
// BecameLeader is a callback function type which is used to inform the user
// that its session became the leader.
type BecameLeader func()
// LeaderElection is a function which performs a simple, but reliable leader election.
// It requires a WAMP router with shared registrations and the session meta API.
// It works by having a simple endpoint returning the callee's session ID and invocation_policy first.
// After the registration, the library calls the endpoint (for a single callee, i.e. the first instance)
// it returns its own session id.
// After the initial leader check, it registers on the MetaEventSessionOnLeave.
// When the leader leaves the WAMP realm, it queries for the new leader.
func LeaderElection(c *client.Client, groupIdentifier string, onBecameLeader BecameLeader) error {
activeSession := wamp.ID(0)
if err := c.Register(
fmt.Sprintf("ee.leader.%s", groupIdentifier),
func(_ context.Context, _ wamp.List, _, _ wamp.Dict) *client.InvokeResult {
return &client.InvokeResult{
Args: wamp.List{c.ID()},
}
},
wamp.Dict{
wamp.OptInvoke: wamp.InvokeFirst,
},
); err != nil {
return err
}
if res, err := c.Call(context.Background(), fmt.Sprintf("ee.leader.%s", groupIdentifier), nil, nil, nil, ""); err != nil {
return err
} else if len(res.Arguments) == 0 {
return errors.New("leader returned nothing")
} else if id, ok := wamp.AsID(res.Arguments[0]); !ok {
return errors.New("leader returned no session id")
} else {
activeSession = id
if id == c.ID() {
onBecameLeader()
}
}
if err := c.Subscribe(string(wamp.MetaEventSessionOnLeave), func(args wamp.List, _, _ wamp.Dict) {
// We have to use a goroutine here, since nexus calls topic handlers synchronously
go func() {
if len(args) == 0 {
return
}
if id, ok := wamp.AsID(args[0]); !ok || id != activeSession {
return
} else if res, err := c.Call(context.Background(), fmt.Sprintf("ee.leader.%s", groupIdentifier), nil, nil, nil, ""); err != nil {
return
} else if len(res.Arguments) == 0 {
return
} else if id, ok := wamp.AsID(res.Arguments[0]); !ok {
return
} else {
activeSession = id
if id == c.ID() {
onBecameLeader()
}
}
}()
}, nil); err != nil {
return err
}
return nil
}