perkeep/pkg/blobserver/blobhub.go

188 lines
4.4 KiB
Go
Raw Normal View History

/*
Copyright 2011 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package blobserver
import (
2011-02-26 20:22:26 +00:00
"sync"
"time"
"camlistore.org/pkg/blob"
)
type BlobHub interface {
// For new blobs to notify
NotifyBlobReceived(blob blob.Ref)
RegisterListener(ch chan<- blob.Ref)
UnregisterListener(ch chan<- blob.Ref)
2011-02-26 20:22:26 +00:00
RegisterBlobListener(blob blob.Ref, ch chan<- blob.Ref)
UnregisterBlobListener(blob blob.Ref, ch chan<- blob.Ref)
}
var (
hubmu sync.Mutex
stohub = map[interface{}]BlobHub{} // Storage -> hub
)
// GetHub return a BlobHub for the give storage implementation.
func GetHub(storage interface{}) BlobHub {
hubmu.Lock()
defer hubmu.Unlock()
h, ok := stohub[storage]
if ok {
return h
}
h = new(memHub)
stohub[storage] = h
return h
}
// canLongPoll is set to false on App Engine. (multi-process environment...)
var canLongPoll = true
// WaitForBlob waits until deadline for blobs to arrive. If blobs is empty, any
// blobs are waited on. Otherwise, those specific blobs are waited on.
// When WaitForBlob returns, nothing may have happened.
func WaitForBlob(storage interface{}, deadline time.Time, blobs []blob.Ref) {
hub := GetHub(storage)
ch := make(chan blob.Ref, 1)
if len(blobs) == 0 {
hub.RegisterListener(ch)
defer hub.UnregisterListener(ch)
}
for _, br := range blobs {
hub.RegisterBlobListener(br, ch)
defer hub.UnregisterBlobListener(br, ch)
}
var tc <-chan time.Time
if !canLongPoll {
tc = time.After(2 * time.Second)
}
select {
case <-ch:
case <-tc:
case <-time.After(deadline.Sub(time.Now())):
}
}
type memHub struct {
2011-02-26 20:22:26 +00:00
l sync.Mutex
listeners map[chan<- blob.Ref]bool
blobListeners map[string]map[chan<- blob.Ref]bool
2011-02-26 20:22:26 +00:00
}
func (h *memHub) NotifyBlobReceived(br blob.Ref) {
2011-02-26 20:22:26 +00:00
h.l.Lock()
defer h.l.Unlock()
// Callback channels to notify, nil until non-empty
var notify []chan<- blob.Ref
2011-02-26 20:22:26 +00:00
// Append global listeners
for ch, _ := range h.listeners {
notify = append(notify, ch)
}
// Append blob-specific listeners
if h.blobListeners != nil {
blobstr := br.String()
2011-02-26 20:22:26 +00:00
if set, ok := h.blobListeners[blobstr]; ok {
for ch, _ := range set {
notify = append(notify, ch)
}
}
}
if len(notify) > 0 {
// Run in a separate Goroutine so NotifyBlobReceived doesn't block
// callers if callbacks are slow.
go func() {
for _, ch := range notify {
ch <- br
}
}()
}
2011-02-26 20:22:26 +00:00
}
func (h *memHub) RegisterListener(ch chan<- blob.Ref) {
2011-02-26 20:22:26 +00:00
h.l.Lock()
defer h.l.Unlock()
if h.listeners == nil {
h.listeners = make(map[chan<- blob.Ref]bool)
2011-02-26 20:22:26 +00:00
}
h.listeners[ch] = true
}
func (h *memHub) UnregisterListener(ch chan<- blob.Ref) {
2011-02-26 20:22:26 +00:00
h.l.Lock()
defer h.l.Unlock()
if h.listeners == nil {
panic("blobhub: UnregisterListener called without RegisterListener")
}
delete(h.listeners, ch)
}
func (h *memHub) RegisterBlobListener(br blob.Ref, ch chan<- blob.Ref) {
2011-02-26 20:22:26 +00:00
h.l.Lock()
defer h.l.Unlock()
if h.blobListeners == nil {
h.blobListeners = make(map[string]map[chan<- blob.Ref]bool)
2011-02-26 20:22:26 +00:00
}
bstr := br.String()
2011-02-26 20:22:26 +00:00
_, ok := h.blobListeners[bstr]
if !ok {
h.blobListeners[bstr] = make(map[chan<- blob.Ref]bool)
2011-02-26 20:22:26 +00:00
}
h.blobListeners[bstr][ch] = true
}
func (h *memHub) UnregisterBlobListener(blob blob.Ref, ch chan<- blob.Ref) {
2011-02-26 20:22:26 +00:00
h.l.Lock()
defer h.l.Unlock()
if h.blobListeners == nil {
panic("blobhub: UnregisterBlobListener called without RegisterBlobListener")
}
bstr := blob.String()
set, ok := h.blobListeners[bstr]
if !ok {
panic("blobhub: UnregisterBlobListener called without RegisterBlobListener for " + bstr)
}
delete(set, ch)
2011-02-26 20:22:26 +00:00
if len(set) == 0 {
delete(h.blobListeners, bstr)
2011-02-26 20:22:26 +00:00
}
}
type memHubPartitionMap struct {
hubLock sync.Mutex
hub BlobHub
}
func (spm *memHubPartitionMap) GetBlobHub() BlobHub {
spm.hubLock.Lock()
defer spm.hubLock.Unlock()
if spm.hub == nil {
// TODO: in the future, allow for different blob hub
// implementations rather than the
// everything-in-memory-on-a-single-machine memHub.
spm.hub = new(memHub)
}
return spm.hub
}