clientv3/concurrency/session.go
func NewSession(client *v3.Client, opts ...SessionOption) (*Session, error) {
	ops := &sessionOptions{ttl: defaultSessionTTL, ctx: client.Ctx()}
	for _, opt := range opts {
		opt(ops)
	}

	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)
	}

	ctx, cancel := context.WithCancel(ops.ctx)
	keepAlive, err := client.KeepAlive(ctx, id)
	if err != nil || keepAlive == nil {
		cancel()
		return nil, err
	}

	donec := make(chan struct{})
	s := &Session{client: client, opts: ops, id: id, cancel: cancel, donec: donec}

	// keep the lease alive until client error or cancelled context
	go func() {
		defer close(donec)
		for range keepAlive {
			// eat messages until keep alive channel closes
		}
	}()

	return s, nil
}
func NewSession(client *v3.Client, opts ...SessionOption) (*Session, error) {
	ops := &sessionOptions{ttl: defaultSessionTTL, ctx: client.Ctx()}
	for _, opt := range opts {
		opt(ops)
	}

	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)
	}

	ctx, cancel := context.WithCancel(ops.ctx)
	keepAlive, err := client.KeepAlive(ctx, id)
	if err != nil || keepAlive == nil {
		cancel()
		return nil, err
	}

	donec := make(chan struct{})
	s := &Session{client: client, opts: ops, id: id, cancel: cancel, donec: donec}

	// keep the lease alive until client error or cancelled context
	go func() {
		defer close(donec)
		for range keepAlive {
			// eat messages until keep alive channel closes
		}
	}()

	return s, nil
}
func NewSession ( client * v3 . Client , opts ... SessionOption ) ( * Session , error ) { ops := & sessionOptions { ttl : defaultSessionTTL , ctx : client . Ctx ( ) } for _ , opt := range opts { opt ( ops ) } 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 ) } ctx , cancel := context . WithCancel ( ops . ctx ) keepAlive , err := client . KeepAlive ( ctx , id ) if err != nil || keepAlive == nil { cancel ( ) return nil , err } donec := make ( chan struct { } ) s := & Session { client : client , opts : ops , id : id , cancel : cancel , donec : donec } // keep the lease alive until client error or cancelled context go func ( ) { defer close ( donec ) for range keepAlive { // eat messages until keep alive channel closes } } ( ) return s , nil }
// NewSession gets the leased session for a client.
NewSession gets the leased session for a client .