Mirror of @tangled.org/core. Running on a Raspberry Pi Zero 2 (Please be gentle).
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

appview,knotclient: refactor cursor store to separate package

Signed-off-by: oppiliappan <me@oppi.li>

+78 -63
+2 -1
appview/state/knotstream.go
··· 12 12 "tangled.sh/tangled.sh/core/appview/config" 13 13 "tangled.sh/tangled.sh/core/appview/db" 14 14 kc "tangled.sh/tangled.sh/core/knotclient" 15 + "tangled.sh/tangled.sh/core/knotclient/cursor" 15 16 "tangled.sh/tangled.sh/core/log" 16 17 "tangled.sh/tangled.sh/core/rbac" 17 18 ··· 33 32 34 33 logger := log.New("knotstream") 35 34 cache := cache.New(c.Redis.Addr) 36 - cursorStore := kc.NewRedisCursorStore(cache) 35 + cursorStore := cursor.NewRedisCursorStore(cache) 37 36 38 37 cfg := kc.ConsumerConfig{ 39 38 Sources: srcs,
+23
knotclient/cursor/memory.go
··· 1 + package cursor 2 + 3 + import ( 4 + "sync" 5 + ) 6 + 7 + type MemoryStore struct { 8 + store sync.Map 9 + } 10 + 11 + func (m *MemoryStore) Set(knot string, cursor int64) { 12 + m.store.Store(knot, cursor) 13 + } 14 + 15 + func (m *MemoryStore) Get(knot string) (cursor int64) { 16 + if result, ok := m.store.Load(knot); ok { 17 + if val, ok := result.(int64); ok { 18 + return val 19 + } 20 + } 21 + 22 + return 0 23 + }
+43
knotclient/cursor/redis.go
··· 1 + package cursor 2 + 3 + import ( 4 + "context" 5 + "fmt" 6 + "strconv" 7 + 8 + "tangled.sh/tangled.sh/core/appview/cache" 9 + ) 10 + 11 + const ( 12 + cursorKey = "cursor:%s" 13 + ) 14 + 15 + type RedisStore struct { 16 + rdb *cache.Cache 17 + } 18 + 19 + func NewRedisCursorStore(cache *cache.Cache) RedisStore { 20 + return RedisStore{ 21 + rdb: cache, 22 + } 23 + } 24 + 25 + func (r *RedisStore) Set(knot string, cursor int64) { 26 + key := fmt.Sprintf(cursorKey, knot) 27 + r.rdb.Set(context.Background(), key, cursor, 0) 28 + } 29 + 30 + func (r *RedisStore) Get(knot string) (cursor int64) { 31 + key := fmt.Sprintf(cursorKey, knot) 32 + val, err := r.rdb.Get(context.Background(), key).Result() 33 + if err != nil { 34 + return 0 35 + } 36 + cursor, err = strconv.ParseInt(val, 10, 64) 37 + if err != nil { 38 + // TODO: log here 39 + return 0 40 + } 41 + 42 + return cursor 43 + }
+6
knotclient/cursor/store.go
··· 1 + package cursor 2 + 3 + type Store interface { 4 + Set(knot string, cursor int64) 5 + Get(knot string) (cursor int64) 6 + }
+3 -61
knotclient/events.go
··· 7 7 "log/slog" 8 8 "math/rand" 9 9 "net/url" 10 - "strconv" 11 10 "sync" 12 11 "time" 13 12 14 - "tangled.sh/tangled.sh/core/appview/cache" 13 + "tangled.sh/tangled.sh/core/knotclient/cursor" 15 14 "tangled.sh/tangled.sh/core/log" 16 15 17 16 "github.com/gorilla/websocket" ··· 35 36 QueueSize int 36 37 Logger *slog.Logger 37 38 Dev bool 38 - CursorStore CursorStore 39 + CursorStore cursor.Store 39 40 } 40 41 41 42 func NewConsumerConfig() *ConsumerConfig { ··· 69 70 // rw lock over edits to ConsumerConfig 70 71 cfgMu sync.RWMutex 71 72 cfg ConsumerConfig 72 - } 73 - 74 - type CursorStore interface { 75 - Set(knot string, cursor int64) 76 - Get(knot string) (cursor int64) 77 - } 78 - 79 - type RedisCursorStore struct { 80 - rdb *cache.Cache 81 - } 82 - 83 - func NewRedisCursorStore(cache *cache.Cache) RedisCursorStore { 84 - return RedisCursorStore{ 85 - rdb: cache, 86 - } 87 - } 88 - 89 - const ( 90 - cursorKey = "cursor:%s" 91 - ) 92 - 93 - func (r *RedisCursorStore) Set(knot string, cursor int64) { 94 - key := fmt.Sprintf(cursorKey, knot) 95 - r.rdb.Set(context.Background(), key, cursor, 0) 96 - } 97 - 98 - func (r *RedisCursorStore) Get(knot string) (cursor int64) { 99 - key := fmt.Sprintf(cursorKey, knot) 100 - val, err := r.rdb.Get(context.Background(), key).Result() 101 - if err != nil { 102 - return 0 103 - } 104 - 105 - cursor, err = strconv.ParseInt(val, 10, 64) 106 - if err != nil { 107 - return 0 // optionally log parsing error 108 - } 109 - 110 - return cursor 111 - } 112 - 113 - type MemoryCursorStore struct { 114 - store sync.Map 115 - } 116 - 117 - func (m *MemoryCursorStore) Set(knot string, cursor int64) { 118 - m.store.Store(knot, cursor) 119 - } 120 - 121 - func (m *MemoryCursorStore) Get(knot string) (cursor int64) { 122 - if result, ok := m.store.Load(knot); ok { 123 - if val, ok := result.(int64); ok { 124 - return val 125 - } 126 - } 127 - 128 - return 0 129 73 } 130 74 131 75 func (e *EventConsumer) buildUrl(s EventSource, cursor int64) (*url.URL, error) { ··· 115 173 cfg.QueueSize = 100 116 174 } 117 175 if cfg.CursorStore == nil { 118 - cfg.CursorStore = &MemoryCursorStore{} 176 + cfg.CursorStore = &cursor.MemoryStore{} 119 177 } 120 178 return &EventConsumer{ 121 179 cfg: cfg,
+1 -1
nix/vm.nix
··· 21 21 g = config.services.tangled-knot.gitUser; 22 22 in [ 23 23 "d /var/lib/knot 0770 ${u} ${g} - -" # Create the directory first 24 - "f+ /var/lib/knot/secret 0660 ${u} ${g} - KNOT_SERVER_SECRET=16154910ef55fe48121082c0b51fc0e360a8b15eb7bda7991d88dc9f7684427a" 24 + "f+ /var/lib/knot/secret 0660 ${u} ${g} - KNOT_SERVER_SECRET=2650ecafdce279b09865fb1923051156eb773ee7485061b2e766086f07dbd85a" 25 25 ]; 26 26 services.tangled-knot = { 27 27 enable = true;