golang 在golang起点上的Epoll例子

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了golang 在golang起点上的Epoll例子相关的知识,希望对你有一定的参考价值。

package main

//
// Starting point is from http://gcmurphy.wordpress.com/2012/11/30/using-epoll-in-go/
// 
 
import (
	"fmt"
	"os"
	"syscall"
)
 
const (
	MaxEpollEvents = 32
	KB             = 1024
)
 
func echo(in, out int) {
 
	var buf [KB]byte
	for {
		nbytes, e := syscall.Read(in, buf[:])
		if nbytes > 0 {
			syscall.Write(out, buf[:nbytes])
		}
		if e != nil {
			break
		}
	}
}
 
func main() {
 
	var event syscall.EpollEvent
	var events [MaxEpollEvents]syscall.EpollEvent
 
	file, e := os.Open("/dev/kmsg")
	if e != nil {
		fmt.Println(e)
		os.Exit(1)
	}
	defer file.Close()
 
	fd := int(file.Fd())
	if e = syscall.SetNonblock(fd, true); e != nil {
		fmt.Println("setnonblock1: ", e)
		os.Exit(1)
	}
 
	epfd, e := syscall.EpollCreate1(0)
	if e != nil {
		fmt.Println("epoll_create1: ", e)
		os.Exit(1)
	}
	defer syscall.Close(epfd)
 
	event.Events = syscall.EPOLLIN
	event.Fd = int32(fd)
	if e = syscall.EpollCtl(epfd, syscall.EPOLL_CTL_ADD, fd, &event); e != nil {
		fmt.Println("epoll_ctl: ", e)
		os.Exit(1)
	}
 
	for {
 
		nevents, e := syscall.EpollWait(epfd, events[:], -1)
		if e != nil {
			fmt.Println("epoll_wait: ", e)
			break
		}
 
		for ev := 0; ev < nevents; ev++ {
			go echo(int(events[ev].Fd), syscall.Stdout)
		}
 
	}
 
}

golang golang上下文和渠道的例子

package channel

import (
	"fmt"
	"sync"
	"time"
)

func ExampleUnbufferedSend() {
	c1 := make(chan string)

	wg := &sync.WaitGroup{}
	wg.Add(1)
	go func() {
		defer wg.Done()
		select {
		case c1 <- "got message":
			return
		case <-time.After(20 * time.Millisecond):
			return
		}
	}()

	wg.Wait() // explicitly defer until goroutine is done
	select {
	case msg := <-c1:
		fmt.Println(msg)
	default:
		fmt.Println("no message")
	}

	// Output: no message
}

func ExampleBufferedSend() {
	c1 := make(chan string, 1)

	wg := &sync.WaitGroup{}
	wg.Add(1)
	go func() {
		defer wg.Done()
		select {
		case c1 <- "got message":
			return
		case <-time.After(20 * time.Millisecond):
			return
		}
	}()

	wg.Wait() // explicitly defer until goroutine is done
	select {
	case msg := <-c1:
		fmt.Println(msg)
	default:
		fmt.Println("no message")
	}

	// Output: got message
}

func ExampleNilChannelSend() {
	var c1 chan string

	wg := &sync.WaitGroup{}
	wg.Add(1)

	result := make(chan string, 1)
	go func() {
		defer wg.Done()
		select {
		case c1 <- "got message":
			return
		case <-time.After(20 * time.Millisecond):
			result <- "nil channel send blocks"
			return
		}
	}()

	wg.Wait()
	select {
	case msg := <-c1:
		fmt.Println(msg)
	case msg := <-result:
		fmt.Println(msg)
	}

	// Output: nil channel send blocks
}

func ExampleClosedChannelSend() {
	c1 := make(chan string, 1)
	close(c1)

	// using a channel so we know when its done
	result := make(chan string)
	go func() {
		defer func() {
			if r := recover(); r != nil { // handling the panic of the send on closed channel that will happen below
				result <- r.(error).Error()
				return
			}
			result <- "timed out"
		}()
		select {
		case c1 <- "got message": // closed channels panic when you send on them
			return
		case <-time.After(20 * time.Millisecond):
			return
		}
	}()

	fmt.Println(<-result)

	// Output: send on closed channel
}

func ExampleCloseNilChannel() {
	defer func() {
		if r := recover(); r != nil { // handling the panic of the close of nil channel
			fmt.Println(r)
			return
		}
		fmt.Println("timed out")
	}()

	var c1 chan string
	close(c1) // close of nil channel panics

	// Output: close of nil channel
}

func ExampleWaitArbitraryNumberOfChannels() {

}
package tasks

import (
	"context"
	"log"
	"sync"
	"time"
)

type ctxExamplesKey string

func ExampleSimpleContext() {
	wg := &sync.WaitGroup{}

	start := time.Now()
	estop := make(chan time.Time, 1)
	ectx := context.Background()

	ctx, cancel := context.WithCancel(ectx) // with cancel needed to be able to do
	stop1 := make(chan time.Time, 1)
	wg.Add(1)
	go func() {
		defer wg.Done()
		for {
			select {
			case <-ctx.Done():
				log.Println("done ctx")
				stop1 <- time.Now()
				return
			case <-time.After(1 * time.Second):
				log.Println("iteration for ctx")
			}
		}
	}()

	wg.Add(1)
	go func() {
		defer func() {
			estop <- time.Now()
			wg.Done()
		}()
		for {
			select {
			case <-ectx.Done():
				log.Println("done ectx, should not occur")
				return
			case <-time.After(1 * time.Second):
				log.Println("iteration for ectx because nil channel blocks forever")
				select {
				case <-ctx.Done():
					log.Println("done ectx through escaping with ctx.Done()")
					return
				default:
				}
			}
		}
	}()

	ctx2 := context.WithValue(ctx, ctxExamplesKey("ctx2val"), 0)
	stop2 := make(chan time.Time, 1)
	wg.Add(1)
	go func() {
		defer wg.Done()
		for {
			select {
			case <-ctx2.Done():
				log.Println("done ctx2")
				stop2 <- time.Now()
				return
			default:
				time.Sleep(2 * time.Second)
				log.Println("iteration for ctx2")
				ctx2 = context.WithValue(ctx2, ctxExamplesKey("ctx2val"), ctx2.Value(ctxExamplesKey("ctx2val")).(int)+1)
			}
		}
	}()

	wg.Add(1)
	go func() {
		defer wg.Done()
		<-time.After(9 * time.Second)
		cancel()
		log.Println("iteration for ctx")
	}()

	wg.Wait()

	log.Printf("ctx2val (ctx): %v", ctx.Value(ctxExamplesKey("ctx2val")))
	log.Printf("ctx2val (ctx2): %d", ctx2.Value(ctxExamplesKey("ctx2val")))
	log.Println("took", time.Now().Sub(start))
	log.Printf("ectx took: %v", (<-estop).Sub(start))
	log.Printf("ctx took: %v", (<-stop1).Sub(start))
	log.Printf("ctx2 took: %v", (<-stop2).Sub(start))
}

以上是关于golang 在golang起点上的Epoll例子的主要内容,如果未能解决你的问题,请参考以下文章

Golang Epoll初体验

Golang Epoll初体验

Golang是怎么利用epoll的

Golang select 用法与实现原理

Golang开发面经字节跳动(三轮技术面)

Golang开发面经字节跳动(三轮技术面)