Stories

Detail Return Return

Golang 如何監聽某個函數(方法)開始執行和執行結束 - Stories Detail

如果想監聽函數(方法)開始執行和執行結束,你需要設置兩個通道:

  • chanStarted: 用於發送開始執行信號。
  • chanFinished: 用於發送執行結束信號。

同時,為了保證監聽方能實時得知“開始執行”或“執行結束”信號,需要在執行任務前開啓監聽。

以下為模擬監聽函數(方法)開始執行和執行結束的示例:

package main

import (
    "context"
    "fmt"
    "time"
)

type Transit struct {
    worker       func(ctx context.Context, a ...any) (any, error)
    chanStarted  chan struct{}
    chanFinished chan struct{}
}

func (t *Transit) Run(ctx context.Context, a ...any) (any, error) {
    defer func() {
        t.chanFinished <- struct{}{}
    }()
    t.chanStarted <- struct{}{}
    return t.worker(ctx, a...)
}

func worker(ctx context.Context, a ...any) (any, error) {
    if timer, ok := a[0].(int); ok && timer > 0 {
        time.Sleep(time.Duration(timer) * time.Second)
    }
    return a[0], nil
}

func NewTransit() *Transit {
    return &Transit{
        worker:       worker,
        chanStarted:  make(chan struct{}),
        chanFinished: make(chan struct{}),
    }
}

func main() {
    transit := NewTransit()
    chanStarted := transit.chanStarted
    chanFinished := transit.chanFinished
    finished := make(chan struct{})
    go func() {
        for {
            select {
            case <-chanStarted:
                fmt.Println(time.Now(), "started.")
            case <-chanFinished:
                fmt.Println(time.Now(), "finished,")
                finished <- struct{}{}
                return
            default:
            }
        }
    }()
    run, _ := transit.Run(context.Background(), 0)
    <-finished
    fmt.Println(time.Now(), "result:", run)
}

上述方案中,必須設置監聽方,否則Run()方法中會觸發死鎖。

如果想無阻塞的向通道發送,可以採取變通辦法,即提前登記事件接收方,產生事件時逐個發送。例如:

package main

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

type Transit struct {
    muListener sync.RWMutex
    listener   []TransitEventInterface

    worker func(ctx context.Context, a ...any) (any, error)
}

func (t *Transit) NotifyStarted() {
    for _, l := range t.listener {
        if l == nil {
            continue
        }
        l.NotifyStarted()
    }
}

func (t *Transit) NotifyFinished() {
    for _, l := range t.listener {
        if l == nil {
            continue
        }
        l.NotifyFinished()
    }
}

type TransitEventInterface interface {
    NotifyStarted()
    NotifyFinished()
}

type TransitEventListener struct {
    TransitEventInterface
}

var notifiedStarted = make(chan struct{})
var notifiedFinished = make(chan struct{})

func (l *TransitEventListener) NotifyStarted() {
    notifiedStarted <- struct{}{}
}

func (l *TransitEventListener) NotifyFinished() {
    notifiedFinished <- struct{}{}
}

func (t *Transit) Run(ctx context.Context, a ...any) (any, error) {
    t.muListener.RLock()
    defer t.muListener.RUnlock()
    t.NotifyStarted()
    defer t.NotifyFinished()
    return t.worker(ctx, a...)
}

func worker(ctx context.Context, a ...any) (any, error) {
    if timer, ok := a[0].(int); ok && timer > 0 {
        time.Sleep(time.Duration(timer) * time.Second)
    }
    return a[0], nil
}

func NewTransit() *Transit {
    return &Transit{
        worker:   worker,
        listener: []TransitEventInterface{&TransitEventListener{}},
    }
}

func main() {
    transit := NewTransit()
    finished := make(chan struct{})
    startedTime := time.Now()
    finishedTime := time.Now()
    go func() {
        for {
            select {
            case <-notifiedStarted:
                startedTime = time.Now()
            case <-notifiedFinished:
                finishedTime = time.Now()
                finished <- struct{}{}
                return
            default:
            }
        }
    }()
    run, _ := transit.Run(context.Background(), 0)
    <-finished
    fmt.Println(time.Now(), "result:", run)
    fmt.Println(finishedTime.Sub(startedTime))
}

由於 fmt.Println() 方法在向屏幕輸出內容時採取非阻塞形式,因此,直接在接收信號處直接輸出會發現輸出“started.”和“finished.”的順序不固定。

為了保證儘可能精確測量開始和結束的時間差,建議採用上述記錄時間點並在結束後計算時間差的方式。

user avatar manongsir Avatar yejianfeixue Avatar daqidexihongshi Avatar 5e4jkgqh Avatar sayornottt Avatar mrbone11 Avatar phpnan Avatar yaochujiadebiandou Avatar
Favorites 8 users favorite the story!
Favorites

Add a new Comments

Some HTML is okay.