本文整理匯總了Golang中github.com/pebbe/zmq4.Socket.Close方法的典型用法代碼示例。如果您正苦於以下問題:Golang Socket.Close方法的具體用法?Golang Socket.Close怎麽用?Golang Socket.Close使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/pebbe/zmq4.Socket
的用法示例。
在下文中一共展示了Socket.Close方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: run
func (c *WorkerConnection) run(socket *zmq.Socket) {
defer socket.Close()
socket.SetRcvtimeo(c.activeTimeout)
for {
select {
case <-c.quit:
close(c.wait)
return
case request := <-c.requestChannel:
if _, err := socket.SendMessage(request); err != nil {
log.Println("Failed to send request:", err)
}
case request := <-c.connectionChan:
go c.handleConnectionRequest(socket, request)
default:
message, err := socket.RecvMessageBytes(0)
if err != nil {
// Needed to yield to goroutines when GOMAXPROCS is 1.
// Note: The 1.3 preemptive scheduler doesn't seem to work here,
// so this is still required.
runtime.Gosched()
break
}
socket.SetRcvtimeo(c.activeTimeout)
go c.handleResponse(message)
}
}
}
示例2: StartZeromqProtobufTest
func StartZeromqProtobufTest(address string, clients int, requestsPerClient int, messageSize int, timer metrics.Timer, requestSize *int) func(wg *sync.WaitGroup) {
return func(wg *sync.WaitGroup) {
var socket *zmq.Socket
var err error
var request []byte
if socket, err = zmq.NewSocket(zmq.REQ); err != nil {
log.Fatal(err)
}
defer socket.Close()
defer wg.Done()
if err = socket.Connect("tcp://" + address); err != nil {
log.Fatal(err)
}
if request, err = proto.Marshal(&pb.Request{
Method: "TEST",
Payload: strings.Repeat("a", messageSize),
}); err != nil {
log.Fatal(err)
}
*requestSize = len(request)
for i := 0; i < requestsPerClient; i++ {
timer.Time(func() {
socket.SendBytes(request, 0)
socket.Recv(0)
})
}
}
}
示例3: TestSocketEvent
func TestSocketEvent(t *testing.T) {
var rep *zmq.Socket
defer func() {
if rep != nil {
rep.SetLinger(0)
rep.Close()
}
}()
// REP socket
rep, err := zmq.NewSocket(zmq.REP)
if err != nil {
t.Fatal("NewSocket:", err)
}
// REP socket monitor, all events
err = rep.Monitor("inproc://monitor.rep", zmq.EVENT_ALL)
if err != nil {
t.Fatal("rep.Monitor:", err)
}
chMsg := make(chan string, 10)
go rep_socket_monitor("inproc://monitor.rep", chMsg)
time.Sleep(time.Second)
// Generate an event
err = rep.Bind("tcp://*:9689")
if err != nil {
t.Fatal("rep.Bind:", err)
}
rep.Close()
rep = nil
expect := []string{
"EVENT_LISTENING tcp://0.0.0.0:9689",
"EVENT_CLOSED tcp://0.0.0.0:9689",
"Done",
}
i := 0
for msg := range chMsg {
if i < len(expect) {
if msg != expect[i] {
t.Errorf("Expected message %q, got %q", expect[i], msg)
}
i++
} else {
t.Error("Unexpected message: %q", msg)
}
}
for ; i < len(expect); i++ {
t.Errorf("Expected message %q, got nothing", expect[i])
}
}
示例4: serveZMQ
// serveZMQ
func serveZMQ(zmqPort int, channelStr chan string, metrics metricContainer) {
var zmqListener *zmq.Socket
if channelStr == nil {
listenerURL := "*:" + strconv.Itoa(zmqPort)
fmt.Println("ZeroMQ listening on port: " + listenerURL)
zmqListener, _ = zmq.NewSocket(zmq.PULL)
defer zmqListener.Close()
zmqListener.Bind("tcp://" + listenerURL)
}
for {
var msg string
if channelStr == nil {
// Wait for next request from client
var err error
msg, err = zmqListener.Recv(0)
if err != nil {
fmt.Printf("Error in receive: %v", err)
break
}
} else {
msg = <-channelStr
}
// unmarshall bulked data
var bulk []string
err := json.Unmarshal([]byte(msg), &bulk)
if err != nil {
fmt.Println("json unmarshall error:", err)
}
// extra data
for _, data := range bulk {
dtime, _ := utils.ParseDate4(data[:19])
//dtime, err := time.Parse(dtFormat, data[:19]) // date time
if err != nil {
fmt.Println("time.Parse error:", err)
}
value := data[20:]
intval, _ := strconv.Atoi(value)
m := metric{dtime, intval}
metrics.AddMetric(&m)
//fmt.Println("At ", dtime, " value=", value)
}
}
}
示例5: main
// main entry point for data writer
func main() {
var err error
var writerSocket *zmq4.Socket
var eventSubSocket *zmq4.Socket
fog.Info("program starts")
if writerSocket, err = createWriterSocket(); err != nil {
fog.Critical("createWriterSocket %s", err)
}
defer writerSocket.Close()
fog.Info("binding writer socket to %s", dataWriterAddress)
if err = writerSocket.Bind(dataWriterAddress); err != nil {
fog.Critical("Bind(%s) %s", dataWriterAddress, err)
}
if eventSubSocket, err = createEventSubSocket(); err != nil {
fog.Critical("createEventSubSocket %s", err)
}
defer eventSubSocket.Close()
fog.Info("connecting event sub socket to %s", eventAggregatorPubAddress)
if err = eventSubSocket.Connect(eventAggregatorPubAddress); err != nil {
fog.Critical("Connect(%s) %s", eventAggregatorPubAddress, err)
}
messageChan := NewMessageHandler()
reactor := zmq4.NewReactor()
reactor.AddChannel(tools.NewSignalWatcher(), 1, tools.SigtermHandler)
reactor.AddSocket(writerSocket, zmq4.POLLIN,
NewWriterSocketHandler(writerSocket, messageChan))
reactor.AddSocket(eventSubSocket, zmq4.POLLIN,
NewEventSubSocketHandler(eventSubSocket))
fog.Debug("starting reactor.Run")
reactor.SetVerbose(true)
err = reactor.Run(reactorPollingInterval)
if err == tools.SigtermError {
fog.Info("program terminates normally due to SIGTERM")
} else if errno, ok := err.(syscall.Errno); ok {
// we can get 'interrupted system call' if we get SIGTERM while
// a socket is waiting on a read. That's not too bad.
if errno == syscall.EINTR {
fog.Warn("reactor.Run returns '%s' assuming SIGTERM", errno)
} else {
fog.Error("reactor.Run returns %T '%s'", errno, errno)
}
} else {
fog.Error("reactor.Run returns %T %s", err, err)
}
}
示例6: TestPairTcp
func TestPairTcp(t *testing.T) {
var sb, sc *zmq.Socket
defer func() {
for _, s := range []*zmq.Socket{sb, sc} {
if s != nil {
s.SetLinger(0)
s.Close()
}
}
}()
sb, err := zmq.NewSocket(zmq.PAIR)
if err != nil {
t.Fatal("NewSocket:", err)
}
err = sb.Bind("tcp://127.0.0.1:9736")
if err != nil {
t.Fatal("sb.Bind:", err)
}
sc, err = zmq.NewSocket(zmq.PAIR)
if err != nil {
t.Fatal("NewSocket:", err)
}
err = sc.Connect("tcp://127.0.0.1:9736")
if err != nil {
t.Fatal("sc.Connect:", err)
}
msg, err := bounce(sb, sc)
if err != nil {
t.Error(msg, err)
}
err = sc.Close()
sc = nil
if err != nil {
t.Error("sc.Close:", err)
}
err = sb.Close()
sb = nil
if err != nil {
t.Error("sb.Close:", err)
}
}
示例7: NewReplyHandler
func NewReplyHandler() chan<- *ReplyMessage {
replyChan := make(chan *ReplyMessage, replyChanCapacity)
pushSockets := make(map[string]*zmq4.Socket)
go func() {
for replyMessage := range replyChan {
marshalledReply, err := json.Marshal(replyMessage.Content)
if err != nil {
fog.Error("unable to marshall reply %s %s", replyMessage, err)
continue
}
var pushSocket *zmq4.Socket
var ok bool
pushSocket, ok = pushSockets[replyMessage.ClientAddress]
if !ok {
fog.Info("creating PUSH socket to %s", replyMessage.ClientAddress)
if pushSocket, err = createPushSocket(); err != nil {
fog.Error("Unable to create PUSH socket for %s %s",
replyMessage.ClientAddress, err)
continue
}
if err = pushSocket.Connect(replyMessage.ClientAddress); err != nil {
fog.Error("Unable to Connect PUSH socket to %s %s",
replyMessage.ClientAddress, err)
pushSocket.Close()
continue
}
pushSockets[replyMessage.ClientAddress] = pushSocket
}
if _, err = pushSocket.SendMessage(marshalledReply); err != nil {
fog.Error("pushSocket SendMessage to %s failed %s",
replyMessage.ClientAddress, err)
pushSocket.Close()
delete(pushSockets, replyMessage.ClientAddress)
continue
}
}
}()
return replyChan
}
示例8: Add
// Add registers a socket for sending and/or receiving. The caller can't
// access the socket directly after this. The send channel (if any) should be
// closed by the caller.
func (io *IO) Add(s *zmq.Socket, send <-chan Data, recv chan<- Data) (err error) {
fd, err := s.GetFd()
if err != nil {
return
}
w := newWorker()
io.lock.Lock()
io.workers[int32(fd)] = w
io.lock.Unlock()
defer func() {
if err != nil {
io.lock.Lock()
delete(io.workers, int32(fd))
io.lock.Unlock()
}
}()
e := &syscall.EpollEvent{
Events: syscall.EPOLLIN | syscall.EPOLLET&0xffffffff,
Fd: int32(fd),
}
if err = syscall.EpollCtl(io.epollFd, syscall.EPOLL_CTL_ADD, fd, e); err != nil {
return
}
state, err := s.GetEvents()
if err != nil {
syscall.EpollCtl(io.epollFd, syscall.EPOLL_CTL_DEL, fd, nil)
return
}
go func() {
defer s.Close()
defer syscall.EpollCtl(io.epollFd, syscall.EPOLL_CTL_DEL, fd, nil)
w.socketLoop(s, send, recv, state)
}()
return
}
示例9: Remove
// Remove closes a socket. If it has been registered, it will be removed. The
// recv channel (if any) will be closed.
func (io *IO) Remove(s *zmq.Socket) (err error) {
fd, err := s.GetFd()
if err != nil {
return
}
io.lock.Lock()
w := io.workers[int32(fd)]
if w != nil {
delete(io.workers, int32(fd))
}
io.lock.Unlock()
if w != nil {
w.close()
} else {
err = s.Close()
}
return
}
示例10: TestMultipleContexts
func TestMultipleContexts(t *testing.T) {
chQuit := make(chan interface{})
chErr := make(chan error, 2)
needQuit := false
var sock1, sock2, serv1, serv2 *zmq.Socket
var serv_ctx1, serv_ctx2, ctx1, ctx2 *zmq.Context
var err error
defer func() {
if needQuit {
chQuit <- true
chQuit <- true
<-chErr
<-chErr
}
for _, s := range []*zmq.Socket{sock1, sock2, serv1, serv2} {
if s != nil {
s.SetLinger(0)
s.Close()
}
}
for _, c := range []*zmq.Context{serv_ctx1, serv_ctx2, ctx1, ctx2} {
if c != nil {
c.Term()
}
}
}()
addr1 := "tcp://127.0.0.1:9997"
addr2 := "tcp://127.0.0.1:9998"
serv_ctx1, err = zmq.NewContext()
if err != nil {
t.Fatal("NewContext:", err)
}
serv1, err = serv_ctx1.NewSocket(zmq.REP)
if err != nil {
t.Fatal("NewSocket:", err)
}
err = serv1.Bind(addr1)
if err != nil {
t.Fatal("Bind:", err)
}
serv_ctx2, err = zmq.NewContext()
if err != nil {
t.Fatal("NewContext:", err)
}
serv2, err = serv_ctx2.NewSocket(zmq.REP)
if err != nil {
t.Fatal("NewSocket:", err)
}
err = serv2.Bind(addr2)
if err != nil {
t.Fatal("Bind:", err)
}
new_service := func(sock *zmq.Socket, addr string) {
socket_handler := func(state zmq.State) error {
msg, err := sock.RecvMessage(0)
if err != nil {
return err
}
_, err = sock.SendMessage(addr, msg)
return err
}
quit_handler := func(interface{}) error {
return errors.New("quit")
}
reactor := zmq.NewReactor()
reactor.AddSocket(sock, zmq.POLLIN, socket_handler)
reactor.AddChannel(chQuit, 1, quit_handler)
err = reactor.Run(100 * time.Millisecond)
chErr <- err
}
go new_service(serv1, addr1)
go new_service(serv2, addr2)
needQuit = true
time.Sleep(time.Second)
// default context
sock1, err = zmq.NewSocket(zmq.REQ)
if err != nil {
t.Fatal("NewSocket:", err)
}
sock2, err = zmq.NewSocket(zmq.REQ)
if err != nil {
t.Fatal("NewSocket:", err)
}
err = sock1.Connect(addr1)
if err != nil {
t.Fatal("sock1.Connect:", err)
}
err = sock2.Connect(addr2)
if err != nil {
//.........這裏部分代碼省略.........
示例11: TestSecurityPlain
func TestSecurityPlain(t *testing.T) {
time.Sleep(100 * time.Millisecond)
var handler, server, client *zmq.Socket
defer func() {
for _, s := range []*zmq.Socket{handler} {
if s != nil {
s.SetLinger(0)
s.Close()
}
}
}()
handler, err := zmq.NewSocket(zmq.REP)
if err != nil {
t.Fatal("NewSocket:", err)
}
err = handler.Bind("inproc://zeromq.zap.01")
if err != nil {
t.Fatal("handler.Bind:", err)
}
doHandler := func(state zmq.State) error {
msg, err := handler.RecvMessage(0)
if err != nil {
return err // Terminating
}
version := msg[0]
sequence := msg[1]
// domain := msg[2]
// address := msg[3]
identity := msg[4]
mechanism := msg[5]
username := msg[6]
password := msg[7]
if version != "1.0" {
return errors.New("version != 1.0")
}
if mechanism != "PLAIN" {
return errors.New("mechanism != PLAIN")
}
if identity != "IDENT" {
return errors.New("identity != IDENT")
}
if username == "admin" && password == "password" {
handler.SendMessage(version, sequence, "200", "OK", "anonymous", "")
} else {
handler.SendMessage(version, sequence, "400", "Invalid username or password", "", "")
}
return nil
}
doQuit := func(i interface{}) error {
err := handler.Close()
if err != nil {
t.Error("handler.Close:", err)
}
return errors.New("Quit")
}
quit := make(chan interface{})
reactor := zmq.NewReactor()
reactor.AddSocket(handler, zmq.POLLIN, doHandler)
reactor.AddChannel(quit, 0, doQuit)
go func() {
reactor.Run(100 * time.Millisecond)
quit <- true
}()
defer func() {
quit <- true
<-quit
close(quit)
}()
// Server socket will accept connections
server, err = zmq.NewSocket(zmq.DEALER)
if err != nil {
t.Fatal("NewSocket", err)
}
err = server.SetIdentity("IDENT")
if err != nil {
t.Fatal("server.SetIdentity:", err)
}
err = server.SetPlainServer(1)
if err != nil {
t.Fatal("server.SetPlainServer(1):", err)
}
err = server.Bind("tcp://127.0.0.1:9998")
if err != nil {
t.Fatal("server.Bind")
}
// Check PLAIN security with correct username/password
client, err = zmq.NewSocket(zmq.DEALER)
if err != nil {
t.Fatal("NewSocket:", err)
}
//.........這裏部分代碼省略.........
示例12: TestAbstractIpc
func TestAbstractIpc(t *testing.T) {
var sb, sc *zmq.Socket
defer func() {
for _, s := range []*zmq.Socket{sb, sc} {
if s != nil {
s.SetLinger(0)
s.Close()
}
}
}()
addr := "ipc://@/tmp/tester"
// This is Linux only
if runtime.GOOS != "linux" {
t.Skip("Only on Linux")
}
sb, err := zmq.NewSocket(zmq.PAIR)
if err != nil {
t.Fatal("NewSocket:", err)
}
err = sb.Bind(addr)
if err != nil {
t.Fatal("sb.Bind:", err)
}
endpoint, err := sb.GetLastEndpoint()
expected := "ipc://@/tmp/tester"
if endpoint != expected || err != nil {
t.Fatalf("sb.GetLastEndpoint: expected 'nil' %q, got '%v' %q", expected, err, endpoint)
return
}
sc, err = zmq.NewSocket(zmq.PAIR)
if err != nil {
t.Fatal("NewSocket:", err)
}
err = sc.Connect(addr)
if err != nil {
t.Fatal("sc.Bind:", err)
}
resp, err := bounce(sb, sc)
if err != nil {
t.Error(resp, err)
}
err = sc.Close()
sc = nil
if err != nil {
t.Fatal("sc.Close:", err)
}
err = sb.Close()
sb = nil
if err != nil {
t.Fatal("sb.Close:", err)
}
}
示例13: TestSecurityCurve
func TestSecurityCurve(t *testing.T) {
time.Sleep(100 * time.Millisecond)
var handler, server, client *zmq.Socket
defer func() {
for _, s := range []*zmq.Socket{handler} {
if s != nil {
s.SetLinger(0)
s.Close()
}
}
}()
if _, minor, _ := zmq.Version(); minor >= 1 && !zmq.HasCurve() {
t.Skip("Curve not available")
}
// Generate new keypairs for this test
client_public, client_secret, err := zmq.NewCurveKeypair()
if err != nil {
t.Fatal("NewCurveKeypair:", err)
}
server_public, server_secret, err := zmq.NewCurveKeypair()
if err != nil {
t.Fatal("NewCurveKeypair:", err)
}
handler, err = zmq.NewSocket(zmq.REP)
if err != nil {
t.Fatal("NewSocket:", err)
}
err = handler.Bind("inproc://zeromq.zap.01")
if err != nil {
t.Fatal("handler.Bind:", err)
}
doHandler := func(state zmq.State) error {
msg, err := handler.RecvMessage(0)
if err != nil {
return err // Terminating
}
version := msg[0]
sequence := msg[1]
// domain := msg[2]
// address := msg[3]
identity := msg[4]
mechanism := msg[5]
client_key := msg[6]
client_key_text := zmq.Z85encode(client_key)
if version != "1.0" {
return errors.New("version != 1.0")
}
if mechanism != "CURVE" {
return errors.New("mechanism != CURVE")
}
if identity != "IDENT" {
return errors.New("identity != IDENT")
}
if client_key_text == client_public {
handler.SendMessage(version, sequence, "200", "OK", "anonymous", "")
} else {
handler.SendMessage(version, sequence, "400", "Invalid client public key", "", "")
}
return nil
}
doQuit := func(i interface{}) error {
err := handler.Close()
handler = nil
if err != nil {
t.Error("handler.Close:", err)
}
return errors.New("Quit")
}
quit := make(chan interface{})
reactor := zmq.NewReactor()
reactor.AddSocket(handler, zmq.POLLIN, doHandler)
reactor.AddChannel(quit, 0, doQuit)
go func() {
reactor.Run(100 * time.Millisecond)
quit <- true
}()
defer func() {
quit <- true
<-quit
close(quit)
}()
// Server socket will accept connections
server, err = zmq.NewSocket(zmq.DEALER)
if err != nil {
t.Fatal("NewSocket:", err)
}
err = server.SetCurveServer(1)
if err != nil {
t.Fatal("server.SetCurveServer(1):", err)
//.........這裏部分代碼省略.........
示例14: TestSecurityNull
func TestSecurityNull(t *testing.T) {
time.Sleep(100 * time.Millisecond)
var handler, server, client *zmq.Socket
defer func() {
for _, s := range []*zmq.Socket{handler} {
if s != nil {
s.SetLinger(0)
s.Close()
}
}
}()
handler, err := zmq.NewSocket(zmq.REP)
if err != nil {
t.Fatal("NewSocket:", err)
}
err = handler.Bind("inproc://zeromq.zap.01")
if err != nil {
t.Fatal("handler.Bind:", err)
}
doHandler := func(state zmq.State) error {
msg, err := handler.RecvMessage(0)
if err != nil {
return err // Terminating
}
version := msg[0]
sequence := msg[1]
domain := msg[2]
// address := msg[3]
// identity := msg[4]
mechanism := msg[5]
if version != "1.0" {
return errors.New("version != 1.0")
}
if mechanism != "NULL" {
return errors.New("mechanism != NULL")
}
if domain == "TEST" {
handler.SendMessage(version, sequence, "200", "OK", "anonymous", "")
} else {
handler.SendMessage(version, sequence, "400", "BAD DOMAIN", "", "")
}
return nil
}
doQuit := func(i interface{}) error {
err := handler.Close()
handler = nil
if err != nil {
t.Error("handler.Close:", err)
}
return errors.New("Quit")
}
quit := make(chan interface{})
reactor := zmq.NewReactor()
reactor.AddSocket(handler, zmq.POLLIN, doHandler)
reactor.AddChannel(quit, 0, doQuit)
go func() {
reactor.Run(100 * time.Millisecond)
quit <- true
}()
defer func() {
quit <- true
<-quit
close(quit)
}()
// We bounce between a binding server and a connecting client
server, err = zmq.NewSocket(zmq.DEALER)
if err != nil {
t.Fatal("NewSocket:", err)
}
client, err = zmq.NewSocket(zmq.DEALER)
if err != nil {
t.Fatal("NewSocket:", err)
}
// We first test client/server with no ZAP domain
// Libzmq does not call our ZAP handler, the connect must succeed
err = server.Bind("tcp://127.0.0.1:9683")
if err != nil {
t.Fatal("server.Bind:", err)
}
err = client.Connect("tcp://127.0.0.1:9683")
if err != nil {
t.Fatal("client.Connect:", err)
}
msg, err := bounce(server, client)
if err != nil {
t.Error(msg, err)
}
server.Unbind("tcp://127.0.0.1:9683")
client.Disconnect("tcp://127.0.0.1:9683")
//.........這裏部分代碼省略.........
示例15: TestHwm
func TestHwm(t *testing.T) {
MAX_SENDS := 10000
BIND_FIRST := 1
CONNECT_FIRST := 2
test_defaults := func() (result int) {
result = -1
// Set up bind socket
bind_socket, err := zmq.NewSocket(zmq.PULL)
if err != nil {
t.Error("NewSocket:", err)
return
}
defer func() {
err := bind_socket.Close()
if err != nil {
t.Error("bind_socket.Close:", err)
}
}()
err = bind_socket.Bind("inproc://a")
if err != nil {
t.Error("bind_socket.Bind:", err)
return
}
// Set up connect socket
connect_socket, err := zmq.NewSocket(zmq.PUSH)
if err != nil {
t.Error("NewSocket:", err)
return
}
defer func() {
err := connect_socket.Close()
if err != nil {
t.Error("connect_socket.Close:", err)
}
}()
err = connect_socket.Connect("inproc://a")
if err != nil {
t.Error("connect_socket.Connect:", err)
return
}
// Send until we block
send_count := 0
for send_count < MAX_SENDS {
_, err := connect_socket.Send("", zmq.DONTWAIT)
if err != nil {
break
}
send_count++
}
// Now receive all sent messages
recv_count := 0
for {
_, err := bind_socket.Recv(zmq.DONTWAIT)
if err != nil {
break
}
recv_count++
}
if send_count != recv_count {
t.Error("test_defaults: send_count == recv_count")
}
return send_count
}
count_msg := func(send_hwm, recv_hwm, testType int) (result int) {
result = -1
var bind_socket, connect_socket *zmq.Socket
var err error
if testType == BIND_FIRST {
// Set up bind socket
bind_socket, err = zmq.NewSocket(zmq.PULL)
if err != nil {
t.Error("NewSocket:", err)
return
}
defer func() {
err := bind_socket.Close()
if err != nil {
t.Error("bind_socket.Close:", err)
}
}()
err = bind_socket.SetRcvhwm(recv_hwm)
if err != nil {
t.Error("bind_socket.SetRcvhwm:", err)
return
}
//.........這裏部分代碼省略.........