Skip to content

Commit 982f73f

Browse files
committed
Added timeout for filter & removed clipboard. Closes #350
1 parent 0006585 commit 982f73f

File tree

7 files changed

+129
-82
lines changed

7 files changed

+129
-82
lines changed

rpc/packages.go

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"math/big"
1414
"strings"
1515
"sync"
16+
"time"
1617

1718
"github.com/ethereum/go-ethereum/core"
1819
"github.com/ethereum/go-ethereum/core/types"
@@ -31,13 +32,14 @@ const (
3132

3233
type EthereumApi struct {
3334
xeth *xeth.XEth
35+
quit chan struct{}
3436
filterManager *filter.FilterManager
3537

3638
logMut sync.RWMutex
37-
logs map[int]state.Logs
39+
logs map[int]*logFilter
3840

3941
messagesMut sync.RWMutex
40-
messages map[int][]xeth.WhisperMessage
42+
messages map[int]*whisperFilter
4143
// Register keeps a list of accounts and transaction data
4244
regmut sync.Mutex
4345
register map[string][]*NewTxArgs
@@ -49,12 +51,14 @@ func NewEthereumApi(eth *xeth.XEth) *EthereumApi {
4951
db, _ := ethdb.NewLDBDatabase("dapps")
5052
api := &EthereumApi{
5153
xeth: eth,
54+
quit: make(chan struct{}),
5255
filterManager: filter.NewFilterManager(eth.Backend().EventMux()),
53-
logs: make(map[int]state.Logs),
54-
messages: make(map[int][]xeth.WhisperMessage),
56+
logs: make(map[int]*logFilter),
57+
messages: make(map[int]*whisperFilter),
5558
db: db,
5659
}
5760
go api.filterManager.Start()
61+
go api.start()
5862

5963
return api
6064
}
@@ -97,7 +101,11 @@ func (self *EthereumApi) NewFilter(args *FilterOptions, reply *interface{}) erro
97101
self.logMut.Lock()
98102
defer self.logMut.Unlock()
99103

100-
self.logs[id] = append(self.logs[id], logs...)
104+
if self.logs[id] == nil {
105+
self.logs[id] = &logFilter{timeout: time.Now()}
106+
}
107+
108+
self.logs[id].add(logs...)
101109
}
102110
id = self.filterManager.InstallFilter(filter)
103111
*reply = id
@@ -113,7 +121,11 @@ func (self *EthereumApi) NewFilterString(args string, reply *interface{}) error
113121
self.logMut.Lock()
114122
defer self.logMut.Unlock()
115123

116-
self.logs[id] = append(self.logs[id], &state.StateLog{})
124+
if self.logs[id] == nil {
125+
self.logs[id] = &logFilter{timeout: time.Now()}
126+
}
127+
128+
self.logs[id].add(&state.StateLog{})
117129
}
118130
if args == "pending" {
119131
filter.PendingCallback = callback
@@ -131,9 +143,9 @@ func (self *EthereumApi) FilterChanged(id int, reply *interface{}) error {
131143
self.logMut.Lock()
132144
defer self.logMut.Unlock()
133145

134-
*reply = toLogs(self.logs[id])
135-
136-
self.logs[id] = nil // empty the logs
146+
if self.logs[id] != nil {
147+
*reply = toLogs(self.logs[id].get())
148+
}
137149

138150
return nil
139151
}
@@ -331,7 +343,10 @@ func (p *EthereumApi) NewWhisperFilter(args *xeth.Options, reply *interface{}) e
331343
args.Fn = func(msg xeth.WhisperMessage) {
332344
p.messagesMut.Lock()
333345
defer p.messagesMut.Unlock()
334-
p.messages[id] = append(p.messages[id], msg)
346+
if p.messages[id] == nil {
347+
p.messages[id] = &whisperFilter{timeout: time.Now()}
348+
}
349+
p.messages[id].add(msg) // = append(p.messages[id], msg)
335350
}
336351
id = p.xeth.Whisper().Watch(args)
337352
*reply = id
@@ -342,9 +357,9 @@ func (self *EthereumApi) MessagesChanged(id int, reply *interface{}) error {
342357
self.messagesMut.Lock()
343358
defer self.messagesMut.Unlock()
344359

345-
*reply = self.messages[id]
346-
347-
self.messages[id] = nil // empty the messages
360+
if self.messages[id] != nil {
361+
*reply = self.messages[id].get()
362+
}
348363

349364
return nil
350365
}
@@ -535,3 +550,34 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
535550
rpclogger.DebugDetailf("Reply: %T %s", reply, reply)
536551
return nil
537552
}
553+
554+
var filterTickerTime = 15 * time.Second
555+
556+
func (self *EthereumApi) start() {
557+
timer := time.NewTicker(filterTickerTime)
558+
done:
559+
for {
560+
select {
561+
case <-timer.C:
562+
self.logMut.Lock()
563+
self.messagesMut.Lock()
564+
for id, filter := range self.logs {
565+
if time.Since(filter.timeout) > 20*time.Second {
566+
delete(self.logs, id)
567+
}
568+
}
569+
570+
for id, filter := range self.messages {
571+
if time.Since(filter.timeout) > 20*time.Second {
572+
delete(self.messages, id)
573+
}
574+
}
575+
case <-self.quit:
576+
break done
577+
}
578+
}
579+
}
580+
581+
func (self *EthereumApi) stop() {
582+
close(self.quit)
583+
}

