- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我在 REST API 服务器上工作,该服务器的功能之一是能够在创建新资源或修改现有资源时通过 websocket 通知任意数量的客户端。
我有一个自定义操作路由器,用于将 URL 绑定(bind)到函数和 gorillas 的 websocket 库实现。对于 IPC,我决定依赖 channel ,因为它似乎是协程之间通信的惯用方式。它的行为也像一个管道,这是我熟悉的概念。
Create
函数的原型(prototype)如下所示:
func Create (res http.ResponseWriter, req *http.Request, userdata interface {}) (int, string, interface {})
作为 userdata
结构 PipeSet
的实例被传递。它是一个在所有协程之间共享的映射,其中键是 Pipe
的地址(指向)并且值相同。这里的基本原理是在删除时加快查找过程。
type Pipe chan string
type PipeSet struct {
sync.Mutex
Pipes map [*Pipe] *Pipe
}
func NewPipe () Pipe {
return make (Pipe)
}
func NewPipeSet () PipeSet {
var newSet PipeSet
newSet.Pipes = make (map[*Pipe] *Pipe)
return newSet
}
func (o *PipeSet) AddPipe (pipe *Pipe) {
o.Lock ()
o.Pipes[pipe] = pipe
o.Unlock ()
}
func (o *PipeSet) ForeachPipe (f func (pipe Pipe)) {
o.Lock ()
for k := range (o.Pipes) {
f (*o.Pipes[k])
}
o.Unlock ()
}
func (o *PipeSet) DeletePipe (pipe *Pipe) {
o.Lock ()
delete (o.Pipes, pipe)
o.Unlock ()
}
当客户端通过 websocket 连接时,将创建一个新 channel (Pipe
)并将其添加到共享 PipeSet
。然后,如果创建了一个新资源,协程将通过整个 PipeSet
向每个 Pipe
发送消息。然后将消息转发到另一端连接的客户端。
我无法检测客户端的 websocket 连接是否仍然存在。我需要知道这一点以确定是否应该从 PipeSet
中删除一个 Pipe
。在这种情况下,我依赖于 CloseNotifier
。它永远不会触发。
代码如下所示(摘录):
var upgrader = websocket.Upgrader {
CheckOrigin: func (r *http.Request) bool { return true },
}
conn, err := upgrader.Upgrade (res, req, nil)
if err != nil {
marker.MarkError (err)
return http.StatusBadRequest, "", nil
}
defer conn.Close ()
exitStatus = http.StatusOK
pipe := genstore.NewPipe ()
quit := res.(http.CloseNotifier).CloseNotify ()
genStore.WSChannels.AddPipe (&pipe)
for {
log.Printf ("waiting for a message")
select {
case wsMsg = <-pipe:
log.Printf ("got a message: %s (num pipes %d)", wsMsg, len (genStore.WSChannels.Pipes))
if err = conn.WriteMessage (websocket.TextMessage, []byte (wsMsg)); err != nil {
marker.MarkError (err)
goto egress
}
case <-quit:
log.Printf ("quit...")
goto egress
}
}
egress:
genStore.WSChannels.DeletePipe (&pipe)
最佳答案
当您使用 Gorilla 将 HTTP 连接升级为 WebSocket 连接时,它会劫持该连接并且 net/http 服务器停止为其提供服务。这意味着,您不能依赖那一刻起的 net/http 事件。
检查这个:https://github.com/gorilla/websocket/issues/123
因此,您在这里可以做的是为每个新的 WebSocket 连接启动新的 goroutine,它将从此连接读取数据并在失败时将消息写入 quit
channel 。
关于http - 净/http : concurrency and message passing between coroutines,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44340192/
我尝试将 python 2.x 代码移植到 python 3。我正在努力解决的问题是 from mimetools import Message ... headers = Message(Strin
我有一个输入组件,它有三种类型的验证(required、validatorMessage、converterMessage),这个输入有自己的消息图标,整个表单有一个消息组件来显示所有组件的所有消息,
我有一个使用消息代理 (activemq) 的带有 spring 和 websockets 的 webapp。 这是我的配置类: @Configuration @EnableWebSocketMess
据我了解mbox Python 3.6 标准库中的类生成 email.message.Message 类型的旧式消息对象. 较新的类(class) email.message.EmailMessage
我使用的是mysql。 我有一个包含 userid、message_id、opened(true 或 false)、时间戳的消息表。 我希望所有未打开最近收到的 5 条消息的用户 这就是我现在拥有的:
我是 Android 的新手,发现要不断更新主视图,我必须创建一个线程来处理各种进程,然后将更新传回主视图。我决定使用 Handler 类来执行此操作。此示例中的 View 有一个用于激活代码的按钮和
我遇到了重定向符号的不同位置( > , &2 message message echo message >&2 message >&2 echo message message 对于所有表单,我得到了
我想使用 firebase 云函数发送通知,所以我尝试使用 firebase.messaging().getToken() 获取 token ,但我不断收到错误消息: TypeError: fireb
我实现了一个短信应用。现在我在使用 Oppo 设备时遇到了问题,因为无论何时收到消息,系统都会将默认应用程序更改为内置应用程序并显示此消息: For your messages security, S
我正在实现本指南:https://spring.io/guides/gs/centralized-configuration/关于Spring Cloud配置。 服务器: @EnableConfigS
我想在“匹配”之后,向所有比赛发送介绍信息。你知道一种轻松发送的方法吗?(我使用 Bluestacks) 提前致谢。问候。 最佳答案 只需传递 session.send()在session.dialo
在我们的应用程序中,我们使用 kafka 并有一个像这样的 Spring Cloud 输入流 @Component public interface SomeChannel { @Input(
这周我在通过 Node.js 库(代码相同,库版本相同等)向我的 iOS 设备发送消息时遇到了很多内部错误 很难调试,因为有时它会起作用。当我使用 for 循环发送 10 条消息时,我的设备将收到 3
我目前正在记录错误并希望获得尽可能多的描述性细节。我知道我可以捕获许多不同类型的异常,但 Exception.Message 之间有什么区别?和 Exception.InnerException.Me
我创建了一个映射到 MyBean.beansField 的表单。我使用 javax.validation.NotNull 注释来确保必须输入它。到目前为止一切正常,但错误消息如下所示: beansFi
我正在研究 Azure 服务总线。我的服务总线队列正在处理一条消息 3 次。我的消息锁定时间是5分钟。每条消息最多处理 2 分钟,但我不知道为什么队列会选择相同的消息并发送到处理,而重复的消息仅在 5
我正在使用最新的快速修复版本,即 1.6.0。我已经有针对 1.5.3 编写的代码,我想做的是将其升级到 1.6.0 我遇到的问题是,当我使用破解(msg,sessionID)方法时,它会抛出quic
当我调用 grails message()函数来查找和评估消息 key 对,它无法评估参数。 在我的 Controller 中,我调用消息函数: rejectWithError(message(cod
我使用一个小型 Spring 应用程序,其中数据库中的值很少,我想使用可变调用来检索它们。 API 在这里, @RestController @RequestMapping("/api/v1/prod
我在想在用以更好的方式,像这样: Please inform us about: 这个想法是以一种不同的方式向用户展示消息,具有更多的风格。这可能吗? 最佳答案 它们可通过 Fac
我是一名优秀的程序员,十分优秀!