2018-09-02 19:03:02 +08:00
|
|
|
package pstoreds
|
2018-08-29 22:12:41 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2018-09-02 19:10:55 +08:00
|
|
|
"github.com/ipfs/go-datastore"
|
2018-08-30 23:24:09 +08:00
|
|
|
|
2018-08-29 22:12:41 +08:00
|
|
|
pstore "github.com/libp2p/go-libp2p-peerstore"
|
2018-09-05 01:07:44 +08:00
|
|
|
"github.com/libp2p/go-libp2p-peerstore/pstoremem"
|
2018-08-29 22:12:41 +08:00
|
|
|
)
|
|
|
|
|
2018-09-04 18:34:55 +08:00
|
|
|
// Configuration object for the peerstore.
|
|
|
|
type PeerstoreOpts struct {
|
|
|
|
// The size of the in-memory cache. A value of 0 or lower disables the cache.
|
|
|
|
CacheSize uint
|
|
|
|
|
|
|
|
// Sweep interval to expire entries when TTL is not managed by underlying datastore.
|
|
|
|
TTLInterval time.Duration
|
|
|
|
|
|
|
|
// Number of times to retry transactional writes.
|
|
|
|
WriteRetries uint
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultOpts returns the default options for a persistent peerstore:
|
|
|
|
// * Cache size: 1024
|
|
|
|
// * TTL sweep interval: 1 second
|
|
|
|
// * WriteRetries: 5
|
|
|
|
func DefaultOpts() PeerstoreOpts {
|
|
|
|
return PeerstoreOpts{
|
|
|
|
CacheSize: 1024,
|
|
|
|
TTLInterval: time.Second,
|
|
|
|
WriteRetries: 5,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-29 22:12:41 +08:00
|
|
|
// NewPeerstore creates a peerstore backed by the provided persistent datastore.
|
2018-09-04 18:34:55 +08:00
|
|
|
func NewPeerstore(ctx context.Context, ds datastore.TxnDatastore, opts PeerstoreOpts) (pstore.Peerstore, error) {
|
|
|
|
addrBook, err := NewAddrBook(ctx, ds, opts)
|
2018-08-29 22:12:41 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-09-05 22:14:40 +08:00
|
|
|
ps := pstore.NewPeerstore(pstoremem.NewKeyBook(), addrBook, pstoremem.NewPeerMetadata())
|
2018-08-29 22:12:41 +08:00
|
|
|
return ps, nil
|
|
|
|
}
|