rpc/packages_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package rpc
2+
3+
import (
4+
"sync"
5+
"testing"
6+
"time"
7+
)
8+
9+
func TestFilterClose(t *testing.T) {
10+
api := &EthereumApi{
11+
logs: make(map[int]*logFilter),
12+
messages: make(map[int]*whisperFilter),
13+
quit: make(chan struct{}),
14+
}
15+
16+
filterTickerTime = 1
17+
api.logs[0] = &logFilter{}
18+
api.messages[0] = &whisperFilter{}
19+
var wg sync.WaitGroup
20+
wg.Add(1)
21+
go api.start()
22+
go func() {
23+
select {
24+
case <-time.After(500 * time.Millisecond):
25+
api.stop()
26+
wg.Done()
27+
}
28+
}()
29+
wg.Wait()
30+
if len(api.logs) != 0 {
31+
t.Error("expected logs to be empty")
32+
}
33+
34+
if len(api.messages) != 0 {
35+
t.Error("expected messages to be empty")
36+
}
37+
}

rpc/util.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ import (
2020
"encoding/json"
2121
"io"
2222
"net/http"
23+
"time"
2324

2425
"github.com/ethereum/go-ethereum/ethutil"
2526
"github.com/ethereum/go-ethereum/logger"
2627
"github.com/ethereum/go-ethereum/state"
28+
"github.com/ethereum/go-ethereum/xeth"
2729
)
2830

2931
var rpclogger = logger.NewLogger("RPC")
@@ -100,3 +102,34 @@ func toLogs(logs state.Logs) (ls []Log) {
100102

101103
return
102104
}
105+
106+
type whisperFilter struct {
107+
messages []xeth.WhisperMessage
108+
timeout time.Time
109+
}
110+
111+
func (w *whisperFilter) add(msgs ...xeth.WhisperMessage) {
112+
w.messages = append(w.messages, msgs...)
113+
}
114+
func (w *whisperFilter) get() []xeth.WhisperMessage {
115+
w.timeout = time.Now()
116+
tmp := w.messages
117+
w.messages = nil
118+
return tmp
119+
}
120+
121+
type logFilter struct {
122+
logs state.Logs
123+
timeout time.Time
124+
}
125+
126+
func (l *logFilter) add(logs ...state.Log) {
127+
l.logs = append(l.logs, logs...)
128+
}
129+
130+
func (l *logFilter) get() state.Logs {
131+
l.timeout = time.Now()
132+
tmp := l.logs
133+
l.logs = nil
134+
return tmp
135+
}

ui/qt/clipboard/capi.hpp

Lines changed: 0 additions & 11 deletions
This file was deleted.

ui/qt/clipboard/clipboard.cpp

Lines changed: 0 additions & 20 deletions
This file was deleted.

ui/qt/clipboard/clipboard.go

Lines changed: 0 additions & 15 deletions
This file was deleted.

ui/qt/clipboard/clipboard.hpp

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)