2026-05-30

GoBlr #86 30 May 2026

by Aditya Hegde

notes tech

Go routine and channel internals

Buffered channel

ch <- 1
ch <- 2
ch <- 3 // this is pushed to sendq
	res <- ch // 1	
// sendq value of 3 pushed into queue
	res <- ch // 2
	res <- ch // 3
	res <- ch // we now call gopark as there is nothign in the buf. We have a sudog allocated and puhsed into the recvq
func recv(c *hchan, sg *sudog, ep unsafe.Pointer, unlockf func(), skip int) {
  // snip
  if c.dataqsiz == 0 {
    if raceenabled {
      racesync(c, sg)
    }
    if ep != nil {
      // copy data from sender
      recvDirect(c.elemtype, sg, ep)
    }
    // snip
  }
  // snip
}

func recvDirect(t *_type, sg *sudog, dst unsafe.Pointer) {
  // dst is on our stack or the heap, src is on another stack.
  // The channel is locked, so src will not move during this
  // operation.
  src := sg.elem.get()
  typeBitsBulkBarrier(t, uintptr(dst), uintptr(src), t.Size_)
  memmove(dst, src, t.Size_)
}

Unbuffered channel flow

Select statement

select {
  case <-ch1:
  case <-ch2:
  default: // default non blocking exit
}

select { // blocking multi-select
  case <-ch1:
  case <-ch2:
}

Patterns

dbw by hashicorp (https://github.com/hashicorp/go-dbw) as a database wrapper in boundary