// NewSession gets the leased session for a client. func NewSession(client *v3.Client) (*Session, error) { clientSessions.mu.Lock() defer clientSessions.mu.Unlock() if s, ok := clientSessions.sessions[client]; ok { return s, nil } resp, err := client.Create(context.TODO(), sessionTTL) if err != nil { return nil, err } id := lease.LeaseID(resp.ID) ctx, cancel := context.WithCancel(context.Background()) keepAlive, err := client.KeepAlive(ctx, id) if err != nil || keepAlive == nil { return nil, err } donec := make(chan struct{}) s := &Session{client: client, id: id, cancel: cancel, donec: donec} clientSessions.sessions[client] = s // keep the lease alive until client error or cancelled context go func() { defer func() { clientSessions.mu.Lock() delete(clientSessions.sessions, client) clientSessions.mu.Unlock() close(donec) }() for range keepAlive { // eat messages until keep alive channel closes } }() return s, nil }