etcd 0.0.3

  • Readme
  • Changelog
  • Example
  • Installing
  • 41

Bitbucket Pipelines branch Codecov Dart Version extra_pedantic on pub.dev License

A client library for connecting to and interacting with ETCD, a distributed, reliable key-value store for the most critical data of a distributed system

Usage #

Create a client:

var client = new EtcdClient('etcd.example.com', port: 2379, options: new ChannelOptions(credentials: ChannelCredentials.insecure()));

Fetch a value:

var currentValue = await client.kv.fetch('myKey');

Set/Update a value:

await client.kv.put('myKey', 'myValue');

Attach a lease to a value:

var lease = await client.lease.grant(Duration(seconds: 5));
await client.kv.put('myKey', 'myValue', lease: lease.id);

Check remaining time on a lease:

var remainingTTL = await client.lease.timeToLive(lease.id, listKeys: true);

Renew a lease:

await lease.keepAlive();

Revoke a lease (and the values its attached to):

await client.lease.revoke(lease.id);

Watch a value for changes:

var watcher = await client.watch.single(KEY, prevKv: true);
watcher.listen((EtcdWatchEvent event) {
  switch (event.type) {
    case EtcdWatchEventType.PUT:
      print('Key `${event.kv.key}` modified: replaced value `${event.prevKv.value}` with `${event.kv.value}`');
      break;

    case EtcdWatchEventType.DELETE:
      print('Key `${event.kv.key}` was deleted: deleted value was `${event.prevKv.value}`');
      break;
  }
});

Features and bugs #

For now, please file feature requests and bugs by emailing the author

0.0.1 #

  • Initial version, created by Stagehand

0.0.2 #

  • pub health adjustments

0.0.3 #

  • Modify generated grpc files to turn off some linter errors
  • Support for compact requests
  • Support for transaction requests
  • Support for key prefixes

example/example.dart

// ignore_for_file: unused_local_variable

import 'dart:async';
import 'package:etcd/etcd.dart';
import 'package:faker/faker.dart';
import 'package:grpc/grpc.dart';
import 'package:stack_trace/stack_trace.dart';



const String HOST = '192.168.145.139';
const int    PORT = 30326;
const String KEY  = 'foo';

