Created
August 25, 2014 09:17
-
-
Save mariokostelac/b21d88b6e1a9d1aab46a to your computer and use it in GitHub Desktop.
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
package servercomm | |
import ( | |
"sync" | |
"time" | |
) | |
type Subscription struct { | |
sync.Mutex | |
id int64 | |
C chan PubSubMessage | |
closed bool | |
closedCh chan bool | |
} | |
func CreateSubscription() *Subscription { | |
return &Subscription{ | |
id: time.Now().UnixNano(), | |
C: make(chan (PubSubMessage)), | |
closedCh: make(chan bool), | |
} | |
} | |
// ID returns the subscription ID. | |
func (sub *Subscription) ID() int64 { | |
return sub.id | |
} | |
// Close closed the subscription and underlying channel. Operation is idempotent. | |
func (sub *Subscription) Close() { | |
sub.Lock() | |
defer sub.Unlock() | |
if sub.closed { | |
return | |
} | |
sub.closed = true | |
close(sub.closedCh) | |
close(sub.C) | |
} | |
// CloseNotify returns a channel that's passable when subscription in closed state. | |
func (sub *Subscription) CloseNotify() <-chan bool { | |
return sub.closedCh | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment