github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/module/mempool/dns_cache.go (about)

     1  package mempool
     2  
     3  import (
     4  	"net"
     5  )
     6  
     7  // DNSCache provides an in-memory cache for storing dns entries.
     8  type DNSCache interface {
     9  	// PutIpDomain adds the given ip domain into cache.
    10  	// The int64 argument is the timestamp associated with the domain.
    11  	PutIpDomain(string, []net.IPAddr, int64) bool
    12  
    13  	// PutTxtRecord adds the given txt record into the cache.
    14  	// The int64 argument is the timestamp associated with the domain.
    15  	PutTxtRecord(string, []string, int64) bool
    16  
    17  	// GetDomainIp returns the ip domain if exists in the cache.
    18  	// The boolean return value determines if domain exists in the cache.
    19  	GetDomainIp(string) (*IpRecord, bool)
    20  
    21  	// GetTxtRecord returns the txt record if exists in the cache.
    22  	// The boolean return value determines if record exists in the cache.
    23  	GetTxtRecord(string) (*TxtRecord, bool)
    24  
    25  	// LockIPDomain locks an ip address dns record if exists in the cache.
    26  	// The boolean return value determines whether attempt on locking was successful.
    27  	//
    28  	// A locking attempt is successful when the domain record exists in the cache and has not
    29  	// been locked before.
    30  	// Once a domain record gets locked the only way to unlock it is through updating that record.
    31  	//
    32  	// The locking process is defined to record that a resolving attempt is ongoing for an expired domain.
    33  	// So the locking happens to avoid any other parallel resolving.
    34  	LockIPDomain(string) (bool, error)
    35  
    36  	// LockTxtRecord locks a txt address dns record if exists in the cache.
    37  	// The boolean return value determines whether attempt on locking was successful.
    38  	//
    39  	// A locking attempt is successful when the domain record exists in the cache and has not
    40  	// been locked before.
    41  	// Once a domain record gets locked the only way to unlock it is through updating that record.
    42  	//
    43  	// The locking process is defined to record that a resolving attempt is ongoing for an expired domain.
    44  	// So the locking happens to avoid any other parallel resolving.
    45  	LockTxtRecord(string) (bool, error)
    46  
    47  	// RemoveIp removes an ip domain from cache.
    48  	RemoveIp(string) bool
    49  
    50  	// RemoveTxt removes a txt record from cache.
    51  	RemoveTxt(string) bool
    52  
    53  	// UpdateTxtRecord updates the dns record for the given txt domain with the new address and timestamp values.
    54  	UpdateTxtRecord(string, []string, int64) error
    55  
    56  	// UpdateIPDomain updates the dns record for the given ip domain with the new address and timestamp values.
    57  	UpdateIPDomain(string, []net.IPAddr, int64) error
    58  
    59  	// Size returns total domains maintained into this cache.
    60  	// The first returned value determines number of ip domains.
    61  	// The second returned value determines number of txt records.
    62  	Size() (uint, uint)
    63  }
    64  
    65  // TxtRecord represents the data model for maintaining a txt dns record in cache.
    66  type TxtRecord struct {
    67  	Txt       string
    68  	Records   []string
    69  	Timestamp int64
    70  
    71  	// A TxtRecord is locked when it is expired and a resolving is ongoing for it.
    72  	// A locked TxtRecord is unlocked by updating it through the cache.
    73  	Locked bool
    74  }
    75  
    76  // IpRecord represents the data model for maintaining an ip dns record in cache.
    77  type IpRecord struct {
    78  	Domain    string
    79  	Addresses []net.IPAddr
    80  	Timestamp int64
    81  
    82  	// An IpRecord is locked when it is expired and a resolving is ongoing for it.
    83  	// A locked IpRecord is unlocked by updating it through the cache.
    84  	Locked bool
    85  }