Future<void> main(List<String> args) async {
  return Chain.capture(() async {
    Timer refreshTimer;

    final client = new EtcdClient(HOST, port: PORT, options: new ChannelOptions(credentials: ChannelCredentials.insecure()));

    try {
      print('creating lease...');
      var lease = await client.lease.grant(Duration(seconds: 5));
      print('lease ${lease.id} created with TTL: ${lease.ttl}');
      print('');

      print('creating a watcher for `$KEY`...');
      var watcher = await client.watch.prefix(KEY, prevKv: true); // ignore: avoid_single_cascade_in_expression_statements
      watcher.listen((EtcdWatchEvent event) {
        switch (event.type) {
          case EtcdWatchEventType.PUT:
            print('Key `${event.kv.key}` modified: replaced value `${event.prevKv.value}` with `${event.kv.value}`');
            break;

          case EtcdWatchEventType.DELETE:
            print('Key `${event.kv.key}` was deleted: deleted value was `${event.prevKv.value}`');
            break;

          default:
            print('some other event ${event.type.name}');
        }
      });
      print('watcher ${watcher.id} created for `$KEY`');
      print('');

      refreshTimer = Timer.periodic(Duration(seconds: 1), (_) async {
        await lease.keepAlive().then((response) => print('\t*renewed: ${response.ttl}'));
      });



      print('setting original value and applying lease ...');
      await client.kv.put(KEY, faker.lorem.word(), lease: lease.id);

      var currentValue = await client.kv.fetch(KEY);
      print('received current value: ${currentValue}');

      var remainingTTL = await lease.timeToLive(listKeys: true);
      print('expiring in ${remainingTTL.ttl}: keys: ${remainingTTL.keys}');

      await Future.delayed(Duration(seconds: 3), refreshTimer.cancel); // -- wait for a bit so we can see our lease renewals

      await lease.revoke();
      print('forcefully revoked lease ${lease.id} (and cleared all keys bound to it)');

      currentValue = await client.kv.fetch(KEY);
      print('received current value: ${currentValue}');
      print('');



      print('re-setting value ...');
      var oldValue = await client.kv.put(KEY, faker.lorem.word());

      print('updating value ...');
      await client.kv.put(KEY, faker.lorem.word());

      print('compacting history ...');
      await client.kv.compact(3); // -- compact up to (but not including) version 3



      print('deleting value (transaction)...');
      var transaction = client.kv.startTransaction()
        ..compare.value(KEY, EtcdComparisonOperator.IS_NOT_EQUAL, '') // -- if KEY is not empty
        ..success.delete(KEY)                                         // -- delete it
        ..failure.put('ERROR', 'key already deleted');                // -- otherwise, record the error

      var responses = await transaction.send();

    } finally {
      print('\nshutting down...');
      await client.shutdown(); // -- close our connection
    }
  });
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  etcd: ^0.0.3

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:etcd/etcd.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
75
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
41
Learn more about scoring.

We analyzed this package on Jul 3, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.13

Analysis suggestions

Package not compatible with runtime flutter-web on web

Because of the import of dart:io via the import chain package:etcd/etcd.dartpackage:grpc/grpc.dartpackage:grpc/src/shared/streams.dartpackage:http2/transport.dartpackage:http2/src/hpack/hpack.dartpackage:http2/src/hpack/huffman_table.dartpackage:http2/src/hpack/huffman.dartdart:io

Package not compatible with runtime web

Because of the import of dart:io via the import chain package:etcd/etcd.dartpackage:grpc/grpc.dartpackage:grpc/src/shared/streams.dartpackage:http2/transport.dartpackage:http2/src/hpack/hpack.dartpackage:http2/src/hpack/huffman_table.dartpackage:http2/src/hpack/huffman.dartdart:io

Health suggestions

Fix lib/src/kv/KVClient.dart. (-6.31 points)

Analysis of lib/src/kv/KVClient.dart reported 13 hints, including:

line 6 col 65: Unnecessary new keyword.

line 27 col 27: Unnecessary new keyword.

line 86 col 27: Unnecessary new keyword.

line 135 col 27: Unnecessary new keyword.

line 156 col 80: Unnecessary new keyword.

Fix lib/src/lease/LeaseClient.dart. (-5.84 points)

Analysis of lib/src/lease/LeaseClient.dart reported 12 hints, including:

line 8 col 68: Unnecessary new keyword.

line 17 col 19: Unnecessary new keyword.

line 23 col 27: Unnecessary new keyword.

line 31 col 39: Unnecessary new keyword.

line 33 col 72: Unnecessary new keyword.

Fix lib/src/watch/WatchClient.dart. (-5.36 points)

Analysis of lib/src/watch/WatchClient.dart reported 11 hints, including:

line 8 col 68: Unnecessary new keyword.

line 89 col 19: Unnecessary new keyword.

line 90 col 29: Unnecessary new keyword.

line 103 col 29: Unnecessary new keyword.

line 103 col 67: Unnecessary new keyword.

Fix additional 29 files with analysis or formatting issues. (-18.86 points)

Additional issues in the following files:

  • lib/src/kv/EtcdTransactionOpQueue.dart (8 hints)
  • lib/src/kv/EtcdTransactionResponse.dart (5 hints)
  • lib/src/EtcdClient.dart (4 hints)
  • lib/src/kv/EtcdTransaction.dart (4 hints)
  • lib/src/kv/EtcdTransactionCompareQueue.dart (4 hints)
  • lib/src/watch/EtcdWatchEvent.dart (4 hints)
  • lib/src/utils/_delegatingListMixin.dart (3 hints)
  • lib/src/kv/EtcdDeleteRangeResponse.dart (1 hint)
  • lib/src/kv/EtcdFetchRangeResponse.dart (1 hint)
  • lib/src/kv/EtcdPutResponse.dart (1 hint)
  • lib/src/lease/_EtcdLeaseResponse.dart (1 hint)
  • lib/src/watch/EtcdWatchResponse.dart (1 hint)
  • lib/src/watch/EtcdWatcher.dart (1 hint)
  • lib/src/_EtcdResponse.dart (Run dartfmt to format lib/src/_EtcdResponse.dart.)
  • lib/src/_EtcdStreamCache.dart (Run dartfmt to format lib/src/_EtcdStreamCache.dart.)
  • lib/src/_EtcdSubClient.dart (Run dartfmt to format lib/src/_EtcdSubClient.dart.)
  • lib/src/kv/EtcdKeyValue.dart (Run dartfmt to format lib/src/kv/EtcdKeyValue.dart.)
  • lib/src/kv/_EtcdComparisonOperator.dart (Run dartfmt to format lib/src/kv/_EtcdComparisonOperator.dart.)
  • lib/src/kv/_EtcdKVClientBase.dart (Run dartfmt to format lib/src/kv/_EtcdKVClientBase.dart.)
  • lib/src/kv/_EtcdKVResponse.dart (Run dartfmt to format lib/src/kv/_EtcdKVResponse.dart.)
  • lib/src/lease/EtcdLeaseGrantResponse.dart (Run dartfmt to format lib/src/lease/EtcdLeaseGrantResponse.dart.)
  • lib/src/lease/EtcdLeaseKeepAliveResponse.dart (Run dartfmt to format lib/src/lease/EtcdLeaseKeepAliveResponse.dart.)
  • lib/src/lease/EtcdLeaseListResponse.dart (Run dartfmt to format lib/src/lease/EtcdLeaseListResponse.dart.)
  • lib/src/lease/EtcdLeaseRevokeResponse.dart (Run dartfmt to format lib/src/lease/EtcdLeaseRevokeResponse.dart.)
  • lib/src/lease/EtcdLeaseTimeToLiveResponse.dart (Run dartfmt to format lib/src/lease/EtcdLeaseTimeToLiveResponse.dart.)
  • lib/src/utils/Strings.dart (Run dartfmt to format lib/src/utils/Strings.dart.)
  • lib/src/watch/_EtcdWatchFilter.dart (Run dartfmt to format lib/src/watch/_EtcdWatchFilter.dart.)
  • lib/src/watch/_EtcdWatchResponse.dart (Run dartfmt to format lib/src/watch/_EtcdWatchResponse.dart.)
  • lib/src/watch/_EtcdWatchStreamCache.dart (Run dartfmt to format lib/src/watch/_EtcdWatchStreamCache.dart.)

Maintenance suggestions

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
fixnum ^0.10.11 0.10.11
grpc ^2.1.3 2.2.0
logging ^0.11.4 0.11.4
path ^1.7.0 1.7.0
protobuf ^1.0.1 1.0.1
stack_trace ^1.9.3 1.9.5
Transitive dependencies
async 2.4.1
charcode 1.1.3
collection 1.14.13
convert 2.1.1
crypto 2.1.5
googleapis_auth 0.2.12
http 0.12.1
http2 1.0.0
http_parser 3.1.4
meta 1.1.8
source_span 1.7.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.2.0
Dev dependencies
extra_pedantic ^1.2.0
faker_extended ^0.0.1
mockito ^4.1.1
pedantic ^1.9.0 1.9.0 1.9.1
test ^1.9.3
test_coverage ^0.4.1