lib/avromatic/schema_registry_patch.rb in avromatic-0.15.0 vs lib/avromatic/schema_registry_patch.rb in avromatic-0.15.1
- old
+ new
@@ -1,27 +1,33 @@
require 'avro_turf'
require 'avro_turf/schema_registry'
-AvroTurf::SchemaRegistry.class_eval do
- # Override register to first check if a schema is registered by fingerprint
- def register(subject, schema)
- schema_object = if schema.is_a?(String)
- Avro::Schema.parse(schema)
- else
- schema
- end
+module Avromatic
+ module CacheableSchemaRegistration
+ # Override register to first check if a schema is registered by fingerprint
+ def register(subject, schema)
+ return super unless Avromatic.use_cacheable_schema_registration
- registered = false
- data = begin
- get("/subjects/#{subject}/fingerprints/#{schema_object.sha256_fingerprint.to_s(16)}")
- rescue
- registered = true
- post("/subjects/#{subject}/versions", body: { schema: schema.to_s }.to_json)
- end
+ schema_object = if schema.is_a?(String)
+ Avro::Schema.parse(schema)
+ else
+ schema
+ end
- id = data.fetch('id')
+ registered = false
+ data = begin
+ get("/subjects/#{subject}/fingerprints/#{schema_object.sha256_fingerprint.to_s(16)}")
+ rescue
+ registered = true
+ post("/subjects/#{subject}/versions", body: { schema: schema.to_s }.to_json)
+ end
- @logger.info("#{registered ? 'Registered' : 'Found'} schema for subject `#{subject}`; id = #{id}")
+ id = data.fetch('id')
- id
+ @logger.info("#{registered ? 'Registered' : 'Found'} schema for subject `#{subject}`; id = #{id}")
+
+ id
+ end
end
end
+
+AvroTurf::SchemaRegistry.prepend(Avromatic::CacheableSchemaRegistration)