Skip to content

Commit

Permalink
Fix async FakeIP save
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Jul 8, 2023
1 parent 1c526fe commit 9d75385
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 9 deletions.
2 changes: 2 additions & 0 deletions adapter/fakeip.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/netip"

"github.com/sagernet/sing-dns"
"github.com/sagernet/sing/common/logger"
)

type FakeIPStore interface {
Expand All @@ -18,6 +19,7 @@ type FakeIPStorage interface {
FakeIPMetadata() *FakeIPMetadata
FakeIPSaveMetadata(metadata *FakeIPMetadata) error
FakeIPStore(address netip.Addr, domain string) error
FakeIPStoreAsync(address netip.Addr, domain string, logger logger.Logger)
FakeIPLoad(address netip.Addr) (string, bool)
FakeIPReset() error
}
Expand Down
14 changes: 11 additions & 3 deletions experimental/clashapi/cachefile/cache.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package cachefile

import (
"net/netip"
"os"
"sync"
"time"

"github.com/sagernet/sing-box/adapter"
Expand All @@ -14,8 +16,10 @@ var bucketSelected = []byte("selected")
var _ adapter.ClashCacheFile = (*CacheFile)(nil)

type CacheFile struct {
DB *bbolt.DB
cacheID []byte
DB *bbolt.DB
cacheID []byte
saveAccess sync.RWMutex
saveCache map[netip.Addr]string
}

func Open(path string, cacheID string) (*CacheFile, error) {
Expand All @@ -36,7 +40,11 @@ func Open(path string, cacheID string) (*CacheFile, error) {
if cacheID != "" {
cacheIDBytes = append([]byte{0}, []byte(cacheID)...)
}
return &CacheFile{db, cacheIDBytes}, nil
return &CacheFile{
DB: db,
cacheID: cacheIDBytes,
saveCache: make(map[netip.Addr]string),
}, nil
}

func (c *CacheFile) bucket(t *bbolt.Tx, key []byte) *bbolt.Bucket {
Expand Down
22 changes: 22 additions & 0 deletions experimental/clashapi/cachefile/fakeip.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"

"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing/common/logger"

"go.etcd.io/bbolt"
)
Expand Down Expand Up @@ -57,7 +58,28 @@ func (c *CacheFile) FakeIPStore(address netip.Addr, domain string) error {
})
}

func (c *CacheFile) FakeIPStoreAsync(address netip.Addr, domain string, logger logger.Logger) {
c.saveAccess.Lock()
c.saveCache[address] = domain
c.saveAccess.Unlock()
go func() {
err := c.FakeIPStore(address, domain)
if err != nil {
logger.Warn("save FakeIP address pair: ", err)
}
c.saveAccess.Lock()
delete(c.saveCache, address)
c.saveAccess.Unlock()
}()
}

func (c *CacheFile) FakeIPLoad(address netip.Addr) (string, bool) {
c.saveAccess.RLock()
cachedDomain, cached := c.saveCache[address]
c.saveAccess.RUnlock()
if cached {
return cachedDomain, true
}
var domain string
_ = c.DB.View(func(tx *bbolt.Tx) error {
bucket := tx.Bucket(bucketFakeIP)
Expand Down
5 changes: 5 additions & 0 deletions transport/fakeip/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing/common/cache"
"github.com/sagernet/sing/common/logger"
)

var _ adapter.FakeIPStorage = (*MemoryStorage)(nil)
Expand Down Expand Up @@ -34,6 +35,10 @@ func (s *MemoryStorage) FakeIPStore(address netip.Addr, domain string) error {
return nil
}

func (s *MemoryStorage) FakeIPStoreAsync(address netip.Addr, domain string, logger logger.Logger) {
s.domainCache.Store(address, domain)
}

func (s *MemoryStorage) FakeIPLoad(address netip.Addr) (string, bool) {
return s.domainCache.Load(address)
}
Expand Down
7 changes: 1 addition & 6 deletions transport/fakeip/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,7 @@ func (s *Store) Create(domain string, strategy dns.DomainStrategy) (netip.Addr,
s.inet6Current = nextAddress
address = nextAddress
}
go func() {
err := s.storage.FakeIPStore(address, domain)
if err != nil {
s.logger.Warn("save FakeIP address pair: ", err)
}
}()
s.storage.FakeIPStoreAsync(address, domain, s.logger)
return address, nil
}

Expand Down

0 comments on commit 9d75385

Please sign in to comment.