aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author2026-03-14 21:13:39 +0300
committer2026-03-14 21:13:39 +0300
commit8ee031a9e82d96fa15fe4d1cf32479da39ce85bf (patch)
tree8e56ffb620fa0a18933c2825e103f2a864544b83
parentначальный коммит (diff)
downloadpose-8ee031a9e82d96fa15fe4d1cf32479da39ce85bf.tar.gz
pose-8ee031a9e82d96fa15fe4d1cf32479da39ce85bf.tar.bz2
pose-8ee031a9e82d96fa15fe4d1cf32479da39ce85bf.tar.xz
pose-8ee031a9e82d96fa15fe4d1cf32479da39ce85bf.zip
поправил досадный баг с повторным постингомHEADmaster
-rw-r--r--internal/database/db.go11
1 files changed, 11 insertions, 0 deletions
diff --git a/internal/database/db.go b/internal/database/db.go
index 2fbfe74..d5aa1f4 100644
--- a/internal/database/db.go
+++ b/internal/database/db.go
@@ -11,11 +11,13 @@ import (
"bytes"
"fmt"
"os"
+ "sync"
)
type DB struct {
fp *os.File
cache map[string]struct{}
+ mu *sync.RWMutex
}
func New(path string) (*DB, error) {
@@ -28,6 +30,7 @@ func New(path string) (*DB, error) {
db := &DB{
fp: fp,
cache: map[string]struct{}{},
+ mu: &sync.RWMutex{},
}
for part := range bytes.SplitSeq(cache, []byte("\n")) {
db.cache[string(part)] = struct{}{}
@@ -41,14 +44,22 @@ func (d *DB) Close() error {
}
func (d *DB) Exists(s string) bool {
+ d.mu.RLock()
+ defer d.mu.RUnlock()
+
_, ok := d.cache[s]
return ok
}
func (d *DB) Append(s string) error {
+ d.mu.Lock()
+ defer d.mu.Unlock()
+
if d.Exists(s) {
return nil
}
+
+ d.cache[s] = struct{}{}
_, err := fmt.Fprintln(d.fp, s)
return err
}