whisper: refactoring

This commit is contained in:
Vlad
2018-02-23 14:52:25 +01:00
parent b677a07d36
commit 6919c36432
3 changed files with 3 additions and 51 deletions

View File

@ -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