Skip to content

Commit e063d53

Browse files
egonelbrefjl
authored andcommitted
rpc: fix megacheck warnings
1 parent 4343780 commit e063d53

5 files changed

Lines changed: 24 additions & 62 deletions

File tree

‎rpc/server.go‎

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,7 @@ import (
2929
"gopkg.in/fatih/set.v0"
3030
)
3131

32-
const (
33-
notificationBufferSize = 10000 // max buffered notifications before codec is closed
34-
35-
MetadataApi = "rpc"
36-
)
32+
const MetadataApi = "rpc"
3733

3834
// CodecOption specifies which type of messages this codec supports
3935
type CodecOption int
@@ -49,10 +45,9 @@ const (
4945
// NewServer will create a new server instance with no registered handlers.
5046
func NewServer() *Server {
5147
server := &Server{
52-
services: make(serviceRegistry),
53-
subscriptions: make(subscriptionRegistry),
54-
codecs: set.New(),
55-
run: 1,
48+
services: make(serviceRegistry),
49+
codecs: set.New(),
50+
run: 1,
5651
}
5752

5853
// register a default service which will provide meta information about the RPC service such as the services and
@@ -124,16 +119,6 @@ func (s *Server) RegisterName(name string, rcvr interface{}) error {
124119
return nil
125120
}
126121

127-
// hasOption returns true if option is included in options, otherwise false
128-
func hasOption(option CodecOption, options []CodecOption) bool {
129-
for _, o := range options {
130-
if option == o {
131-
return true
132-
}
133-
}
134-
return false
135-
}
136-
137122
// serveRequest will reads requests from the codec, calls the RPC callback and
138123
// writes the response to the given codec.
139124
//
@@ -148,13 +133,11 @@ func (s *Server) serveRequest(codec ServerCodec, singleShot bool, options CodecO
148133
const size = 64 << 10
149134
buf := make([]byte, size)
150135
buf = buf[:runtime.Stack(buf, false)]
151-
log.Error(fmt.Sprint(string(buf)))
136+
log.Error(string(buf))
152137
}
153138
s.codecsMu.Lock()
154139
s.codecs.Remove(codec)
155140
s.codecsMu.Unlock()
156-
157-
return
158141
}()
159142

160143
ctx, cancel := context.WithCancel(context.Background())
@@ -246,7 +229,7 @@ func (s *Server) ServeSingleRequest(codec ServerCodec, options CodecOption) {
246229
// close all codecs which will cancel pending requests/subscriptions.
247230
func (s *Server) Stop() {
248231
if atomic.CompareAndSwapInt32(&s.run, 1, 0) {
249-
log.Debug(fmt.Sprint("RPC Server shutdown initiatied"))
232+
log.Debug("RPC Server shutdown initiatied")
250233
s.codecsMu.Lock()
251234
defer s.codecsMu.Unlock()
252235
s.codecs.Each(func(c interface{}) bool {

‎rpc/subscription.go‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ type notifierKey struct{}
5353
type Notifier struct {
5454
codec ServerCodec
5555
subMu sync.RWMutex // guards active and inactive maps
56-
stopped bool
5756
active map[ID]*Subscription
5857
inactive map[ID]*Subscription
5958
}

‎rpc/subscription_test.go‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func TestNotifications(t *testing.T) {
165165
}
166166

167167
func waitForMessages(t *testing.T, in *json.Decoder, successes chan<- jsonSuccessResponse,
168-
failures chan<- jsonErrResponse, notifications chan<- jsonNotification) {
168+
failures chan<- jsonErrResponse, notifications chan<- jsonNotification, errors chan<- error) {
169169

170170
// read and parse server messages
171171
for {
@@ -177,12 +177,14 @@ func waitForMessages(t *testing.T, in *json.Decoder, successes chan<- jsonSucces
177177
var responses []map[string]interface{}
178178
if rmsg[0] == '[' {
179179
if err := json.Unmarshal(rmsg, &responses); err != nil {
180-
t.Fatalf("Received invalid message: %s", rmsg)
180+
errors <- fmt.Errorf("Received invalid message: %s", rmsg)
181+
return
181182
}
182183
} else {
183184
var msg map[string]interface{}
184185
if err := json.Unmarshal(rmsg, &msg); err != nil {
185-
t.Fatalf("Received invalid message: %s", rmsg)
186+
errors <- fmt.Errorf("Received invalid message: %s", rmsg)
187+
return
186188
}
187189
responses = append(responses, msg)
188190
}
@@ -216,7 +218,7 @@ func waitForMessages(t *testing.T, in *json.Decoder, successes chan<- jsonSucces
216218
}
217219
continue
218220
}
219-
t.Fatalf("Received invalid message: %s", msg)
221+
errors <- fmt.Errorf("Received invalid message: %s", msg)
220222
}
221223
}
222224
}
@@ -235,6 +237,8 @@ func TestSubscriptionMultipleNamespaces(t *testing.T) {
235237
successes = make(chan jsonSuccessResponse)
236238
failures = make(chan jsonErrResponse)
237239
notifications = make(chan jsonNotification)
240+
241+
errors = make(chan error, 10)
238242
)
239243

240244
// setup and start server
@@ -248,7 +252,7 @@ func TestSubscriptionMultipleNamespaces(t *testing.T) {
248252
defer server.Stop()
249253

250254
// wait for message and write them to the given channels
251-
go waitForMessages(t, in, successes, failures, notifications)
255+
go waitForMessages(t, in, successes, failures, notifications, errors)
252256

253257
// create subscriptions one by one
254258
n := 3
@@ -297,6 +301,8 @@ func TestSubscriptionMultipleNamespaces(t *testing.T) {
297301
}
298302

299303
select {
304+
case err := <-errors:
305+
t.Fatal(err)
300306
case suc := <-successes: // subscription created
301307
subids[namespaces[int(suc.Id.(float64))]] = suc.Result.(string)
302308
case failure := <-failures:

‎rpc/types.go‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ type callback struct {
4848
// service represents a registered object
4949
type service struct {
5050
name string // name for service
51-
rcvr reflect.Value // receiver of methods for the service
5251
typ reflect.Type // receiver type
5352
callbacks callbacks // registered handlers
5453
subscriptions subscriptions // available subscriptions/notifications
@@ -58,23 +57,19 @@ type service struct {
5857
type serverRequest struct {
5958
id interface{}
6059
svcname string
61-
rcvr reflect.Value
6260
callb *callback
6361
args []reflect.Value
6462
isUnsubscribe bool
6563
err Error
6664
}
6765

68-
type serviceRegistry map[string]*service // collection of services
69-
type callbacks map[string]*callback // collection of RPC callbacks
70-
type subscriptions map[string]*callback // collection of subscription callbacks
71-
type subscriptionRegistry map[string]*callback // collection of subscription callbacks
66+
type serviceRegistry map[string]*service // collection of services
67+
type callbacks map[string]*callback // collection of RPC callbacks
68+
type subscriptions map[string]*callback // collection of subscription callbacks
7269

7370
// Server represents a RPC server
7471
type Server struct {
75-
services serviceRegistry
76-
muSubcriptions sync.Mutex // protects subscriptions
77-
subscriptions subscriptionRegistry
72+
services serviceRegistry
7873

7974
run int32
8075
codecsMu sync.Mutex

‎rpc/utils.go‎

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -119,21 +119,6 @@ func isHexNum(t reflect.Type) bool {
119119
return t == bigIntType
120120
}
121121

122-
var blockNumberType = reflect.TypeOf((*BlockNumber)(nil)).Elem()
123-
124-
// Indication if the given block is a BlockNumber
125-
func isBlockNumber(t reflect.Type) bool {
126-
if t == nil {
127-
return false
128-
}
129-
130-
for t.Kind() == reflect.Ptr {
131-
t = t.Elem()
132-
}
133-
134-
return t == blockNumberType
135-
}
136-
137122
// suitableCallbacks iterates over the methods of the given type. It will determine if a method satisfies the criteria
138123
// for a RPC callback or a subscription callback and adds it to the collection of callbacks or subscriptions. See server
139124
// documentation for a summary of these criteria.
@@ -210,18 +195,12 @@ METHODS:
210195
}
211196

212197
switch mtype.NumOut() {
213-
case 0, 1:
214-
break
215-
case 2:
216-
if h.errPos == -1 { // method must one return value and 1 error
198+
case 0, 1, 2:
199+
if mtype.NumOut() == 2 && h.errPos == -1 { // method must one return value and 1 error
217200
continue METHODS
218201
}
219-
break
220-
default:
221-
continue METHODS
202+
callbacks[mname] = &h
222203
}
223-
224-
callbacks[mname] = &h
225204
}
226205

227206
return callbacks, subscriptions

0 commit comments

Comments
 (0)