forked from zimbatm/socketmaster
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
01ec596
commit 1c02062
Showing
9 changed files
with
242 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package child | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"strconv" | ||
"syscall" | ||
) | ||
|
||
var ( | ||
// ErrZeroMasterPID returned when given master PID is zero | ||
ErrZeroMasterPID = errors.New("master PID is zero or empty") | ||
) | ||
|
||
// NotifyMaster notifies the master about child readyness | ||
func NotifyMaster() error { | ||
pidStr := os.Getenv("SOCKETMASTER_PID") | ||
if pidStr == "" { | ||
return ErrZeroMasterPID | ||
} | ||
|
||
masterPID, err := strconv.Atoi(pidStr) | ||
if err != nil { | ||
return err | ||
} | ||
if masterPID == 0 { | ||
return ErrZeroMasterPID | ||
} | ||
|
||
proc, err := os.FindProcess(masterPID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return proc.Signal(syscall.SIGUSR1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package slave | ||
|
||
import ( | ||
"net" | ||
"sync" | ||
) | ||
|
||
type trackedConn struct { | ||
net.Conn | ||
listener *TrackingListener | ||
once sync.Once | ||
} | ||
|
||
// TODO: Is Close called even if it's the client who closed the connection ? | ||
func (self *trackedConn) Close() error { | ||
// Make sure Done() is not called twice here | ||
self.once.Do(func() { self.listener.wg.Done() }) | ||
return self.Conn.Close() | ||
} | ||
|
||
type TrackingListener struct { | ||
net.Listener | ||
wg sync.WaitGroup | ||
} | ||
|
||
func NewTrackingListener(listener net.Listener) *TrackingListener { | ||
return &TrackingListener{ | ||
Listener: listener, | ||
} | ||
} | ||
|
||
func (self *TrackingListener) Accept() (net.Conn, error) { | ||
self.wg.Add(1) | ||
|
||
conn, err := self.Listener.Accept() | ||
if err != nil { | ||
self.wg.Done() | ||
return nil, err | ||
} | ||
|
||
conn2 := &trackedConn{ | ||
Conn: conn, | ||
listener: self, | ||
} | ||
|
||
return conn2, nil | ||
} | ||
|
||
func (self *TrackingListener) WaitForChildren() { | ||
self.wg.Wait() | ||
} |
Oops, something went wrong.