Skip to content

Instantly share code, notes, and snippets.

@mariokostelac
Created August 25, 2014 09:17
Show Gist options
  • Save mariokostelac/b21d88b6e1a9d1aab46a to your computer and use it in GitHub Desktop.
Save mariokostelac/b21d88b6e1a9d1aab46a to your computer and use it in GitHub Desktop.
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