via

Simple pubsub server inspired by https://patchbay.pub/
git clone https://git.ce9e.org/via.git

commit
6f19bac953b4b59a64922bb1d69120d822f0fa59
parent
b2802e9d85dbcbed3c562b3d5fa15dc91ac8f79b
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2020-10-15 08:36
refactor: split Topic.post() from post()

Diffstat

M via.go 42 +++++++++++++++++++++++-------------------

1 files changed, 23 insertions, 19 deletions


diff --git a/via.go b/via.go

@@ -49,6 +49,28 @@ func getStorePath(key string) string {
   49    49 	return path.Join(dir, hash)
   50    50 }
   51    51 
   -1    52 func (topic *Topic) post(data []byte) {
   -1    53 	topic.Lock()
   -1    54 	defer topic.Unlock()
   -1    55 
   -1    56 	topic.lastId += 1
   -1    57 	msg := Msg{topic.lastId, data}
   -1    58 
   -1    59 	if topic.hasHistory {
   -1    60 		topic.history = append(topic.history, msg)
   -1    61 
   -1    62 		for len(topic.history) > maxHistorySize {
   -1    63 			topic.history = topic.history[1:]
   -1    64 		}
   -1    65 	}
   -1    66 
   -1    67 	for channel := range topic.channels {
   -1    68 		go func(ch chan Msg) {
   -1    69 			ch <- msg
   -1    70 		}(channel)
   -1    71 	}
   -1    72 }
   -1    73 
   52    74 func pushChannel(key string, password string, ch chan Msg, lastId int) bool {
   53    75 	mux.RLock()
   54    76 	topic, ok := topics[key]
@@ -128,25 +150,7 @@ func post(w http.ResponseWriter, r *http.Request) {
  128   150 		return
  129   151 	}
  130   152 
  131    -1 	topic.RLock()
  132    -1 	defer topic.RUnlock()
  133    -1 
  134    -1 	topic.lastId += 1
  135    -1 	msg := Msg{topic.lastId, body}
  136    -1 
  137    -1 	if topic.hasHistory {
  138    -1 		topic.history = append(topic.history, msg)
  139    -1 
  140    -1 		for len(topic.history) > maxHistorySize {
  141    -1 			topic.history = topic.history[1:]
  142    -1 		}
  143    -1 	}
  144    -1 
  145    -1 	for channel := range topic.channels {
  146    -1 		go func(ch chan Msg) {
  147    -1 			ch <- msg
  148    -1 		}(channel)
  149    -1 	}
   -1   153 	topic.post(body)
  150   154 }
  151   155 
  152   156 func get(w http.ResponseWriter, r *http.Request) {