Skip to content

Commit

Permalink
Merge pull request #47 from raulk/feat/pstore-ds-gc
Browse files Browse the repository at this point in the history
pstoreds: migrate from strict TTL to GC-based expiry.
  • Loading branch information
raulk authored Feb 22, 2019
2 parents d3ce7dc + 6a955ca commit 0c66884
Show file tree
Hide file tree
Showing 18 changed files with 2,238 additions and 534 deletions.
3 changes: 3 additions & 0 deletions p2p/host/peerstore/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package peerstore
import (
"context"
"errors"
"io"
"math"
"time"

Expand Down Expand Up @@ -49,6 +50,8 @@ const (
// Peerstore provides a threadsafe store of Peer related
// information.
type Peerstore interface {
io.Closer

AddrBook
KeyBook
PeerMetadata
Expand Down
12 changes: 12 additions & 0 deletions p2p/host/peerstore/pb/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
PB = $(wildcard *.proto)
GO = $(PB:.proto=.pb.go)

all: $(GO)

%.pb.go: %.proto
protoc --proto_path=$(GOPATH)/src:. --gogofaster_out=. $<

clean:
rm -f *.pb.go

.PHONY: clean
110 changes: 110 additions & 0 deletions p2p/host/peerstore/pb/custom.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package pstore_pb

import (
"encoding/json"

proto "github.com/gogo/protobuf/proto"
peer "github.com/libp2p/go-libp2p-peer"
pt "github.com/libp2p/go-libp2p-peer/test"
ma "github.com/multiformats/go-multiaddr"
)

// customGogoType aggregates the interfaces that custom Gogo types need to implement.
// it is only used for type assertions.
type customGogoType interface {
proto.Marshaler
proto.Unmarshaler
json.Marshaler
json.Unmarshaler
proto.Sizer
}

// ProtoAddr is a custom type used by gogo to serde raw peer IDs into the peer.ID type, and back.
type ProtoPeerID struct {
peer.ID
}

var _ customGogoType = (*ProtoPeerID)(nil)

func (id ProtoPeerID) Marshal() ([]byte, error) {
return []byte(id.ID), nil
}

func (id ProtoPeerID) MarshalTo(data []byte) (n int, err error) {
return copy(data, []byte(id.ID)), nil
}

func (id ProtoPeerID) MarshalJSON() ([]byte, error) {
m, _ := id.Marshal()
return json.Marshal(m)
}

func (id *ProtoPeerID) Unmarshal(data []byte) (err error) {
id.ID = peer.ID(string(data))
return nil
}

func (id *ProtoPeerID) UnmarshalJSON(data []byte) error {
var v []byte
err := json.Unmarshal(data, v)
if err != nil {
return err
}
return id.Unmarshal(v)
}

func (id ProtoPeerID) Size() int {
return len([]byte(id.ID))
}

// ProtoAddr is a custom type used by gogo to serde raw multiaddresses into the ma.Multiaddr type, and back.
type ProtoAddr struct {
ma.Multiaddr
}

var _ customGogoType = (*ProtoAddr)(nil)

func (a ProtoAddr) Marshal() ([]byte, error) {
return a.Bytes(), nil
}

func (a ProtoAddr) MarshalTo(data []byte) (n int, err error) {
return copy(data, a.Bytes()), nil
}

func (a ProtoAddr) MarshalJSON() ([]byte, error) {
m, _ := a.Marshal()
return json.Marshal(m)
}

func (a *ProtoAddr) Unmarshal(data []byte) (err error) {
a.Multiaddr, err = ma.NewMultiaddrBytes(data)
return err
}

func (a *ProtoAddr) UnmarshalJSON(data []byte) error {
v := new([]byte)
err := json.Unmarshal(data, v)
if err != nil {
return err
}
return a.Unmarshal(*v)
}

func (a ProtoAddr) Size() int {
return len(a.Bytes())
}

// NewPopulatedProtoAddr generates a populated instance of the custom gogo type ProtoAddr.
// It is required by gogo-generated tests.
func NewPopulatedProtoAddr(r randyPstore) *ProtoAddr {
a, _ := ma.NewMultiaddr("/ip4/123.123.123.123/tcp/7001")
return &ProtoAddr{Multiaddr: a}
}

// NewPopulatedProtoPeerID generates a populated instance of the custom gogo type ProtoPeerID.
// It is required by gogo-generated tests.
func NewPopulatedProtoPeerID(r randyPstore) *ProtoPeerID {
id, _ := pt.RandPeerID()
return &ProtoPeerID{ID: id}
}
Loading

0 comments on commit 0c66884

Please sign in to comment.