![]() |
VOOZH | about |
We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.
Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.
Follow TNS on your favorite social media networks.
Become a TNS follower on LinkedIn.
Check out the latest featured and trending stories while you wait for your first TNS newsletter.
GoFr is an opinionated Go framework designed for accelerated microservice development. It comes equipped with built-in observability tools, including health-check and heartbeat URLs, fulfilling Kubernetes liveness and readiness probe requirements, alongside tracing, metrics, and structured logs with varying levels to monitor your application’s health.
Moreover, GoFr extends its support to a plethora of data sources, such as MySQL, PostgreSQL, Kafka, Google Cloud services, MQTT and more. This comprehensive coverage ensures that developers can exclusively concentrate on their business’s core logic, while GoFr ensures that applications are production-ready right from the proof of concept stage.
Within the GoFr framework, the GoFr Context emerges as a bespoke implementation of the context package, tailored for usage within the framework’s ecosystem. While retaining the standard features of Go’s context package, the GoFr Context extends its capabilities, aligning closely with GoFr’s philosophy of microservice development.
At the heart of every request-response cycle lies the `gofr.Context` object, a central figure adept at orchestrating various elements. Serving as the guardian of request-specific attributes, it ensures that each cycle is endowed with a context finely attuned to its unique requirements.
GoFr dedicates careful attention to crafting this context, imbuing it with dependencies such as loggers, data stores, and more — each intricately configured to align seamlessly with your project’s specifications.
In this article, we’ll go through the essence of GoFr, unraveling its purpose, elucidating the problems it elegantly solves, and illuminating how it can seamlessly integrate into your project.
Additionally, we’ll delve into two specific examples: the Circuit Breaker Implementation for HTTP Communication in GoFr and Implementing the Publisher-Subscriber Pattern in GoFr. Through these examples, we’ll explore how GoFr addresses common challenges in distributed systems.
Gin offers a lightweight and flexible framework for building web applications. However, compared to GoFr, it requires additional configuration and manual setup for certain features.
One key difference is that Gin doesn’t provide built-in support for database migrations like GoFr does. This means we’ll need to manage database schema changes manually, potentially increasing development time and complexity.
Another distinction is that Gin doesn’t automatically generate CRUD (create, read, update and delete) handlers for entities, so we’ll have to define endpoints and implement CRUD operations. This might lead to variations in endpoint design and require more development effort.
Additionally, Gin doesn’t come with default tracing and metrics support. This means we need to integrate third-party libraries or services for monitoring application performance.
GoFr boasts an arsenal of features designed to empower developers in their quest for excellence:
At the heart of GoFr lie foundational principles that define its ethos:
When transient faults occur during HTTP communication in a distributed system, the retry pattern can sometimes help.
When an application encounters such an error (typically due to temporary network glitches or database timeouts), instead of immediately giving up and returning an error to the user, the retry pattern involves automatically retrying the operation after a short delay. This delay can be fixed or exponential, meaning that subsequent retries occur after increasing intervals.
But sometimes relentless retries exacerbate the problem, leading to potential service degradation and even unintentional denial-of-service attacks.
To address this challenge, GoFr integrates the circuit breaker pattern, a robust defense mechanism designed to prevent futile operations and mitigate the impact of non-transient faults.
The circuit breaker pattern complements the retry pattern by focusing on recognizing and handling scenarios where repeated attempts at an operation are unlikely to succeed.
Rather than persistently retrying, the circuit breaker pattern aims to safeguard the system by temporarily halting further attempts upon detecting a certain threshold of failures. This proactive approach prevents cascading failures and conserves system resources, enhancing overall resilience.
GoFr seamlessly incorporates circuit breaker functionality into its HTTP communication framework, empowering developers to fortify their applications against transient faults and service unavailability. By simply passing circuit breaker configurations as options to the `AddHTTPService()` method, developers can enable this critical defense mechanism with ease.
Here’s an example of how the circuit breaker pattern is used:
The circuit breaker state transitions to ‘open’ when the number of consecutive failed requests exceeds the specified threshold. In the open state, GoFr periodically checks the aliveness endpoint `default being: /.well-known/alive` at the configured interval.
Once the service is deemed operational again, the circuit breaker transitions back to the `closed` state, allowing normal operation to resume.
In GoFr, native support for the Publisher–Subscriber pattern is provided through two message brokers: Apache Kafka and Google PubSub.
We initialize the PubSub component within an Inversion of Control (IoC) container, so that dependency management will be handled by the framework itself. This encapsulation promotes modularity, testability and reusability, empowering developers to focus on application logic rather than infrastructure concerns.
Users can access methods provided by the container to obtain Publisher and Subscriber interfaces, enabling seamless interaction with the message broker.
To define the message broker our application will use, we must configure the PubSub backend. A key configuration parameter,`PUBSUB_BACKEND`, determines the choice of the message broker, allowing users to tailor their setup according to their specific requirements.
Configurations
For Kafka Docker setup:
Adding a subscriber in GoFr mirrors adding an HTTP handler, promoting scalability by decoupling from the Sender/Publisher. Developers define a subscriber handler for message processing and use `app.Subscribe` to inject the handler into the application. This follows the inversion of control pattern, ensuring framework control and easing development and debugging.
Example of a subscriber handler:
Message publishing is recommended at the point of message generation. Developers can access the publishing interface from the gofr Context (`ctx`) to publish messages.
Example of a publisher:
Embarking on your GoFr journey is a streamlined affair, thanks to the comprehensive step-by-step guides curated to expedite system setup and library installation. These guides serve as the quintessential roadmap, ensuring a smooth initiation into the GoFr ecosystem.
The repository of examples serves as a beacon of enlightenment, guiding developers through the intricacies of common tasks within the GoFr framework. From rudimentary maneuvers to sophisticated operations, each example elucidates best practices and fosters a deeper understanding of GoFr’s capabilities.