Paul's Programming Notes PostsRSSGithub

ESP32 Plant Sensor

I set up a ESP32 houseplant soil water + temperature + humidity + light sensor that sends me a daily status update message.

Here’s the code for it:

esp32 plant sensor 1

esp32 plant sensor 2

esp32 plant sensor 3

AWS - redirecting domain to url using a 302 redirect (without running a server)

I wanted to make a domain name (heckingoodboys.com) redirect to a multisubreddit for dog pictures, but I didn’t want to run a web server for it.

Here’s what I did:

  1. Purchase the domain using Route53.
  2. Create two public s3 buckets (www.heckingoodboys.com and heckingoodboys.com)
  3. Enable “Static website hosting” on www.heckingoodboys.com and redirect to heckingoodboys.com.
  4. Enable “Static website hosting” on heckingoodboys.com, select “use this bucket to host this website”, and use routing rules similar to this:

    <RoutingRules>
      <RoutingRule>
        <Redirect>
          <Protocol>https</Protocol>
          <HostName>www.reddit.com</HostName>
          <HttpRedirectCode>302</HttpRedirectCode>
          <ReplaceKeyPrefixWith>user/heckingoodboys/m/heckingoodboys/</ReplaceKeyPrefixWith>
        </Redirect>
      </RoutingRule>
    </RoutingRules>
    
  5. Back to Route53 - Create an A record for both www.heckingoodboys.com and heckingoodboys.com using the alias to their respective buckets. (this will be the first option in autocomplete)

For more details: https://medium.com/@P_Lessing/single-page-apps-on-aws-part-1-hosting-a-website-on-s3-3c9871f126

Why not just use a CNAME from www.heckingoodboys.com to heckingoodboys.com? AWS says they don’t charge for aliases, but they do charge for CNAMEs. So, I used an alias to a bucket instead.

django-celery-email - Reducing memory usage on large email batches

I got a pull request merged into django-celery-email that cuts peak memory usage by about 74% when sending large batches of emails with attachments.

CeleryEmailBackend.send_messages used to serialize every message up front, then split the serialized list into chunks, then hand each chunk to a Celery task:

def send_messages(self, email_messages):
    result_tasks = []
    messages = [email_to_dict(msg) for msg in email_messages]
    for chunk in chunked(messages, settings.CELERY_EMAIL_CHUNK_SIZE):
        result_tasks.append(send_emails.delay(chunk, self.init_kwargs))

email_to_dict copies the whole message into a dict, attachments included. Doing that for the entire batch before chunking means every serialized email sits in memory at once. With attachments, that gets expensive fast.

The fix is to chunk the messages first, then serialize one chunk at a time:

def send_messages(self, email_messages):
    result_tasks = []
    for chunk in chunked(email_messages, settings.CELERY_EMAIL_CHUNK_SIZE):
        chunk_messages = [email_to_dict(msg) for msg in chunk]
        result_tasks.append(send_emails.delay(chunk_messages, self.init_kwargs))

Same tasks get queued, but only one chunk’s worth of serialized emails exists at a time, so the earlier chunks can be garbage collected once their task is queued.

Benchmarks on 80 emails with a 5 MB attachment each:

  • Before: 1028 MB peak memory usage
  • After: 269 MB peak memory usage

Python 3 - Comparing a version string to an int

I added Twilio backward compatibility to django-sendsms so its Twilio backend would work with both the old (v5) and new (v6+) Twilio clients. The version detection looked like this:

import twilio
if twilio.__version__ > 5:
    from twilio.rest import Client as TwilioRestClient
else:
    from twilio.rest import TwilioRestClient

That works on Python 2, but it’s broken on Python 3. twilio.__version__ is a string like "6.5.0", and comparing a string to an int raises a TypeError on Python 3:

>>> "6.5.0" > 5
TypeError: '>' not supported between instances of 'str' and 'int'

Python 2 lets you order any two objects (ints always sort before strings), so the same comparison silently returns True there. The bug stayed hidden until the code ran under Python 3.

The fix pulls the major version out of __version_info__ and compares ints to ints:

if int(twilio.__version_info__[0]) > 5:

That same PR added Python 3.5 and 3.6 to the Travis build, so this kind of thing gets caught next time.

Thrift Is More Difficult To Use Than HTTP

The microservices at my work implement both HTTP endpoints and Apache Thrift RPC endpoints, with Thrift carrying the internal communication between services. External access goes through an API gateway that needs HTTP anyway. I keep losing hours to a Thrift problem I could have solved in minutes over HTTP.

New services don’t get Thrift support at all anymore. They’re documented with Swagger and validated with JSON schema instead, and the tests check requests and responses against the spec.

