|
@@ -41,11 +41,14 @@ func NewSession(client *v3.Client, opts ...SessionOption) (*Session, error) {
|
|
|
opt(ops)
|
|
opt(ops)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- resp, err := client.Grant(ops.ctx, int64(ops.ttl))
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return nil, err
|
|
|
|
|
|
|
+ id := ops.leaseID
|
|
|
|
|
+ if id == v3.NoLease {
|
|
|
|
|
+ resp, err := client.Grant(ops.ctx, int64(ops.ttl))
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return nil, err
|
|
|
|
|
+ }
|
|
|
|
|
+ id = v3.LeaseID(resp.ID)
|
|
|
}
|
|
}
|
|
|
- id := v3.LeaseID(resp.ID)
|
|
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(ops.ctx)
|
|
ctx, cancel := context.WithCancel(ops.ctx)
|
|
|
keepAlive, err := client.KeepAlive(ctx, id)
|
|
keepAlive, err := client.KeepAlive(ctx, id)
|
|
@@ -98,8 +101,9 @@ func (s *Session) Close() error {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
type sessionOptions struct {
|
|
type sessionOptions struct {
|
|
|
- ttl int
|
|
|
|
|
- ctx context.Context
|
|
|
|
|
|
|
+ ttl int
|
|
|
|
|
+ leaseID v3.LeaseID
|
|
|
|
|
+ ctx context.Context
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// SessionOption configures Session.
|
|
// SessionOption configures Session.
|
|
@@ -115,6 +119,15 @@ func WithTTL(ttl int) SessionOption {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// WithLease specifies the existing leaseID to be used for the session.
|
|
|
|
|
+// This is useful in process restart scenario, for example, to reclaim
|
|
|
|
|
+// leadership from an election prior to restart.
|
|
|
|
|
+func WithLease(leaseID v3.LeaseID) SessionOption {
|
|
|
|
|
+ return func(so *sessionOptions) {
|
|
|
|
|
+ so.leaseID = leaseID
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
// WithContext assigns a context to the session instead of defaulting to
|
|
// WithContext assigns a context to the session instead of defaulting to
|
|
|
// using the client context. This is useful for canceling NewSession and
|
|
// using the client context. This is useful for canceling NewSession and
|
|
|
// Close operations immediately without having to close the client. If the
|
|
// Close operations immediately without having to close the client. If the
|