-
Notifications
You must be signed in to change notification settings - Fork 75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
www: Update wsDcrdata context #933
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"sync" | ||
"time" | ||
|
||
client "github.com/decred/dcrdata/pubsub/v2/psclient" | ||
"github.com/decred/dcrdata/semver" | ||
"github.com/decred/politeia/util" | ||
) | ||
|
||
const ( | ||
// addrSubPrefix must be prefixed to the address when subscribing | ||
// to address events. | ||
addrSubPrefix = "address:" | ||
) | ||
|
||
var ( | ||
// errDuplicateSub is emitted when attempting to subscribe to an | ||
// event that has already been subscribed to. | ||
errDuplicateSub = errors.New("duplicate subscription") | ||
|
||
// errSubNotFound is emitted when attempting to unsubscribe to an | ||
// event that has not yet been subscribed to. | ||
errSubNotFound = errors.New("subscription not found") | ||
) | ||
|
||
// wsDcrdata is the context used for managing a dcrdata websocket connection. | ||
type wsDcrdata struct { | ||
sync.RWMutex | ||
client *client.Client // dcrdata websocket client | ||
subscriptions map[string]struct{} // Client subscriptions | ||
} | ||
|
||
// addSub adds an event subscription to the subscriptions map. | ||
func (w *wsDcrdata) addSub(event string) { | ||
w.Lock() | ||
defer w.Unlock() | ||
|
||
w.subscriptions[event] = struct{}{} | ||
} | ||
|
||
// removeSub removes an event subscription from the subscriptions map. | ||
func (w *wsDcrdata) removeSub(event string) { | ||
w.Lock() | ||
defer w.Unlock() | ||
|
||
delete(w.subscriptions, event) | ||
} | ||
|
||
// isSubscribed returns whether the client is subscribed to the provided event. | ||
func (w *wsDcrdata) isSubscribed(event string) bool { | ||
w.RLock() | ||
defer w.RUnlock() | ||
|
||
_, ok := w.subscriptions[event] | ||
return ok | ||
} | ||
|
||
// subToPing subscribes to the dcrdata ping event. | ||
func (w *wsDcrdata) subToPing() error { | ||
_, err := w.client.Subscribe("ping") | ||
if err != nil { | ||
return fmt.Errorf("failed to subscribe: %v", err) | ||
} | ||
log.Debugf("wsDcrdata subscribed to ping") | ||
return nil | ||
} | ||
|
||
// subToAddr subscribes to dcrdata events for the provided address. | ||
func (w *wsDcrdata) subToAddr(address string) error { | ||
event := addrSubPrefix + address | ||
if w.isSubscribed(event) { | ||
return errDuplicateSub | ||
} | ||
_, err := w.client.Subscribe(event) | ||
if err != nil { | ||
return fmt.Errorf("failed to subscribe: %v", err) | ||
} | ||
w.addSub(event) | ||
log.Debugf("wsDcrdata subscribed to addr: %v", address) | ||
return nil | ||
} | ||
|
||
// unsubFromAddr unsubscribes from dcrdata events for the provided address. | ||
func (w *wsDcrdata) unsubFromAddr(address string) error { | ||
event := addrSubPrefix + address | ||
if !w.isSubscribed(event) { | ||
return errSubNotFound | ||
} | ||
_, err := w.client.Unsubscribe(event) | ||
if err != nil { | ||
return fmt.Errorf("failed to unsubscribe: %v", err) | ||
} | ||
w.removeSub(event) | ||
log.Debugf("wsDcrdata unsubscribed from addr: %v", address) | ||
return nil | ||
} | ||
|
||
// newWSDcrdata return a new wsDcrdata context. | ||
func newWSDcrdata() (*wsDcrdata, error) { | ||
// Init websocket client | ||
u, err := util.BlockExplorerURLForSubscriptions(activeNetParams.Params) | ||
if err != nil { | ||
return nil, err | ||
} | ||
opts := client.Opts{ | ||
ReadTimeout: 3 * time.Second, | ||
WriteTimeout: 3 * time.Second, | ||
} | ||
c, err := client.New(u, context.Background(), &opts) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to connect to %v: %v", u, err) | ||
} | ||
|
||
log.Infof("Dcrdata websocket host: %v", u) | ||
|
||
// Check client and server compatibility | ||
v, err := c.ServerVersion() | ||
if err != nil { | ||
return nil, fmt.Errorf("server version failed: %v", err) | ||
} | ||
serverSemVer := semver.NewSemver(v.Major, v.Minor, v.Patch) | ||
clientSemVer := client.Version() | ||
if !semver.Compatible(clientSemVer, serverSemVer) { | ||
return nil, fmt.Errorf("version mismatch; client %v, server %v", | ||
serverSemVer, clientSemVer) | ||
} | ||
|
||
log.Infof("Dcrdata pubsub server version: %v, client version %v", | ||
serverSemVer, clientSemVer) | ||
|
||
return &wsDcrdata{ | ||
client: c, | ||
subscriptions: make(map[string]struct{}), | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we not need to verify that p.wsDcrdata.client is not nil here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No because
wsDcrdata
is initialized using the functionnewWSDcrdata
, which ensures that the client is setup correctly. If it's being initialized without usingnewWSDcrdata
then its being initialized incorrectly.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool thanks for clarifying. Looks good.