260×260

科学搜查官yuchanns

咖啡、代码与键盘
  • Shenzhen, China
  • Backend Developer
readme.md

嗨~我是@yuchanns,网友们通常叫我羽毛,我的常用中文网名是科学搜查官,取自《逆转裁判》中的女角色宝月茜的职业。

我的头像是一只喝着咖啡敲着代码的地鼠,姿势模仿了《崩坏3》的布洛尼娅的一个表情包,由我不愿透露姓名的朋友所绘。

readme

你现在所看到的博客是我使用gatsby编写的主题博客gatsby-theme-yuchanns,融合了github和reddit两者中我所喜爱的元素,目前还在完善中;等vuerpess-next功能完善后将会提供基于vuepress的版本。

我还编写过另一个主题vuepress-theme-hermit,这是一个hugo-hermit的vuepress实现。

到现在为止我谈到的都是前端相关的作品,但前端只是我的兴趣——实际上我是一名后端开发工程师,使用过php、python,目前的主力开发语言是go,并且我对自己的gopher身份而感到自豪。

Posted 8 months ago

[WIP]Replacing net.DefaultResolver with a caching DNS over TLS/HTTPS resolver

NS caching has been discussed multiple times in the past. The consensus seems to be that Go won't go there: github.com/golang/go/issues/24796

I've seen a few DNS caching solutions for Go (one, two), however, I haven't seen any implementations that allow replacing the net.DefaultResolver?

Package github.com/artyom/dot got me thinking if I could do the same for DNS caching, and also DNS over HTTPS.

So github.com/ncruces/go-dns is my attempt. Replacing net.DefaultResolver with a caching DNS…

Posted 8 months ago

How to do something every 5 minutes in Go?

I feel like it's a dummy question, but I want to ask it anyway since I do not have much experience with concurrent programming.

In the app I am currently working on, someone wrote a code that should do something (in this case, printing "TICK!") every, let's say 5 minutes:

go func() { for { fmt.Println("TICK!") time.Sleep(5 * time.Minute) } }()

However, from what I have read, the recommended approach is:

func CallTickerInBackground(done <-chan bool) { ticker := time.NewTicker(5 * time.Minute) go func() { for { select { case <-done: logrus.Info("Stopping ticker") ticker.Stop() return case <-ticker.C: fmt.Println("TICK!") } } }() }

The advantages of the latter are that we have more stable time…

Posted 10 months ago

用redis做队列

最近做一个小型项目,因为rabbitMQ等专业软件比较重,故团队决定采用redis实现一个轻量的队列。

目标

在这篇文章中,你可以获得:

  • redigo包的基本用法
  • 初步认知context包的作用
  • 了解一个功能模块的实现思路以及如何逐步完善的步骤
  • Go特性(selectchannelgoroutine)的利用

最终代码量大概265行左右。

redis队列的原生用法

redis并不是被设计用来做队列的,事实上它并不是那么适合作为队列载体——官方也不推荐用来做队列,甚至因为使用redis做队列的人太多,而促使antirez(redis的作者)开发了另一个名为Disque1的专业队列库,据说将会加入到redis6中。

尽管如此,redis依旧提供了号称Reliable queue的队列指令2

我们知道,当生产者在另一端生成消息之后,这一端的消费者就要取出这一消息进行消费动作;而在消费的过程中如果出现任何异常——例如“程序崩溃”等问题,造成进程的退出,消息就会丢失。为此,redis官方提供了RPOPLPUSH这一队列指令,在从队列中取出消息的同时又塞进另一个队列中。这样当程序发生异常退出时,我们也可以通过第二个队列来找回丢失的消息。

  • 2020
  • 2016