Skip to content

Instantly share code, notes, and snippets.

View sachaarbonel's full-sized avatar
👨‍💻
Uncovering bugs

Sacha Arbonel sachaarbonel

👨‍💻
Uncovering bugs
View GitHub Profile
{
"books": [
{
"title": "The Great Gatsby",
"author": "F. Scott Fitzgerald",
"summary": "The story primarily concerns the young and mysterious millionaire Jay Gatsby and his quixotic passion and obsession with the beautiful former debutante Daisy Buchanan."
},
{
"title": "The Catcher in the Rye",
"author": "J. D. Salinger",
let kdtree = kd_tree::KdTree::build_by_ordered_float(vec![
[1.0, 2.0, 3.0],
[3.0, 1.0, 2.0],
[2.0, 3.0, 1.0],
]);
// search the nearest neighbor
let found = kdtree.nearest(&[3.1, 0.9, 2.1]).unwrap();
let model = SentenceEmbeddingsBuilder::remote(SentenceEmbeddingsModelType::AllMiniLmL12V2).create_model()?;
let sentences = [
"this is an example sentence",
"each sentence is converted"
];
let output = model.predict(&sentences);
Color randomPastelColor(){
final saturation = 0.4
final lightness = 0.9
final alpha = 1.0
final hue = Random().nextDouble();
final hsl = HSLColor(alpha, hue, saturation, lightness);
return hsl.toColor();
}

Scaling your API with rate limiters

The following are examples of the four types rate limiters discussed in the accompanying blog post. In the examples below I've used pseudocode-like Ruby, so if you're unfamiliar with Ruby you should be able to easily translate this approach to other languages. Complete examples in Ruby are also provided later in this gist.

In most cases you'll want all these examples to be classes, but I've used simple functions here to keep the code samples brief.

Request rate limiter

This uses a basic token bucket algorithm and relies on the fact that Redis scripts execute atomically. No other operations can run between fetching the count and writing the new count.

@sachaarbonel
sachaarbonel / main.dart
Created September 2, 2021 12:59
colorsKind
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class FrogColor extends InheritedWidget {
const FrogColor({
@sachaarbonel
sachaarbonel / pubspec.yaml
Last active February 26, 2021 19:38
pubspec
dependencies:
 stream_chat_flutter: ^1.3.0-beta
@sachaarbonel
sachaarbonel / message_page.dart
Last active February 25, 2021 20:19
MessagePage with SDK
import 'package:flutter/cupertino.dart';
import 'package:imessage/message_list_view.dart';
import 'package:stream_chat_flutter/stream_chat_flutter.dart'
show
LazyLoadScrollView,
MessageListController,
MessageListCore,
StreamChannel,
StreamChatCore;
@sachaarbonel
sachaarbonel / chat_loader.dart
Last active February 25, 2021 20:18
ChatLoader with sdk
import 'package:flutter/cupertino.dart';
import 'package:intl/date_symbol_data_local.dart';
import 'package:stream_chat_flutter/stream_chat_flutter.dart'
show
Channel,
ChannelListController,
ChannelListCore,
ChannelsBloc,
LazyLoadScrollView,
Level,
@sachaarbonel
sachaarbonel / main.dart
Last active February 25, 2021 20:16
main
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
final client = StreamChatClient('b67pax5b2wdq', logLevel: Level.INFO); //we want to see debug
//logs in our terminal
await client.connectUser(
User(
id: 'cool-shadow-7',
extraData: {
'image':
'https://getstream.io/random_png/?id=cool-shadow-7&name=Cool+shadow',