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

The size of the session item is bigger than the actual data size of the one when using CosmosDBSessionStateProviderAsync #20

Closed
atsushina opened this issue Apr 3, 2019 · 1 comment

Comments

@atsushina
Copy link

Title
The size of the session item is bigger than the actual data size of the one when using CosmosDBSessionStateProviderAsync.

Functional impact
The user need to increase the RU of the Cosmos DB Provider more than enough.

Minimal repro steps

  1. Create a web application project
  2. install CosmosDBSessionStateProviderAsync 1.0.1 nupkg and configure it
  3. Write a small Session data in an aspx (Session["test"] = "test")

Expected result
The size of the session data is dozens of bytes at most.

Actual result
The size of the session data is hundreds of bytes.

How to fix
Use the MemoryStream.ToAttay() instead of MemoryStream.GetBuffer at the following line.

https://github.com/aspnet/AspNetSessionState/blob/master/src/CosmosDBSessionStateProviderAsync/CosmosDBSessionStateProviderAsync.cs#L1148
https://github.com/aspnet/AspNetSessionState/blob/master/src/CosmosDBSessionStateProviderAsync/CosmosDBSessionStateProviderAsync.cs#L1161

Further technical details
The CosmosDBSessionStateProviderAsync get the session item which the application set by calling MemoryStream.GetBuffer().

But the MemoryStream.GetBuffer() return the memory which includes the unused one.

MemoryStream.GetBuffer Method

For example, if the string "test" is written into the MemoryStream object, the length of the buffer returned from GetBuffer is 256, not 4, with 252 bytes unused. To obtain only the data in the buffer, use the ToArray method; however, ToArray creates a copy of the data in memory.

So the size of the memory becomes 256 * 2^N (256, 512, 1024, 2048, 4096 ...) if the actual data size is over 256 * 2^(N-1).

Therefore the size of the session item is bigger than the actual data size of the one.

@StephenMolloy
Copy link
Collaborator

Fixed with #25

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

No branches or pull requests

2 participants