Golang Context Source Code Analysis

All source code analysis in this article is based on Go 1.16.4. 1. Context Introduction Context in the standard library is an interface with various implementations; Context was added to the standard library in Go 1.7 and is mainly used for setting deadlines, synchronizing signals, passing context request values, etc. across multiple Goroutines. Because of the need to pass signals across multiple Goroutines, multiple Contexts often need to be associated

Lazy Lists Based on Generator and Iterator

If you know Haskell, you are familiar with the following expressions 1 2 3 repeat 1 -- => [1, 1, 1, 1, 1,...] cycle "abc" -- => "abcabcabc..." [1, 3..] -- => [1, 3, 5, 7, ...] Several of the above expressions produce infinite lists. For those of you who are used to mainstream programming voices may be confused as to how the concept of infinity can be expressed inside a finite amount of memory.

React-getDerivedStateFromProp

Starting with React 16.3, React deprecated some APIs (componentWillMount, componentWillReceiveProps, and componentWillUpdate) and introduced some new ones instead, including getDerivedStateFromProps. getDerivedStateFromProps. Depending on the application scenario, getDerivedStateFromProps is used in different ways. Semi-controlled components Although React officially does not recommend semi-controlled components, and certainly not from the perspective of API design and maintenance. However, in practice, users often don’t care about the internal implementation of a business logic, but want to have full control over some internal state when needed, so semi-controlled components are a good choice.

Elegant Stopping Springboot Application

The closing of SpringBoot applications is currently summarized in four ways. Rest api: use the spring-boot-starter-actuator module in the ShutdownEndpoint the exit static method of SpringApplication: just call the static method directly JMX: use the MXBean provided inside SpringBoot. use third-party process management tools Rest api The Rest api is exposed using Endpoint and requires the introduction of spring-boot-starter-actuator the stater. The corresponding Endpoint for this shutdown application is ShutdownEndpoint,

Go Implements Prioritization in Select Statements

This article reviews some of the uses of the select statement in the Go language and extends a tip on how to implement priority in select, which I hope will be helpful to you. Introduction to the select statement The select statement in Go is used to monitor and select a set of case statements to execute the corresponding code. It looks similar to the switch statement, but all the expressions in the case in the select statement must be send or receive operations of the channel.

Go Reflect Performance

Go reflect package provides the ability to get the type and value of an object at runtime, which can help us to abstract and simplify the code, achieve dynamic data acquisition and method invocation, improve development efficiency and readability, and make up for Go’s ability to handle data uniformly in the absence of generics. With reflect, we can achieve the ability to get object types, object fields, object methods, get tag information of struct, dynamically create objects, whether objects implement specific interfaces, convert objects, get and set object values, call Select branches dynamically, etc.

Tracking Users via Js

This article describes how to write JavaScript scripts to send user data back to the server. I made a code repository containing all the examples below, which can be run to see the results. 1. Synchronization AJAX The common practice of sending data back to the server is to put the collected user data inside the unload event and send it back to the server with an AJAX request. However, asynchronous AJAX inside the unload event may not always work, because the page is already in unload and the browser may or may not send it.

Dateparse Usage Guide

Handling time is always a headache, no matter what time it is. The time format is too diverse, and it is even more difficult to deal with time zones, daylight saving time, leap seconds and other minor details. Therefore, we usually use standard libraries or time libraries provided by third parties to handle time in our programs. The dateparse we are going to introduce today focuses on a very small area of time processing - parsing strings in date time format.

Go Channel vs Java BlockingQueue

Recently, I was implementing two requirements and wanted to decouple them using a queue since there is no dependency between them; however, there is no readily available and concurrency-safe data structure in Go’s standard library; however, Go provides a more elegant solution, which is channel. Using Channel One of the major differences between Go and Java is the different concurrency model; Go uses the CSP (Communicating sequential processes) model; in

Building an AWS Local Development Environment with Localstack

I believe there are many people who have used AWS for their projects or are learning AWS. We know that with cloud services like AWS, it’s not very easy to connect to the cloud when developing locally, not to mention that the staging/production environment will have security considerations. So when we create a project, how do we build its local development environment to facilitate our local development and debugging? That’s right!

Getting Started With Mockoon

Sometimes I run into the need to build a quick test service, like this. Build an HTTP Service, which can be run locally, but also needs to be accessible on the public network, and request the service to get a custom set of JSON data. For no other reason than to be able to use it for testing quickly. At this time I want to complete in the shortest possible time, such as a minute to write it, how can I do this time?

Spring Cloud Gateway Guide

Recently, I was working on the refactoring of the old system, and I needed to introduce a gateway service into the new system after the refactoring was completed, as an adaptation and proxy for the interface between the new system and the old system. Previously, many gateway applications used the Spring-Cloud-Netfilx solution based on the Zuul1.x version, but given that Zuul1.x had stopped iterating, it used a more traditional blocking

Spring Cloud Gateway Custom Globalfilter

