2017-03-19 115 views
0

我有大约100请求/秒的系统。有时它会在我重新启动我的go程序之前无法响应。我发现这是因为我打开了交易,并没有在某些地方关闭它。这就是为什么所有的连接被打开交易占用的,我不能打开另一个在这之后我加入这个代码Golang交易退出处理

defer func() { 
if r := recover(); r != nil { 
    tx.Rollback() 
    return 
} 

if err == nil { 
    err = tx.Commit() 
} else { 
    tx.Rollback() 
} 
}() 

这让我一个月工作方案没有中断。但是现在它又发生了。可能是因为这个问题。有没有更好的方式来结束交易?或者,也许一种方式来关闭交易,如果它是开放1分钟?

+0

什么是错误您收到?此外,只是一个旁注,但'回滚'也可以返回一个错误,所以你应该处理它。 – MahlerFive

+0

@MahlerFive,我不确定,它恐慌并导致执行停止。是的,我知道Rollback和Commit也会返回错误,但我不知道如何处理它们 –

+0

你可以发布你的整个功能吗? – MahlerFive

回答

0

如果你想1分钟后回滚事务,你可以使用标准的围棋超时模式(如下图)。

但是,可能有更好的方法来解决你的问题。你不会提供很多关于它的信息,但你可能使用带有上下文的标准HTTP请求。在这种情况下,它可能会帮助你知道a transaction started within a context is automatically rolled back when the context is cancelled。一种方法可以确保上下文被取消,如果出现问题,可以给它一个deadline


Channels - Timeouts摘录。原作者为Chris Lucas,KwarrtzRodolfo Carvalho。您可以在contributor page上找到归因细节。信息来源根据CC BY-SA 3.0获得许可,可在Documentation archive中找到。参考主题ID:1263和实例ID:6050

超时

通道经常被用来实现超时。

func main() { 
    // Create a buffered channel to prevent a goroutine leak. The buffer 
    // ensures that the goroutine below can eventually terminate, even if 
    // the timeout is met. Without the buffer, the send on the channel 
    // blocks forever, waiting for a read that will never happen, and the 
    // goroutine is leaked. 
    ch := make(chan struct{}, 1) 

    go func() { 
     time.Sleep(10 * time.Second) 
     ch <- struct{}{} 
    }() 

    select { 
    case <-ch: 
     // Work completed before timeout. 
    case <-time.After(1 * time.Second): 
     // Work was not completed after 1 second. 
    } 
} 
+0

您能否提供一个使用它的例子?我在互联网上找不到任何东西 –

+0

Golang博客(https://blog.golang.org/context)提供了如何使用Context的示例,并且Context包本身已有详细记录(https:// golang。组织/ PKG /上下文/)。 – Zoyd