Skip to content
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

Implement SSE events transport. Add new event types. Update README #79

Merged
merged 1 commit into from
Nov 5, 2015
Merged

Implement SSE events transport. Add new event types. Update README #79

merged 1 commit into from
Nov 5, 2015

Conversation

x-cray
Copy link
Contributor

@x-cray x-cray commented Oct 28, 2015

Kind of fixes #19
Please review the PR, I'm a bit new to Go

@eicca
Copy link
Collaborator

eicca commented Oct 28, 2015

As current code doesn't fully pass golint, would it make sense to write the new code in a proper way, or in a consistent way? Writing the golintable code would make it easier to refactor the old code in the future, however it will look inconsistent. What do you think, what's better?

for {
event := <-update
glog.Infof("EVENT: %s", event)
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you're touching the examples anyway, what do you think about removing else statements from them?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'll also modify actual example code file.

@eicca
Copy link
Collaborator

eicca commented Oct 28, 2015

I wonder if it's possible to have some tests for this addition. Do you have any thoughts about this?

@x-cray
Copy link
Contributor Author

x-cray commented Oct 28, 2015

@eicca As I mentioned earlier, I'm new to Go =) I've checked golint output and it seems that there are many places it complains about. I'll fix points which are related to this PR, but IMO others should be fixed is separate PRs to maintain consistent intent of this specific PR.

@x-cray
Copy link
Contributor Author

x-cray commented Oct 28, 2015

@eicca In terms of tests I think that we might mock SSE server like in this example https://github.com/donovanhide/eventsource/blob/master/example_event_test.go and send predefined set of events expecting to receive them from this channel https://github.com/gambol99/go-marathon/blob/master/subscription.go#L47

This was referenced Oct 29, 2015
}
// Try to connect to Marathon until succeed or
// the whole custer is down
for {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 definitely easier to read when using the for loop ...

@gambol99
Copy link
Owner

👍 LGTM

@x-cray
Copy link
Contributor Author

x-cray commented Oct 30, 2015

@gambol99 I'd like to add some tests to it.

// Prevent multiple SSE subscriptions
if r.subscribedToSSE {
return nil
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's still could be affected by a race condition. Would it be better to try to use https://golang.org/pkg/sync/#Once? Or that would be totally users fault to achieve the race condition here and the boolean field is enough? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eicca Good point. But I see another issue here: registerSSESubscription() is only called from within RegisterSubscription() which in turn is only called from AddEventsListener() which is already synchronised. The issue is that RegisterSubscription itself is being exported while it is not defined in Marathon interface. I think that we could just rename RegisterSubscription to registerSubscription and that's it. What do you think?

@x-cray
Copy link
Contributor Author

x-cray commented Nov 2, 2015

@gambol99 @eicca Question: should Marathon.UnSubscribe() be exported to interface? It is being called automatically upon event listener removal in Marathon.RemoveEventsListener(). Would it be better is we don't export it? What's the purpose of this method if we call it from outside while having some event subscriptions active?
I'm not even sure that this line is correct since UnSubscribe() should have been already called earlier.

@eicca
Copy link
Collaborator

eicca commented Nov 2, 2015

@x-cray I'm sorry for the lack of activity in this PR, unfortunately I don't have enough knowledge of the marathon events. I can try to dig into it a bit later once I have more time.

@gambol99
Copy link
Owner

gambol99 commented Nov 2, 2015

@x-cray good spot! and no under this context it shouldn't really be exported, neither should the RegisterSubscription() from the looks for it, as the {Add,Remove}EventsListener() methods can handled it all. Off the top of my head though, it might be useful to keep the method but change its function slightly

from
func (r *marathonClient) UnSubscribe() error 
to 
func (r *marathonClient) UnSubscribe(url string) error

And then in https://github.com/gambol99/go-marathon/blob/master/subscription.go#L66 where it checks for any more listeners we could change to

if len(r.listeners) <= 0 {
    r.UnSubscriber(r.SubscriptionURL())
}

Internally the consumer should use the Add/RemoveEventslisten(channel) to handle events, but they can also have a generic method to delete subscriptions(url) if they like, i.e. list all and clean up subscriptions unrelated to the Add/Register methods etc ... what you reckon? ..

@x-cray
Copy link
Contributor Author

x-cray commented Nov 3, 2015

@gambol99 In my opinion your abstraction over Marathon events API with {Add/Remove}EventsListener() is really great and it makes UnSubscribe() unnecessary. However, your suggestion to add url param adds some sense to it, but only in situation when we need to unsubscribe some third-party subscriber. Could be. Will you make this change in upstream?

@gambol99
Copy link
Owner

gambol99 commented Nov 3, 2015

@x-cray

However, your suggestion to add url param adds some sense to it, but only in situation when we
need to unsubscribe some third-party subscriber

agreed ..

Will you make this change in upstream?

If this PR doesn't need to change it i'm happy to raise the change and PR myself? ...

@x-cray
Copy link
Contributor Author

x-cray commented Nov 3, 2015

If this PR doesn't need to change it i'm happy to raise the change and PR myself? ...

I think that it's not related to PR. I've only changed this line by adding check for correct EventsTransport to unsubscribe only when using callback event transport.

For the rest, I'm happy with all my changes now =) I've added functional tests for event stream. The most notable change in unit testing runtime infrastructure is the way how test HTTP server is started now — for every test case a new instance is being started and stopped afterwards. That was required for event stream testing and in general it's always good practice to prepare test environment from scratch for every distinct test and then dispose it.

"github.com/donovanhide/eventsource"
"github.com/golang/glog"
yaml "gopkg.in/yaml.v2"
"sync"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'sync' should probably be placed in the above section

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, missed that one

@gambol99
Copy link
Owner

gambol99 commented Nov 4, 2015

Aside from the one tiny comment ... LGTM ... awesome PR and cheers for fixing up alot of the linting issues

Move GetEvent() to events.go as it as nothing to do in subscription.
Introduce separate type for EventsTransport.
Update README, add SSE example, make other examples to be in line with golint rules.
Don't export registerSubscription() method.
Add functional tests. Fix functional tests runtime infrastructure.
gambol99 added a commit that referenced this pull request Nov 5, 2015
Implement SSE events transport. Add new event types. Update README
@gambol99 gambol99 merged commit 40f4d42 into gambol99:master Nov 5, 2015
@gambol99 gambol99 mentioned this pull request Nov 5, 2015
@x-cray x-cray deleted the sse-transport branch November 6, 2015 09:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unreliable IP retrieval for Event Subscription
3 participants