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

Set extra headers for outbound messaging as late as possible #600

Merged
merged 4 commits into from
Jan 11, 2019

Conversation

iky
Copy link
Contributor

@iky iky commented Jan 11, 2019

Allows for in-entrypoint context data updates. This is useful when setting a language for example:

class LanguageContext(DependencyProvider):

    def get_dependency(self, worker_ctx):
        @contextmanager
        def set_language(language):
            original_language = worker_ctx.data.get('language')
            worker_ctx.data['language'] = language
            yield
            worker_ctx.data['language'] = original_language
        return set_language

class ExampleService(object):

    name = 'exampleservice'

    language_context = LanguageContext()

    @rpc
    def greet_in_french(self):
        with self.language_context('fr'):
            return self.example_rpc.say_hello()

Another use we have is services signing up outbound calls with its own authentication.

I removed the lazy load and caching of call_id and call_id_stack of worker context. These properties are super-cheap and most of the time loaded anyway (if entrypoint contains an outbound call, or in logging debug call_id_stack is logged by the container, if tracer is on and so on...)

Allows for in-entrypoint context data updates.
@iky iky changed the title [WIP] Set extra headers for outbound rpc as late as possible Set extra headers for outbound rpc as late as possible Jan 11, 2019
Copy link
Member

@mattbennett mattbennett left a comment

Choose a reason for hiding this comment

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

Thanks @iky.

I think this same change needs applying to the Publisher and EventDispatcher, because this same issue is in messaging.py:

nameko/nameko/messaging.py

Lines 116 to 124 in b0131f5

def get_dependency(self, worker_ctx):
extra_headers = encode_to_headers(worker_ctx.context_data)
def publish(msg, **kwargs):
self.publisher.publish(
msg, extra_headers=extra_headers, **kwargs
)
return publish

@timbu
Copy link
Contributor

timbu commented Jan 11, 2019

@mattbennett Is this the same problem from here? https://github.com/nameko/nameko/pull/573/files

@mattbennett
Copy link
Member

Gah, yes it is! Sorry @timbu, I completely forgot about that PR.

Interesting that this was introduced for the non-RPC extensions before the big refactor that landed in 3.x.

@mattbennett
Copy link
Member

I guess we should land #573 and then bring master into the v3 branch.

@iky
Copy link
Contributor Author

iky commented Jan 11, 2019

Ah, did not see that. @timbu @mattbennett It's not just Publisher of messaging module, EventDispatcher of events module needs to be changed too, see 5c416ce

@iky iky force-pushed the in-entrypoint-context-data-write branch from 5c416ce to 8ed4356 Compare January 11, 2019 13:24
@iky
Copy link
Contributor Author

iky commented Jan 11, 2019

I've updated the remaining places. The fixes are tiny, maybe we can merge this to 3.0.0 before #573 lands in master and then dealing with merging the two together?

@mattbennett
Copy link
Member

@timbu @mattbennett It's not just Publisher of messaging module, EventDispatcher of events module needs to be changed too, see 5c416ce

Ah, quite right. And that means #573 is missing the equivalent fix for the event dispatcher.

I agree, let's make the fixes here, then #573 can land, then we'll merge master into the v3rc branch.

@iky iky changed the title Set extra headers for outbound rpc as late as possible Set extra headers for outbound messaging as late as possible Jan 11, 2019
Copy link
Member

@kooba kooba left a comment

Choose a reason for hiding this comment

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

Just one small thing

@@ -507,10 +504,12 @@ class Client(object):
"""

def __init__(
self, publish, register_for_reply, service_name=None, method_name=None
self, publish, register_for_reply, context_data,
Copy link
Member

Choose a reason for hiding this comment

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

Might want to add context_data to :Parameters: doc above.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good catch, thanks! .)

Copy link
Member

@mattbennett mattbennett left a comment

Choose a reason for hiding this comment

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

Awesome!

@mattbennett mattbennett merged commit 4118a8c into nameko:v3.0.0-rc Jan 11, 2019
@iky iky deleted the in-entrypoint-context-data-write branch January 11, 2019 15:52
@kooba
Copy link
Member

kooba commented Jan 11, 2019

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants