Skip to content

Questions with regards to lifetimes #105

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
CumpsD opened this issue Jun 18, 2020 · 4 comments
Closed

Questions with regards to lifetimes #105

CumpsD opened this issue Jun 18, 2020 · 4 comments
Labels
question Further information is requested
Milestone

Comments

@CumpsD
Copy link

CumpsD commented Jun 18, 2020

A few questions with regards to best practices for object lifetimes.

Is the client created by InfluxDBClientFactory safe to be registered as a Singleton for the lifetime of the application?

InfluxDBClientFactory.Create(influxEndpoint.ToString(), token.ToCharArray());

Is the WriteApi created by GetWriteApi intended to be created and disposed on every write?

influxDbClient.GetWriteApi(writeOptions);

What I'm playing with now:

            // TODO: What is the usage pattern for influxdbclient?
            containerBuilder
                .Register(context =>
                {
                    return InfluxDBClientFactory.Create(
                        influxEndpoint.ToString(),
                        token.ToCharArray());
                })
                .SingleInstance()
                .As<InfluxDBClient>();

            containerBuilder
                .Register<Func<WriteApi>>(context =>
                {
                    var influxDbClient = context.Resolve<InfluxDBClient>();

                    var writeOptions = WriteOptions
                        .CreateNew()
                        .BatchSize(5000)
                        .FlushInterval(1000)
                        .JitterInterval(1000)
                        .RetryInterval(5000)
                        .Build();

                    return () => influxDbClient.GetWriteApi(writeOptions);
                })
                .As<Func<WriteApi>>();
        private static void WriteTemperature(
            Func<WriteApi> writeApi,
            string location,
            double value)
        {
            var temperature = new Temperature
            {
                Location = location,
                Value = value,
                Time = DateTime.UtcNow
            };

            using (var writeClient = writeApi())
            {
                // TODO: Get bucket and org from config
                writeClient.WriteMeasurement(
                    "functional-living",
                    "cumps",
                    WritePrecision.Ns,
                    temperature);

                writeClient.Flush();
            }
        }
@bednar
Copy link
Contributor

bednar commented Jun 19, 2020

Hi @CumpsD,

Is the client created by InfluxDBClientFactory safe to be registered as a Singleton for the lifetime of the application?

Yes

Is the WriteApi created by GetWriteApi intended to be created and disposed on every write?

It depends on your use-case. The WriteApi uses batching with auto-flush strategy. If you will write a lot of data then keep alive WriteApi for the lifetime of the application.

Otherwise you could dispose a client on every write.

Currently we have in review a new WriteApiAsync (#102) without batching that could be a useful for second strategy.

Regards

@bednar bednar added the question Further information is requested label Jun 19, 2020
@CumpsD
Copy link
Author

CumpsD commented Jun 19, 2020

Thanks!

What happens to WriteApi on exception?

I found

exception =>
{
_disposed = true;
Trace.WriteLine($"The unhandled exception occurs: {exception}");
},

Does it rebuilt itself and retry on exception? Or is there a way to?

That seems to be the only concern I have with keeping WriteApi around for the lifetime of the application.

@bednar
Copy link
Contributor

bednar commented Jun 22, 2020

It depends on type of error:

@CumpsD
Copy link
Author

CumpsD commented Jun 22, 2020

Thanks @bednar! I see now, the exception bit I linked is really for unhandled exceptions in the WriteApi, not for the stuff you deal with like the non retryable errors.

@CumpsD CumpsD closed this as completed Jun 22, 2020
@bednar bednar added this to the 1.10.0 milestone Jun 22, 2020
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants