-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc.go
112 lines (96 loc) · 2.64 KB
/
rpc.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/***************************************************************
*
* Copyright (c) 2017, Menglong TAN <tanmenglong@gmail.com>
*
* This program is free software; you can redistribute it
* and/or modify it under the terms of the Apache licence
*
**************************************************************/
/**
*
*
* @file rpc.go
* @author Menglong TAN <tanmenglong@gmail.com>
* @date Fri May 5 17:40:17 2017
*
**/
package router
import (
"fmt"
"git.apache.org/thrift.git/lib/go/thrift"
"github.com/crackcell/nusadua/config"
"github.com/crackcell/nusadua/router/rpc"
"sync"
)
//===================================================================
// Public APIs
//===================================================================
type Rpc struct {
lock *sync.Mutex
started bool
server *thrift.TSimpleServer
stop chan bool
featureShard *FeatureShard
}
func NewRpc() *Rpc {
return &Rpc{
lock: new(sync.Mutex),
started: false,
stop: make(chan bool),
featureShard: NewFeatureShard([]string{}, 3),
}
}
func (this *Rpc) Start(host string, port int) (err error) {
this.lock.Lock()
defer this.lock.Unlock()
transportFactory := thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())
protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()
addr := fmt.Sprintf("%s:%d", host, port)
serverTransport, err := thrift.NewTServerSocket(addr)
if err != nil {
return err
}
processorFactory := thrift.NewTProcessorFactory(rpc.NewRouterServiceProcessor(this))
this.server = thrift.NewTSimpleServerFactory4(processorFactory, serverTransport, transportFactory, protocolFactory)
go func() {
this.server.Serve()
this.stop <- true
}()
this.started = true
return nil
}
func (this *Rpc) Stop() (err error) {
this.lock.Lock()
defer this.lock.Unlock()
if !this.started {
return nil
}
if err = this.server.Stop(); err != nil {
return err
}
this.started = false
return nil
}
func (this *Rpc) Wait() {
if !this.started {
return
}
<-this.stop
}
func (this *Rpc) SetNodes(nodes []string) (ex *rpc.RouterException, err error) {
this.featureShard.SetNodes(nodes, config.GlobalConfig.RouterConfig.ReplicaNum)
return nil, nil
}
func (this *Rpc) GetNodesByFeature(key []int64) (r []string, ex *rpc.RouterException, err error) {
if nodes, err := this.featureShard.GetNodesByFeature(key); err != nil {
ex := rpc.NewRouterException()
ex.Message = err.Error()
ex.Status = 1
return nodes, ex, err
} else {
return nodes, nil, nil
}
}
//===================================================================
// Private
//===================================================================