Esempio n. 1
0
func vkvWatchKeyHandler(vkvhub *hub.Hub) func(http.ResponseWriter, *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {
		vars := mux.Vars(r)
		defer func() {
			log.Printf("WATCH CLOSE")
		}()
		conn, _ := sse.Upgrade(w, r)
		notify := w.(http.CloseNotifier).CloseNotify()
		stream := vkvhub.Sub(vars["key"])
	L:
		for {
			select {
			case <-notify:
				log.Printf("Close")
				vkvhub.Unsub(vars["key"], stream)
				break L
			case m := <-stream:
				conn.WriteString(m)
			}
		}
	}
}
Esempio n. 2
0
func (mh *MetaHandler) processKvUpdate(wg sync.WaitGroup, blobs chan<- *router.Blob, kvUpdate <-chan *vkv.KeyValue, vkvhub *hub.Hub) {
	wg.Add(1)
	defer wg.Done()
	for kv := range kvUpdate {
		mh.log.Debug(fmt.Sprintf("kvupdate: %+v", kv))
		go vkvhub.Pub(kv.Key, fmt.Sprintf("%d:%s", kv.Version, kv.Value))
		blob := CreateMetaBlob(kv)
		req := &router.Request{
			MetaBlob:  true,
			Type:      router.Write,
			Namespace: kv.Namespace(),
		}
		hash := fmt.Sprintf("%x", blake2b.Sum256(blob))
		if err := kv.SetMetaBlob(hash); err != nil {
			panic(err)
		}
		select {
		case blobs <- &router.Blob{Req: req, Hash: hash, Blob: blob}:
		case <-mh.stop:
			mh.log.Info("Stopping...")
			return
		}
	}
}