node-dhcp icon indicating copy to clipboard operation
node-dhcp copied to clipboard

Handle persistence with DB layer

Open infusion opened this issue 7 years ago • 16 comments

Implement session storage of the server in either

  • mongodb
  • sqlite
  • MySQL/PgSQL

infusion avatar Jun 25 '17 20:06 infusion

Hi, any those storages will require _selectAddress to be an asynchronous. How are you thinking about exposing this to the user? (parameter on server config?)

fredericosilva avatar Jun 28 '17 11:06 fredericosilva

The previous idea is emitting an event, which can be handled by a storage solution to write down the information. On startup the data must be fetched asynchronously. Exposing parameters via the server config would be another option. What do you think is the better way?

infusion avatar Jun 28 '17 11:06 infusion

On startup the data must be fetched asynchronously.

What about data which changes while server is running ?

Maybe dynamic requests to backend with some type of local (in memory or persistent) cache ?

gregorybrzeski avatar Jun 28 '17 11:06 gregorybrzeski

You mean administrative changes in the DB? Every change the server makes while running should affect the actual state and should simply be mirrored to the db layer. Handling administrative changes would require to query the db more frequently.

infusion avatar Jun 28 '17 11:06 infusion

Maybe, one could abstract those requirements in some kind of provider. That implements the selectAddress on an async fashion.

That will handle storage/backend scenarios.

Actual range/static/random may even be different provider implementations.

fredericosilva avatar Jun 28 '17 11:06 fredericosilva

Have a look on my fork, I've extracted the _getLease to have a single place where _selectAddress is called.

On _selectAddress I'm testing if a selectAddress exists on the config.

Any thoughts?

fredericosilva avatar Jun 28 '17 12:06 fredericosilva

this.config('leaseTime');

shouldn't work in the callback. You should reference this to the outer scope. But I like the async nature, you introduced to the address selection. I wonder if we should make it more general to store any config, or at least the lease storage (aka _state).

infusion avatar Jun 28 '17 13:06 infusion

It works because I'm using an arrow function, 'this' is not changed.

Do you mean abstracting the access to _state with async get/set? and using it to implement different persistence layers?

fredericosilva avatar Jun 28 '17 13:06 fredericosilva

Ahh, sorry. I only read the diff and missed the arrow function.

Yep exactly. What do you think about it?

infusion avatar Jun 28 '17 13:06 infusion

I like the ideia. If we have it like that, there is no need of exposing the selectAddress everything can be handled on the storage layer. Besides get/set what else do we need on that layer?

fredericosilva avatar Jun 28 '17 13:06 fredericosilva

I think that's it and yep, it doesn't need to expose individual features like selectAddress this way :)

The remaining question is, how the storage interface is fed into the server. If it's a config option like:

dhcp.createServer({
...
storage: new dhcp.createMongoStorage('127.0.0.1', ...),
... 
});

or something else.

infusion avatar Jun 28 '17 14:06 infusion

Should it be called storage ? Maybe it should be called a provider.

It would be providers decision if to store it persistently, in memory or in any other way. Each provider can also make own decision if to provide response based on dynamic or static data.

gregorybrzeski avatar Jun 28 '17 14:06 gregorybrzeski

You're right, Gregory. Provider might be a better name for it.

infusion avatar Jun 28 '17 14:06 infusion

Hi, I am using this module and would like to add this functionality. However, I am not clear on the requirements per the conversation in this thread. Need some feedback and better instructions :-) Sridhar

SridharRamasami avatar Nov 30 '17 03:11 SridharRamasami

Consider using something like keyv for the storage layer.

roccomuso avatar Jul 10 '18 10:07 roccomuso

Oh, thanks for introducing keyv!

infusion avatar Jul 10 '18 10:07 infusion