mirror of
https://github.com/logos-messaging/go-libp2p-rendezvous.git
synced 2026-01-04 05:43:06 +00:00
refactor database interface and implementation into db subpackage
We now have: - db/dbi.go -- interface, no dependency on any particular database - db/sqlite/... -- sqlite implementation, depends on dbi - the rendezvous.NewRendezvousService API has been redefined to NewRendezvouServiceWithDB service importer constructs the database of choice - the rendezvous package has no dependences on db implementation, so clients can import without linking SQLite in the binary
This commit is contained in:
parent
aa7f9daaa8
commit
c487c2077b
@ -15,7 +15,7 @@ func TestClientRegistrationAndDiscovery(t *testing.T) {
|
|||||||
|
|
||||||
hosts := getRendezvousHosts(t, ctx, 5)
|
hosts := getRendezvousHosts(t, ctx, 5)
|
||||||
|
|
||||||
svc, err := NewRendezvousService(ctx, hosts[0], ":memory:")
|
svc, err := makeRendezvousService(ctx, hosts[0], ":memory:")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -74,7 +74,7 @@ func TestClientRegistrationAndDiscoveryAsync(t *testing.T) {
|
|||||||
|
|
||||||
hosts := getRendezvousHosts(t, ctx, 5)
|
hosts := getRendezvousHosts(t, ctx, 5)
|
||||||
|
|
||||||
svc, err := NewRendezvousService(ctx, hosts[0], ":memory:")
|
svc, err := makeRendezvousService(ctx, hosts[0], ":memory:")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
21
db/dbi.go
Normal file
21
db/dbi.go
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package dbi
|
||||||
|
|
||||||
|
import (
|
||||||
|
peer "github.com/libp2p/go-libp2p-peer"
|
||||||
|
)
|
||||||
|
|
||||||
|
type RegistrationRecord struct {
|
||||||
|
Id peer.ID
|
||||||
|
Addrs [][]byte
|
||||||
|
Ns string
|
||||||
|
Ttl int
|
||||||
|
}
|
||||||
|
|
||||||
|
type DB interface {
|
||||||
|
Close() error
|
||||||
|
Register(p peer.ID, ns string, addrs [][]byte, ttl int) error
|
||||||
|
Unregister(p peer.ID, ns string) error
|
||||||
|
CountRegistrations(p peer.ID) (int, error)
|
||||||
|
Discover(ns string, cookie []byte, limit int) ([]RegistrationRecord, []byte, error)
|
||||||
|
ValidCookie(ns string, cookie []byte) bool
|
||||||
|
}
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package rendezvous
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
@ -11,11 +11,16 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
dbi "github.com/libp2p/go-libp2p-rendezvous/db"
|
||||||
|
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
|
|
||||||
|
logging "github.com/ipfs/go-log"
|
||||||
peer "github.com/libp2p/go-libp2p-peer"
|
peer "github.com/libp2p/go-libp2p-peer"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var log = logging.Logger("rendezvous/db")
|
||||||
|
|
||||||
type DB struct {
|
type DB struct {
|
||||||
db *sql.DB
|
db *sql.DB
|
||||||
|
|
||||||
@ -240,7 +245,7 @@ func (db *DB) Unregister(p peer.ID, ns string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) Discover(ns string, cookie []byte, limit int) ([]RegistrationRecord, []byte, error) {
|
func (db *DB) Discover(ns string, cookie []byte, limit int) ([]dbi.RegistrationRecord, []byte, error) {
|
||||||
now := time.Now().Unix()
|
now := time.Now().Unix()
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -278,10 +283,10 @@ func (db *DB) Discover(ns string, cookie []byte, limit int) ([]RegistrationRecor
|
|||||||
|
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
|
||||||
regs := make([]RegistrationRecord, 0, limit)
|
regs := make([]dbi.RegistrationRecord, 0, limit)
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var (
|
var (
|
||||||
reg RegistrationRecord
|
reg dbi.RegistrationRecord
|
||||||
rid string
|
rid string
|
||||||
rns string
|
rns string
|
||||||
expire int64
|
expire int64
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package rendezvous
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
10
proto.go
10
proto.go
@ -4,6 +4,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
db "github.com/libp2p/go-libp2p-rendezvous/db"
|
||||||
pb "github.com/libp2p/go-libp2p-rendezvous/pb"
|
pb "github.com/libp2p/go-libp2p-rendezvous/pb"
|
||||||
|
|
||||||
logging "github.com/ipfs/go-log"
|
logging "github.com/ipfs/go-log"
|
||||||
@ -21,13 +22,6 @@ const (
|
|||||||
DefaultTTL = 2 * 3600 // 2hr
|
DefaultTTL = 2 * 3600 // 2hr
|
||||||
)
|
)
|
||||||
|
|
||||||
type RegistrationRecord struct {
|
|
||||||
Id peer.ID
|
|
||||||
Addrs [][]byte
|
|
||||||
Ns string
|
|
||||||
Ttl int
|
|
||||||
}
|
|
||||||
|
|
||||||
type RendezvousError struct {
|
type RendezvousError struct {
|
||||||
Status pb.Message_ResponseStatus
|
Status pb.Message_ResponseStatus
|
||||||
Text string
|
Text string
|
||||||
@ -120,7 +114,7 @@ func newRegisterResponseError(status pb.Message_ResponseStatus, text string) *pb
|
|||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
func newDiscoverResponse(regs []RegistrationRecord, cookie []byte) *pb.Message_DiscoverResponse {
|
func newDiscoverResponse(regs []db.RegistrationRecord, cookie []byte) *pb.Message_DiscoverResponse {
|
||||||
r := new(pb.Message_DiscoverResponse)
|
r := new(pb.Message_DiscoverResponse)
|
||||||
r.Status = pb.Message_OK.Enum()
|
r.Status = pb.Message_OK.Enum()
|
||||||
|
|
||||||
|
|||||||
16
svc.go
16
svc.go
@ -1,9 +1,9 @@
|
|||||||
package rendezvous
|
package rendezvous
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
db "github.com/libp2p/go-libp2p-rendezvous/db"
|
||||||
pb "github.com/libp2p/go-libp2p-rendezvous/pb"
|
pb "github.com/libp2p/go-libp2p-rendezvous/pb"
|
||||||
|
|
||||||
ggio "github.com/gogo/protobuf/io"
|
ggio "github.com/gogo/protobuf/io"
|
||||||
@ -21,7 +21,7 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type RendezvousService struct {
|
type RendezvousService struct {
|
||||||
DB *DB
|
DB db.DB
|
||||||
rzs []RendezvousSync
|
rzs []RendezvousSync
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -30,17 +30,7 @@ type RendezvousSync interface {
|
|||||||
Unregister(p peer.ID, ns string)
|
Unregister(p peer.ID, ns string)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRendezvousService(ctx context.Context, host host.Host, dbpath string, rzs ...RendezvousSync) (*RendezvousService, error) {
|
func NewRendezvousService(host host.Host, db db.DB, rzs ...RendezvousSync) *RendezvousService {
|
||||||
db, err := OpenDB(ctx, dbpath)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
rz := NewRendezvousServiceWithDB(host, db, rzs...)
|
|
||||||
return rz, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewRendezvousServiceWithDB(host host.Host, db *DB, rzs ...RendezvousSync) *RendezvousService {
|
|
||||||
rz := &RendezvousService{DB: db, rzs: rzs}
|
rz := &RendezvousService{DB: db, rzs: rzs}
|
||||||
host.SetStreamHandler(RendezvousProto, rz.handleStream)
|
host.SetStreamHandler(RendezvousProto, rz.handleStream)
|
||||||
return rz
|
return rz
|
||||||
|
|||||||
14
svc_test.go
14
svc_test.go
@ -6,6 +6,7 @@ import (
|
|||||||
"math/rand"
|
"math/rand"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
db "github.com/libp2p/go-libp2p-rendezvous/db/sqlite"
|
||||||
pb "github.com/libp2p/go-libp2p-rendezvous/pb"
|
pb "github.com/libp2p/go-libp2p-rendezvous/pb"
|
||||||
|
|
||||||
ggio "github.com/gogo/protobuf/io"
|
ggio "github.com/gogo/protobuf/io"
|
||||||
@ -53,13 +54,22 @@ func getRendezvousClients(t *testing.T, hosts []host.Host) []Rendezvous {
|
|||||||
return clients
|
return clients
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func makeRendezvousService(ctx context.Context, host host.Host, path string) (*RendezvousService, error) {
|
||||||
|
dbi, err := db.OpenDB(ctx, path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return NewRendezvousService(host, dbi), nil
|
||||||
|
}
|
||||||
|
|
||||||
func TestSVCRegistrationAndDiscovery(t *testing.T) {
|
func TestSVCRegistrationAndDiscovery(t *testing.T) {
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
hosts := getRendezvousHosts(t, ctx, 5)
|
hosts := getRendezvousHosts(t, ctx, 5)
|
||||||
|
|
||||||
svc, err := NewRendezvousService(ctx, hosts[0], ":memory:")
|
svc, err := makeRendezvousService(ctx, hosts[0], ":memory:")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -169,7 +179,7 @@ func TestSVCErrors(t *testing.T) {
|
|||||||
|
|
||||||
hosts := getRendezvousHosts(t, ctx, 2)
|
hosts := getRendezvousHosts(t, ctx, 2)
|
||||||
|
|
||||||
svc, err := NewRendezvousService(ctx, hosts[0], ":memory:")
|
svc, err := makeRendezvousService(ctx, hosts[0], ":memory:")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user