The scope of GlobalFilter is all the routing configuration, we can do additional extensions by customizing GlobalFilter, which can be used to implement some global functions. How to customize GlobalFilter The interface definition for org.springframework.cloud.gateway.filter.GlobalFilter is as follows. 1 2 3 4 public interface GlobalFilter { Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain); } We just need to implement the org.springframework.cloud.gateway.filter.GlobalFilter interface and register the implementation class in Spring’s container, the official example is as follows.

Pulsar Getting Started and Introduction

We are recently doing the technology selection for new business, which involves the selection of messaging middleware; combined with our actual situation we hope it can meet the following requirements. Cloud-friendly native support: because the main language is now Go, while being able to be simple enough in terms of operation and maintenance. Official SDK support for multiple languages: There is still some Python, Java related code to maintain. Preferably with some convenient and useful features, such as: delayed messages, dead letter queues, multi-tenancy, etc.

Various Database Connection Strings Commonly Used by Go

1. Relational Databases 1.1 MySQL https://github.com/go-sql-driver/mysql/ 1 2 3 4 5 6 7 8 9 10 11 12 [username[:password]@][protocol[(address)]]/dbname[?param1=value1&...¶mN=valueN] // user@unix(/path/to/socket)/dbname // root:pw@unix(/tmp/mysql.sock)/myDatabase?loc=Local // user:password@tcp(localhost:5555)/dbname?tls=skip-verify&autocommit=true // user:password@/dbname?sql_mode=TRADITIONAL // user:password@tcp([de:ad:be:ef::ca:fe]:80)/dbname?timeout=90s&collation=utf8mb4_unicode_ci // id:password@tcp(your-amazonaws-uri.com:3306)/dbname // user@cloudsql(project-id:instance-name)/dbname // user@cloudsql(project-id:regionname:instance-name)/dbname // user:password@tcp/dbname?charset=utf8mb4,utf8&sys_var=esc%40ped // user:password@/dbname // user:password@/ 1.2 Postgres (pure Go) https://github.com/lib/pq 1 2 3 4 5 6 7 8 9 postgres://[user[:password]@][netloc][:port][,...][/dbname][?param1=value1&...] postgres:// postgres://localhost postgres://localhost:5433 postgres://localhost/mydb postgres://user@localhost postgres://user:secret@localhost postgres://other@localhost/otherdb?connect_timeout=10&application_name=myapp postgres://host1:123,host2:456/somedb?target_session_attrs=any&application_name=myapp 1.3 SQLite (uses cgo) https://github.com/mattn/go-sqlite3 1 2 3 4 test.

Notes on the Use of Feign and Restful Design Specifications

I recently used Spring Cloud Feign as an HTTP client in a lot of projects, and encountered a lot of pitfalls, and also generated some ideas about RESTFUL design, which I’d like to document here. SpringMVC’s request parameter binding mechanism Users who know the history of Feign will know that Feign itself is a Netflix product, Spring Cloud Feign is based on the native Feign encapsulation, the introduction of a large number of SpringMVC annotation support, making it easier to use by the majority of Spring users, but also produced a not small confusing effect.

Spring Cloud Gateway - Downgrading with Hystrix using Custom Filters

Prerequisites In microservices architecture, if the downstream dependencies do not do request degradation processing, the downstream abnormal dependencies are not isolated, and it is likely that one or two services or as small as one or two interface abnormalities will lead to the unavailability of all upstream services and even affect the whole business line. Request degradation processing is still relatively mainstream is Netfilx produced Hystrix. Hystrix works on the

Introduction to Spring Cloud Stream

Spring Cloud Stream is used within the Spring Cloud architecture to build highly scalable, event-driven microservices. There is a lot of content in Spring Cloud Stream itself and it has many external dependencies. To get familiar with Spring Cloud Stream, you need to know the following. Spring Framework(Spring Messaging, Spring Environment) Spring Boot Actuator Spring Boot Externalized Configuration Spring Retry Spring Integration Spring Cloud Stream The purpose of this article is to introduce Spring Cloud Stream, and with so much knowledge, we will try to take you through Spring Cloud Steam in the simplest way possible (we will replace Spring Cloud Stream with SCS later).

A pitfall of comparing empty structs in Golang

A reader recently encountered a new problem with struct that he could not solve. Examples of Doubt The example 1 it gives is as follows. 1 2 3 4 5 6 7 type People struct {} func main() { a := &People{} b := &People{} fmt.Println(a == b) } What do you think the output will be? The output is: false. With a little more modification, Example 2 is as

Docker Serious Error Causes Enterpris Data to Be Deleted by Hackers

Be cautious of popular technologies that promise to make work easy - they often cut corners, and security is often one of them. A few days ago, the database of NewsBlur (a web-based RSS reader) was deleted by a hacker due to a bug that existed in Docker for years. In a blog post this week, NewsBlur founder Samuel Clay detailed his ordeal: during a migration of a MongoDB cluster to a Docker container, a hacker gained access to the NewsBlur database, deleted 250GB of raw data, and demanded a 0.