-
Notifications
You must be signed in to change notification settings - Fork 0
/
syncer.go
51 lines (40 loc) · 1.1 KB
/
syncer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package doltswarm
import (
"context"
"errors"
p2pgrpc "github.com/birros/go-libp2p-grpc"
"github.com/nustiueudinastea/doltswarm/proto"
"github.com/sirupsen/logrus"
)
const (
ExternalHeadEvent = "new_head"
)
var _ proto.DBSyncerServer = (*ServerSyncer)(nil)
func NewServerSyncer(logger *logrus.Entry, db *DB) *ServerSyncer {
return &ServerSyncer{
db: db,
}
}
type Event struct {
Peer string
Type string
Data interface{}
}
type ServerSyncer struct {
db *DB
}
func (s *ServerSyncer) AdvertiseHead(ctx context.Context, req *proto.AdvertiseHeadRequest) (*proto.AdvertiseHeadResponse, error) {
peer, ok := p2pgrpc.RemotePeerFromContext(ctx)
if !ok {
return nil, errors.New("no AuthInfo in context")
}
s.db.eventQueue <- Event{Peer: peer.String(), Type: ExternalHeadEvent, Data: req.Head}
return &proto.AdvertiseHeadResponse{}, nil
}
func (s *ServerSyncer) RequestHead(ctx context.Context, req *proto.RequestHeadRequest) (*proto.RequestHeadResponse, error) {
commit, err := s.db.GetLastCommit("main")
if err != nil {
return nil, err
}
return &proto.RequestHeadResponse{Head: commit.Hash}, nil
}