What makes it harder to live with than HTTP:

  • An exception you didn’t declare in the IDL reaches the client as TApplicationException: Internal error and nothing else. The generated processor catches it, logs the traceback on the server, and sends back that one opaque message, so every debugging session starts with going to find the server log.
  • With an HTTP endpoint I can mock things out with a library like responses. Nothing equivalent exists for Thrift in Python yet, so testing is a lot more work.
  • Version mismatches are very difficult to debug, especially when someone changes the type of an existing field or adds a field to the end of a definition.
  • Updating one endpoint means updating three things: the Thrift definitions, the definitions on the client, and the definitions on the server.
  • The Python tooling for running a Thrift service is nowhere near as mature as it is for HTTP services.
  • A new developer has definitely used HTTP and probably hasn’t used Thrift. The business intelligence people don’t touch it at all, the barrier to entry is too high.
  • Javascript and iOS support isn’t great, though you probably shouldn’t be exposing Thrift services to the public internet anyway.

Thrift does buy real things. It’s strongly typed, the definitions give you one place to look at all of your models, it validates them for you, and the leaner transport puts less over the wire.

That last one matters less than it sounds. Gzipped JSON is already pretty compact and the default Thrift transports don’t compress at all, so you’re saving a few bytes in exchange for everything above.

If you’re only using Python, marshmallow covers the validation, or you can pair JSON schema with something like warlock to build objects from it. If you do stay on Thrift, thriftpy is a big quality of life improvement over the built-in client because it reads the definitions directly instead of making you generate code from them.

Whether the complexity is worth the performance depends on your scale. Uber runs Thrift across a thousand services and Matt Ranney still summed it up as “Thrift is OK, but generated code is bad” in What I Wish I Had Known Before Scaling Uber to 1000 Services, which is the same complaint that makes thriftpy worth using. For a small team it’s a lot of work and learning to end up somewhere HTTP already is.

JSON-API - Lessons Learned

A few things I’ve learned while building against JSON-API:

  • Objects referred to by relationships all go into one shared included array rather than being nested under the relationship that points at them. Without a JSON-API client library that’s a slight pain to parse, because you’re matching type and id pairs back to entries in a flat list. It beats duplicating the same object under every relationship that refers to it, but I would have preferred each object type under its own top level key.
  • It’s a lot more verbose than a response you’d shape by hand. Every record carries type, id, attributes, and relationships wrappers around what would otherwise be a flat object.
  • There’s no PUT. The spec uses PATCH for updates, so you send only the fields that changed instead of replacing the whole resource.

Sphinx Search - Lessons Learned

Here are a few things I’ve learned while working on a project that uses Sphinx search:

  • It’s important to know the difference between fields and attributes. Attributes are basically unindexed columns and you should try to avoid filtering only on these columns. Fields support full text search.
  • It supports its own custom binary protocol and the MySQL protocol (recently they also added a HTTP API). When you see “listen = localhost:9306:mysql41” in the config, that means it’s listening for MySQL protocol traffic on port 9306.
  • https://github.com/a1tus/sphinxapi-py3 appears to be the best Python client for the binary api at the moment. This doesn’t support INSERTing things into the index (you’ll need to use the MySQL protocol for that).
  • The version of sphinxapi-py3 on pypi is a fork with just a few minor fixes and appears to be safe.
  • It does not match partial words by default. Turning on partial matching can also increase the size of your index dramatically. You can also limit the fields that support partial matching with the infix_fields and prefix_fields setting.
  • Stemmers aren’t turned on by default. So, searching for “dog” will not match “dogs”.
  • Most special characters ($, @, &, etc) are ignored by default. You will need to add them to charset_table if you want them to be searchable.
  • Ruby’s thinking-sphinx looks much more battle tested than all of the Python binary api clients: https://github.com/pat/thinking-sphinx
  • You will need to use a real-time index if you want to INSERT/DELETE records immediately.
  • If you’re using a real-time index, you will probably need to increase the rt_mem_limit from its default of 128mb. If this limit is too low, you’ll see a high number of “disk chunks” when you run the “SHOW INDEX rtindex STATUS” query. More info: http://sphinxsearch.com/blog/2014/02/12/rt_performance_basics/
  • You have to use a special dialect if you want to use SQLAlchemy with sphinx: https://github.com/conversant/sqlalchemy-sphinx
  • This appears to be the best Dockerfile for sphinx: https://github.com/leodido/dockerfiles

I probably won’t be using Sphinx search for any new projects. Elasticsearch seems preferable these days.

MySQL - Duplicate Errors & Trailing Whitespace

I had a unique constraint on a VARCHAR column and I inserted two rows with the following values:

  1. “name” (without trailing whitespace)
  2. “name “ (with trailing whitespace)

To my surprise, I got a duplicate error on that 2nd insert. It turns out that MySQL ignores that trailing whitespace when it makes comparisons.

The MySQL docs say this: “All MySQL collations are of type PAD SPACE. This means that all CHAR, VARCHAR, and TEXT values are compared without regard to any trailing spaces. ‘Comparison’ in this context does not include the LIKE pattern-matching operator, for which trailing spaces are significant.” (https://dev.mysql.com/doc/refman/5.7/en/char.html)

The solution? You should probably be trimming trailing whitespace in your API endpoints and on your front-end.