Skip to content

Commit

Permalink
tip(go/go-learn): chain
Browse files Browse the repository at this point in the history
  • Loading branch information
TIGERB committed Jul 18, 2019
1 parent c5c01bd commit 641896d
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 22 deletions.
3 changes: 2 additions & 1 deletion go/src/go-learn/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
go-learn
go-learn
__debug_bin
File renamed without changes.
File renamed without changes.
File renamed without changes.
124 changes: 103 additions & 21 deletions go/src/go-learn/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,115 @@ package main

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

// 导入net/http包
// Context Context
type Context struct {
}

func init() {
// Handler Handler
type Handler interface {
Do(c *Context) error
SetNext(h Handler) Handler
Run(c *Context)
}

// Next Next
type Next struct {
nextHandler Handler
}

func main() {
var wg sync.WaitGroup

// fmt.Println("waiting")
// // wg.Wait()
// fmt.Println("end")

for index := 0; index < 10; index++ {
wg.Add(1)
go func(index int) {
time.Sleep(10 * time.Second)
fmt.Println(index)
wg.Done()
}(index)
// SetNext SetNext
func (n *Next) SetNext(h Handler) Handler {
n.nextHandler = h
return h
}

// Run Run
func (n *Next) Run(c *Context) {
if n.nextHandler != nil {
(n.nextHandler).Do(c)
(n.nextHandler).Run(c)
}
}

// NullHandler NullHandler·
type NullHandler struct {
Next
}

// Do Do
func (h *NullHandler) Do(c *Context) error {
// do nothing...
return nil
}

// SignHandler SignHandler
type SignHandler struct {
Next
}

// Do Do
func (h *SignHandler) Do(c *Context) error {
fmt.Println("SignHandler")
return nil
}

// ArgumentsHandler ArgumentsHandler
type ArgumentsHandler struct {
Next
}

fmt.Println("waiting")
wg.Wait()
fmt.Println("end")
// Do Do
func (h *ArgumentsHandler) Do(c *Context) error {
fmt.Println("ArgumentsHandler")
return nil
}

// FrequentHandler FrequentHandler
type FrequentHandler struct {
Next
}

// Do Do
func (h *FrequentHandler) Do(c *Context) error {
fmt.Println("FrequentHandler")
return nil
}

// LogHandler LogHandler
type LogHandler struct {
Next
}

// Do Do
func (h *LogHandler) Do(c *Context) error {
fmt.Println("LogHandler")
return nil
}

func main() {
nullHandler := &NullHandler{}
argumentsHandler := &ArgumentsHandler{}
signHandler := &SignHandler{}
frequentHandler := &FrequentHandler{}

nullHandler.SetNext(argumentsHandler).SetNext(signHandler).SetNext(frequentHandler)
nullHandler.Run(&Context{})

fmt.Println("----------------------")

middlewares := make([]Handler, 0)
middlewares = append(middlewares, nullHandler)
middlewares = append(middlewares, argumentsHandler)
middlewares = append(middlewares, signHandler)
middlewares = append(middlewares, frequentHandler)

for k, handler := range middlewares {
if k == 0 {
continue
}
middlewares[k-1].SetNext(handler)
}
nullHandler.Run(&Context{})
}

0 comments on commit 641896d

Please sign in to comment.