From 0cf03109be9b34031e76439aff870dcc95c99cd1 Mon Sep 17 00:00:00 2001 From: Evan Jarrett Date: Sun, 26 Oct 2025 22:52:06 -0500 Subject: [PATCH] need the actually new file --- pkg/atproto/directory.go | 58 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 pkg/atproto/directory.go diff --git a/pkg/atproto/directory.go b/pkg/atproto/directory.go new file mode 100644 index 0000000..6f27d92 --- /dev/null +++ b/pkg/atproto/directory.go @@ -0,0 +1,58 @@ +package atproto + +import ( + "context" + "net" + "net/http" + "sync" + "time" + + "github.com/bluesky-social/indigo/atproto/identity" + "github.com/earthboundkid/versioninfo/v2" +) + +var ( + // Shared identity directory instance (singleton) + sharedDirectory identity.Directory + directoryOnce sync.Once +) + +// GetDirectory returns a shared identity.Directory instance with an 8-hour cache TTL. +// This is based on indigo's DefaultDirectory() but with a reduced cache TTL +// to allow faster recovery from PDS migrations (8h instead of 24h). +// +// Using a shared instance ensures all identity lookups across the application +// use the same cache, which is more memory-efficient and provides better cache hit rates. +func GetDirectory() identity.Directory { + directoryOnce.Do(func() { + base := identity.BaseDirectory{ + PLCURL: identity.DefaultPLCURL, + HTTPClient: http.Client{ + Timeout: time.Second * 10, + Transport: &http.Transport{ + // would want this around 100ms for services doing lots of handle resolution. Impacts PLC connections as well, but not too bad. + IdleConnTimeout: time.Millisecond * 1000, + MaxIdleConns: 100, + }, + }, + Resolver: net.Resolver{ + Dial: func(ctx context.Context, network, address string) (net.Conn, error) { + d := net.Dialer{Timeout: time.Second * 3} + return d.DialContext(ctx, network, address) + }, + }, + TryAuthoritativeDNS: true, + // primary Bluesky PDS instance only supports HTTP resolution method + SkipDNSDomainSuffixes: []string{".bsky.social"}, + UserAgent: "indigo-identity/" + versioninfo.Short(), + } + // Cache configuration: + // - capacity: 250,000 entries + // - hitTTL: 8 hours (reduced from indigo's default 24h for faster PDS migration recovery) + // - errTTL: 2 minutes + // - invalidHandleTTL: 5 minutes + cached := identity.NewCacheDirectory(&base, 250_000, time.Hour*8, time.Minute*2, time.Minute*5) + sharedDirectory = &cached + }) + return sharedDirectory +}