via

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

commit
fa9177ac57d4a342286006109a3dd3c4070cb1c5
parent
b5f02bd0e6629c15873e19abddbe68cdfb90bf82
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2025-06-07 15:59
perf: exit early if no history in PUT/DELETE

Diffstat

M via.go 18 +++++++++++-------

1 files changed, 11 insertions, 7 deletions


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

@@ -44,6 +44,10 @@ func getStorePath(key string) string {
   44    44 	return path.Join(dir, hash)
   45    45 }
   46    46 
   -1    47 func hasHistory(key string) bool {
   -1    48 	return strings.HasPrefix(key, "/hmsg/")
   -1    49 }
   -1    50 
   47    51 func (topic *Topic) storeHistory(key string) {
   48    52 	content, err := json.Marshal(topic.history)
   49    53 	if err != nil {
@@ -135,7 +139,7 @@ func getTopic(key string) *Topic {
  135   139 	if !ok {
  136   140 		topic = &Topic{
  137   141 			channels:   make(map[chan Msg]bool, 0),
  138    -1 			hasHistory: strings.HasPrefix(key, "/hmsg/"),
   -1   142 			hasHistory: hasHistory(key),
  139   143 			history:    make([]Msg, 0),
  140   144 			lastId:     0,
  141   145 		}
@@ -261,13 +265,13 @@ func get(w http.ResponseWriter, r *http.Request) {
  261   265 }
  262   266 
  263   267 func put(w http.ResponseWriter, r *http.Request) {
  264    -1 	topic := getTopic(r.URL.Path)
  265    -1 
  266    -1 	if !topic.hasHistory {
   -1   268 	if !hasHistory(r.URL.Path) {
  267   269 		http.Error(w, "No history", http.StatusBadRequest)
  268   270 		return
  269   271 	}
  270   272 
   -1   273 	topic := getTopic(r.URL.Path)
   -1   274 
  271   275 	lastId, err := strconv.Atoi(r.Header.Get("Last-Event-ID"))
  272   276 	if err != nil {
  273   277 		http.Error(w, "Missing Last-Event-ID", http.StatusBadRequest)
@@ -289,13 +293,13 @@ func put(w http.ResponseWriter, r *http.Request) {
  289   293 }
  290   294 
  291   295 func del(w http.ResponseWriter, r *http.Request) {
  292    -1 	topic := getTopic(r.URL.Path)
  293    -1 
  294    -1 	if !topic.hasHistory {
   -1   296 	if !hasHistory(r.URL.Path) {
  295   297 		http.Error(w, "No history", http.StatusBadRequest)
  296   298 		return
  297   299 	}
  298   300 
   -1   301 	topic := getTopic(r.URL.Path)
   -1   302 
  299   303 	topic.Lock()
  300   304 	defer topic.Unlock()
  301   305