Skip to content

Commit

Permalink
Merge pull request ethereum#16176 from gluk256/255-refactoring
Browse files Browse the repository at this point in the history
whisper: filters no longer get removed after a while
  • Loading branch information
gballet authored Feb 23, 2018
2 parents 1147389 + 6919c36 commit 423c8bb
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 51 deletions.
23 changes: 0 additions & 23 deletions whisper/whisperv5/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,32 +60,9 @@ func NewPublicWhisperAPI(w *Whisper) *PublicWhisperAPI {
w: w,
lastUsed: make(map[string]time.Time),
}

go api.run()
return api
}

// run the api event loop.
// this loop deletes filter that have not been used within filterTimeout
func (api *PublicWhisperAPI) run() {
timeout := time.NewTicker(2 * time.Minute)
for {
<-timeout.C

api.mu.Lock()
for id, lastUsed := range api.lastUsed {
if time.Since(lastUsed).Seconds() >= filterTimeout {
delete(api.lastUsed, id)
if err := api.w.Unsubscribe(id); err != nil {
log.Error("could not unsubscribe whisper filter", "error", err)
}
log.Debug("delete whisper filter (timeout)", "id", id)
}
}
api.mu.Unlock()
}
}

// Version returns the Whisper sub-protocol version.
func (api *PublicWhisperAPI) Version(ctx context.Context) string {
return ProtocolVersionStr
Expand Down
26 changes: 2 additions & 24 deletions whisper/whisperv6/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,32 +61,9 @@ func NewPublicWhisperAPI(w *Whisper) *PublicWhisperAPI {
w: w,
lastUsed: make(map[string]time.Time),
}

go api.run()
return api
}

// run the api event loop.
// this loop deletes filter that have not been used within filterTimeout
func (api *PublicWhisperAPI) run() {
timeout := time.NewTicker(2 * time.Minute)
for {
<-timeout.C

api.mu.Lock()
for id, lastUsed := range api.lastUsed {
if time.Since(lastUsed).Seconds() >= filterTimeout {
delete(api.lastUsed, id)
if err := api.w.Unsubscribe(id); err != nil {
log.Error("could not unsubscribe whisper filter", "error", err)
}
log.Debug("delete whisper filter (timeout)", "id", id)
}
}
api.mu.Unlock()
}
}

// Version returns the Whisper sub-protocol version.
func (api *PublicWhisperAPI) Version(ctx context.Context) string {
return ProtocolVersionStr
Expand Down Expand Up @@ -219,7 +196,8 @@ func (api *PublicWhisperAPI) DeleteSymKey(ctx context.Context, id string) bool {
return api.w.DeleteSymKey(id)
}

// MakeLightClient turns the node into light client, which does not forward any incoming messages.
// MakeLightClient turns the node into light client, which does not forward
// any incoming messages, and sends only messages originated in this node.
func (api *PublicWhisperAPI) MakeLightClient(ctx context.Context) bool {
api.w.lightClient = true
return api.w.lightClient
Expand Down
5 changes: 1 addition & 4 deletions whisper/whisperv6/whisper.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,10 +590,7 @@ func (whisper *Whisper) Unsubscribe(id string) error {
// network in the coming cycles.
func (whisper *Whisper) Send(envelope *Envelope) error {
ok, err := whisper.add(envelope, false)
if err != nil {
return err
}
if !ok {
if err == nil && !ok {
return fmt.Errorf("failed to add envelope")
}
return err
Expand Down

0 comments on commit 423c8bb

Please sign in to comment.