Merge pull request #33 from anytypeio/coordinator
This commit is contained in:
commit
51a956fd84
74
coordinator/coordinatorclient/coordinatorclient.go
Normal file
74
coordinator/coordinatorclient/coordinatorclient.go
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
//go:generate mockgen -destination mock_coordinatorclient/mock_coordinatorclient.go github.com/anytypeio/any-sync/coordinator/coordinatorclient CoordinatorClient
|
||||||
|
package coordinatorclient
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"github.com/anytypeio/any-sync/app"
|
||||||
|
"github.com/anytypeio/any-sync/coordinator/coordinatorproto"
|
||||||
|
"github.com/anytypeio/any-sync/net/pool"
|
||||||
|
"github.com/anytypeio/any-sync/nodeconf"
|
||||||
|
)
|
||||||
|
|
||||||
|
const CName = "common.coordinator.coordinatorclient"
|
||||||
|
|
||||||
|
func New() CoordinatorClient {
|
||||||
|
return new(coordinatorClient)
|
||||||
|
}
|
||||||
|
|
||||||
|
type CoordinatorClient interface {
|
||||||
|
SpaceSign(ctx context.Context, spaceId string) (receipt *coordinatorproto.SpaceReceiptWithSignature, err error)
|
||||||
|
FileLimitCheck(ctx context.Context, spaceId string, identity []byte) (limit uint64, err error)
|
||||||
|
app.Component
|
||||||
|
}
|
||||||
|
|
||||||
|
type coordinatorClient struct {
|
||||||
|
pool pool.Pool
|
||||||
|
nodeConf nodeconf.Service
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *coordinatorClient) Init(a *app.App) (err error) {
|
||||||
|
c.pool = a.MustComponent(pool.CName).(pool.Service).NewPool(CName)
|
||||||
|
c.nodeConf = a.MustComponent(nodeconf.CName).(nodeconf.Service)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *coordinatorClient) Name() (name string) {
|
||||||
|
return CName
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *coordinatorClient) SpaceSign(ctx context.Context, spaceId string) (receipt *coordinatorproto.SpaceReceiptWithSignature, err error) {
|
||||||
|
cl, err := c.client(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
resp, err := cl.SpaceSign(ctx, &coordinatorproto.SpaceSignRequest{
|
||||||
|
SpaceId: spaceId,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return resp.Receipt, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *coordinatorClient) FileLimitCheck(ctx context.Context, spaceId string, identity []byte) (limit uint64, err error) {
|
||||||
|
cl, err := c.client(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
resp, err := cl.FileLimitCheck(ctx, &coordinatorproto.FileLimitCheckRequest{
|
||||||
|
AccountIdentity: identity,
|
||||||
|
SpaceId: spaceId,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return resp.Limit, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *coordinatorClient) client(ctx context.Context) (coordinatorproto.DRPCCoordinatorClient, error) {
|
||||||
|
p, err := c.pool.GetOneOf(ctx, c.nodeConf.GetLast().CoordinatorPeers())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return coordinatorproto.NewDRPCCoordinatorClient(p), nil
|
||||||
|
}
|
||||||
@ -0,0 +1,95 @@
|
|||||||
|
// Code generated by MockGen. DO NOT EDIT.
|
||||||
|
// Source: github.com/anytypeio/any-sync/coordinator/coordinatorclient (interfaces: CoordinatorClient)
|
||||||
|
|
||||||
|
// Package mock_coordinatorclient is a generated GoMock package.
|
||||||
|
package mock_coordinatorclient
|
||||||
|
|
||||||
|
import (
|
||||||
|
context "context"
|
||||||
|
reflect "reflect"
|
||||||
|
|
||||||
|
app "github.com/anytypeio/any-sync/app"
|
||||||
|
coordinatorproto "github.com/anytypeio/any-sync/coordinator/coordinatorproto"
|
||||||
|
gomock "github.com/golang/mock/gomock"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MockCoordinatorClient is a mock of CoordinatorClient interface.
|
||||||
|
type MockCoordinatorClient struct {
|
||||||
|
ctrl *gomock.Controller
|
||||||
|
recorder *MockCoordinatorClientMockRecorder
|
||||||
|
}
|
||||||
|
|
||||||
|
// MockCoordinatorClientMockRecorder is the mock recorder for MockCoordinatorClient.
|
||||||
|
type MockCoordinatorClientMockRecorder struct {
|
||||||
|
mock *MockCoordinatorClient
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewMockCoordinatorClient creates a new mock instance.
|
||||||
|
func NewMockCoordinatorClient(ctrl *gomock.Controller) *MockCoordinatorClient {
|
||||||
|
mock := &MockCoordinatorClient{ctrl: ctrl}
|
||||||
|
mock.recorder = &MockCoordinatorClientMockRecorder{mock}
|
||||||
|
return mock
|
||||||
|
}
|
||||||
|
|
||||||
|
// EXPECT returns an object that allows the caller to indicate expected use.
|
||||||
|
func (m *MockCoordinatorClient) EXPECT() *MockCoordinatorClientMockRecorder {
|
||||||
|
return m.recorder
|
||||||
|
}
|
||||||
|
|
||||||
|
// FileLimitCheck mocks base method.
|
||||||
|
func (m *MockCoordinatorClient) FileLimitCheck(arg0 context.Context, arg1 string, arg2 []byte) (uint64, error) {
|
||||||
|
m.ctrl.T.Helper()
|
||||||
|
ret := m.ctrl.Call(m, "FileLimitCheck", arg0, arg1, arg2)
|
||||||
|
ret0, _ := ret[0].(uint64)
|
||||||
|
ret1, _ := ret[1].(error)
|
||||||
|
return ret0, ret1
|
||||||
|
}
|
||||||
|
|
||||||
|
// FileLimitCheck indicates an expected call of FileLimitCheck.
|
||||||
|
func (mr *MockCoordinatorClientMockRecorder) FileLimitCheck(arg0, arg1, arg2 interface{}) *gomock.Call {
|
||||||
|
mr.mock.ctrl.T.Helper()
|
||||||
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FileLimitCheck", reflect.TypeOf((*MockCoordinatorClient)(nil).FileLimitCheck), arg0, arg1, arg2)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Init mocks base method.
|
||||||
|
func (m *MockCoordinatorClient) Init(arg0 *app.App) error {
|
||||||
|
m.ctrl.T.Helper()
|
||||||
|
ret := m.ctrl.Call(m, "Init", arg0)
|
||||||
|
ret0, _ := ret[0].(error)
|
||||||
|
return ret0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Init indicates an expected call of Init.
|
||||||
|
func (mr *MockCoordinatorClientMockRecorder) Init(arg0 interface{}) *gomock.Call {
|
||||||
|
mr.mock.ctrl.T.Helper()
|
||||||
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Init", reflect.TypeOf((*MockCoordinatorClient)(nil).Init), arg0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Name mocks base method.
|
||||||
|
func (m *MockCoordinatorClient) Name() string {
|
||||||
|
m.ctrl.T.Helper()
|
||||||
|
ret := m.ctrl.Call(m, "Name")
|
||||||
|
ret0, _ := ret[0].(string)
|
||||||
|
return ret0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Name indicates an expected call of Name.
|
||||||
|
func (mr *MockCoordinatorClientMockRecorder) Name() *gomock.Call {
|
||||||
|
mr.mock.ctrl.T.Helper()
|
||||||
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Name", reflect.TypeOf((*MockCoordinatorClient)(nil).Name))
|
||||||
|
}
|
||||||
|
|
||||||
|
// SpaceSign mocks base method.
|
||||||
|
func (m *MockCoordinatorClient) SpaceSign(arg0 context.Context, arg1 string) (*coordinatorproto.SpaceReceiptWithSignature, error) {
|
||||||
|
m.ctrl.T.Helper()
|
||||||
|
ret := m.ctrl.Call(m, "SpaceSign", arg0, arg1)
|
||||||
|
ret0, _ := ret[0].(*coordinatorproto.SpaceReceiptWithSignature)
|
||||||
|
ret1, _ := ret[1].(error)
|
||||||
|
return ret0, ret1
|
||||||
|
}
|
||||||
|
|
||||||
|
// SpaceSign indicates an expected call of SpaceSign.
|
||||||
|
func (mr *MockCoordinatorClientMockRecorder) SpaceSign(arg0, arg1 interface{}) *gomock.Call {
|
||||||
|
mr.mock.ctrl.T.Helper()
|
||||||
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SpaceSign", reflect.TypeOf((*MockCoordinatorClient)(nil).SpaceSign), arg0, arg1)
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user